Validate and Dispatch Tool Calls

medium
Unit 18 · Agents0/3 to milestone

When a model asks to call tools, your runtime has to validate the arguments, run the functions, and turn every failure into a message the model can read and recover from. It must never crash the agent loop.

Each call is {"id": str, "name": str, "arguments": str}, where arguments is a JSON string. The registry maps tool names to (function, params), where params is a dict from parameter name to type (int, float, str or bool). All parameters are required.

Implement dispatch(calls, registry), returning one result per call in the same order: {"id": ..., "content": str(result)} on success, or {"id": ..., "error": message} using the first problem found in this order:

  1. "invalid JSON", if the arguments don't parse to a dict
  2. "unknown tool: <name>"
  3. "unexpected argument: <arg>", for the first unexpected key in sorted order
  4. "missing argument: <param>", in parameter order
  5. "argument <param> must be <type name>". A float parameter accepts ints. bool values are not accepted as int or float.
  6. If the function raises, "<ExceptionType>: <message>"
#agents#tool-use#validation2 visible + 2 hidden tests
solution.py · Python 3.13
Loading editor…
Run your code to see results. ⌘↵ to run · ⌘⇧↵ to submit
Validate and Dispatch Tool Calls · TechGradify