v9.1.1
Menu

Substitute Prior Results into Tool Arguments

mediumPROTool Use & Function CallingAI AgentsAI Agents 1

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 <step>.<key>∗∗(e.g.∗∗<step>.<key>** (e.g. **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

Input
print(resolve_refs({"q": "$1.city"}, {1: {"city": "Paris"}}))
Output
{'q': 'Paris'}
Reasoning
  • 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 1 indicates step 1, and the suffix city indicates the target field.
  • Look up step 1 in the results map, which yields the nested dictionary {"city": "Paris"}.
  • Access the city field 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.
  • $n returns results[n]; $n.key returns results[n][key].
  • Non-placeholder values are returned unchanged; missing step/key raises KeyError.

Learn this in

More from AI Agents 1

PRO

Editor locked

The code editor is locked for Pro problems. It is only available for free problems. Please upgrade to gain access to the code editor for all problems.

solution.py

Test results

Sign in to run your code against the test cases.