Run one tool call safely
Write run_tool_call(call, tools). call is a dict with "name" and "arguments" (a JSON string, as in the OpenAI-compatible API). tools maps a name to a pair (function, required_argument_names), where the second part is a list of argument names. The model asked for this call, and your code decides how to run it (Topic 16.10).
Return the function's result as a string. Never raise. Return "error: unknown tool NAME" for an unknown name, "error: arguments are not valid JSON" for bad JSON (or JSON that is not an object), and "error: missing argument KEY" for a missing required argument. If the function raises TypeError, return "error: " followed by the message.
What Check tests
- Runs a valid call and returns the result as a string
- Unknown tool
- Invalid JSON
- Missing required argument
- JSON that is not an object becomes an error string
- Extra argument becomes an error string, not a crash
Stuck? One hint at a time
The solution unlocks after your first Check. Try first: that is where the learning happens.
Output
Press Run to see what your program prints.