Command Palette

Search for a command to run...

Hectal
Phase 16: Python in the AI Era
Python · Exercise 5 of 6 PreviousNext
Advanced

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

  1. Runs a valid call and returns the result as a string
  2. Unknown tool
  3. Invalid JSON
  4. Missing required argument
  5. JSON that is not an object becomes an error string
  6. 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.

main.py
Sign in to run your code.

Output

Press Run to see what your program prints.