Tools, Schemas and Validation

A model asks for a tool by name with arguments. Check the tool exists and the arguments are complete before running anything.

AI · intermediate · 7 min · 24 steps · free

What you'll learn

tool_schemas.py

def get_weather(city):
    return {'city': city, 'temp_c': 31}

TOOLS = {'get_weather': {'run': get_weather, 'required': ['city']}}

def call_tool(request):
    spec = TOOLS.get(request['tool'])
    if spec is None:
        return {'error': 'unknown tool ' + request['tool']}
    missing = [p for p in spec['required'] if p not in request['args']]
    if missing:
        return {'error': 'missing ' + ', '.join(missing)}
    return spec['run'](**request['args'])

print(call_tool({'tool': 'get_weather', 'args': {'city': 'Chennai'}}))
print(call_tool({'tool': 'get_weather', 'args': {}}))
print(call_tool({'tool': 'book_flight', 'args': {}}))

Step by step

  1. Python defines the function get_weather(). Its body does not run yet: it runs each time the function is called.
  2. Each tool has a function to run and the arguments it requires.
  3. Python defines the function call_tool(). Its body does not run yet: it runs each time the function is called.
  4. Python calls call_tool(), with request = {tool: 'get_weather', args: a dict}. It gets its own names.
  5. spec is created and points to a new dict: {run: function get_weather(), required: a list}.
  6. spec is None is false, so the block under it is skipped.
  7. Nothing missing: every required argument is there.
  8. missing is false, so the block under it is skipped.
  9. Python calls get_weather(), with city = 'Chennai'. It gets its own names.
  10. The function returns {'city': city, 'temp_c': 31}.
  11. get_weather() has finished, so its names are gone.
  12. call_tool() has finished, so its names are gone. It prints {'city': 'Chennai', 'temp_c': 31}.
  13. Python calls call_tool(), with request = {tool: 'get_weather', args: a dict}. It gets its own names.
  14. spec is created and points to a new dict: {run: function get_weather(), required: a list}.
  15. spec is None is false, so the block under it is skipped.
  16. This request forgot city.
  17. missing is true, so the block under it runs.
  18. The function returns {'error': 'missing ' + ', '.join(missing)}.
  19. call_tool() has finished, so its names are gone. It prints {'error': 'missing city'}.
  20. Python calls call_tool(), with request = {tool: 'book_flight', args: a dict}. It gets its own names.
  21. spec is created but does not point to any object yet (null).
  22. spec is None is true, so the block under it runs.
  23. Unknown tool: refused. The model cannot call something you did not give it.
  24. call_tool() has finished, so its names are gone. It prints {'error': 'unknown tool book_flight'}.

Common mistakes

Best practices

Key takeaways

Check yourself

Who actually runs the tool?

Answer: Your code. The model only asks.

Missing argument: best response?

Answer: A clear error

·

More in Agents