Substitute Prior Results into Tool Arguments
Problem Statement
In a tool plan, one call's arguments reference an earlier call's output via placeholders like $1.city. Resolve these placeholders against a results map before executing.
Background
A placeholder has the form 1.city) or $<step> for the whole result. Given results mapping step id -> value (a dict or scalar), replace any argument whose value is a placeholder string with the looked-up value. Non-placeholder values pass through. A missing reference raises KeyError.
Your Task
Implement:
def resolve_refs(args, results):
- args: dict of name -> value (values may be placeholder strings).
- results: dict of int step -> result.
- Return a new args dict with placeholders resolved.
Input Format
- args (dict), results (dict of int -> value).
Output Format
- A dict.
Sample
print(resolve_refs({"q": "$1.city"}, {1: {"city": "Paris"}}))
Output:
{'q': 'Paris'}
Example
print(resolve_refs({"q": "$1.city"}, {1: {"city": "Paris"}})){'q': 'Paris'}- Iterate through the input dictionary
args, identifying the single entry where the key is"q"and the value is the string"$1.city". - Parse the placeholder string to extract the step identifier and the specific key: the numeric part
1indicates step 1, and the suffixcityindicates the target field. - Look up step 1 in the
resultsmap, which yields the nested dictionary{"city": "Paris"}. - Access the
cityfield within that result, retrieving the scalar value"Paris", and assign it to the output dictionary under the original key"q". - The final output is
{'q': 'Paris'}
Constraints
- A placeholder is a string starting with
$then digits, optionally.key. $nreturnsresults[n];$n.keyreturnsresults[n][key].- Non-placeholder values are returned unchanged; missing step/key raises KeyError.