AI Agents by HandAll problemsAbout

All problems › Agent problem · easy · ~15 min

Tool schemas and input validation

tool schemas input validation

Solve it in your browser →

Write the tool definitions (~15 min)

tool_schemas.py is a warehouse assistant. The three handlers (check_stock, create_transfer, set_reorder_points) and the loop are written, but TOOLS is empty, so the model can't call anything. Two of the tools change state, and the model sometimes sends inputs that don't match a schema, so inputs must be checked before a handler runs.

Part 1. Fill in TOOLS with one definition per handler (name, description, input_schema) that follows this contract:

ToolParameterTypeRequiredNotes
check_stockskuslist of stringsyese.g. ["SKU-100", "SKU-300"]
warehousestring: tokyo, osaka or allnodefaults to all
create_transferskustringyes
quantityintegeryesat least 1
from_warehousestring: tokyo or osakayes
to_warehousestring: tokyo or osakayes
notestringno
set_reorder_pointspointsobject mapping a SKU to a whole numberyese.g. {"SKU-100": 20, "SKU-300": 12}

No other parameters are allowed. Write descriptions the model can act on.

Part 2. Implement validate_input(tool, tool_input) -> list[str], driven by the tool's input_schema, and call it in the loop before any handler runs. An input with problems is not executed: the model gets an is_error result listing them, so it can fix the call. It must catch:

  • a required parameter that's missing,
  • a parameter that isn't in the schema,
  • a top-level value of the wrong JSON type (string, integer, number, boolean, array, object; in Python True is an int, but true is not a JSON integer),
  • a value that isn't in the parameter's enum.

Each problem names the parameter. You don't need to check inside arrays or objects, or minimum; the handlers already reject bad values by raising.

Keep the loop's existing behaviour: an unknown tool or a handler that raises is an is_error result, and max_turns raises RuntimeError. run_agent(prompt, client=None, max_turns=10) -> str and the module-level STOCK / TRANSFERS / REORDER_POINTS must stay.

Be ready to explain why you validate even though the model was given the schema, and what strict: true on a tool definition would change.

Try it: python -m tool_schemas Check it: pytest tests/test_tool_schemas.py

Starter code and hidden tests open in the editor. Nothing to install.