Defending AI Agents
עברית Join waitlist
Menu

E3 · Lesson 02

A tool is a schema, a function and an error message, and the model reads two of them

A tool has three parts. The function is the only part the model never sees. The other two are text it reads: the schema, which says what it may ask for, and the error message, which says what went wrong when it asked badly. In this loop, the code you write that the model reads matters more than the code that does the work.

The schema is a prompt

Module 1 lesson 8 showed the rendered prompt: every tool's name, description and parameter schema are serialised into the system turn, and the model reads them like any other text. So a schema does two jobs. It is documentation, which the model may follow or ignore, and it is a contract, which only your code can enforce. One of the lab's tools:

{"type": "function", "function": {"name": "send_email", "description": "Send an e-mail.",
  "parameters": {"type": "object",
    "properties": {"to": {"type": "string", "description": "Recipient address."},
                   "subject": {"type": "string", "description": "Subject line."},
                   "body": {"type": "string", "description": "Plain-text body."}},
    "required": ["to", "subject", "body"]}}}

Nothing in this stops the model from writing {"to": "manager@acme.example", "subject": "Q3" and running out of tokens before the closing brace. Gorilla (Patil et al., 2023) measured the general version of the problem: even the strongest models of the time wrote wrong arguments and invented API usages often enough that a model fine-tuned on API documentation beat them. Two failures need different handling.

Failure 1: not JSON at all

Chat Completions servers send arguments as a string of JSON, so it may not parse. Module 1's loop does this:

args = json.loads(args) if isinstance(args, str) else args   # agent/agent.py

A malformed string raises JSONDecodeError, and the whole run dies with nothing reported. loop/schema.py returns the failure as data instead:

>>> parse_arguments('{"path": "notes/q3-report.txt",}')
(None, "arguments are not valid JSON: Expecting property name enclosed in double quotes at character 31")
>>> parse_arguments("")
({}, None)                  # some servers send "" for a call with no arguments

Failure 2: JSON, but not what the schema asked for

SEND is the check's version of send_email's schema, with "additionalProperties": false added:

>>> check_arguments(SEND, {"to": "x", "subject": 3, "cc": "boss"})
["missing required field 'body'", "'subject' must be string, got integer",
 "unexpected field 'cc' (allowed: body, subject, to)"]

check_arguments covers the part of JSON Schema that tool definitions use: type, properties, required, additionalProperties: false, enum and items. It ignores the rest, such as pattern and format, rather than failing on them. Two details are easy to miss. In JSON Schema 3.0 is an integer and true is not, while in Python True == 1, so a naive isinstance(v, int) accepts a boolean as a count. And without additionalProperties: false, a field the schema does not list passes. The lab's send_email above does not set it, so {"to": ..., "subject": ..., "body": ..., "cc": "boss@acme.example"} passes the check, and the tool silently ignores a recipient the model thought it had added. For an optional field, a misspelling ("subjcet") passes the same way. Tighten the schemas you own. For the ones you do not, the check can only enforce what they say.

The error message is the second prompt

Both errors go back to the model as the tool's result, in a tool message carrying the call's id, and the tool does not run. The id is not optional. Chat Completions servers expect every call in an assistant turn to be answered by a tool message with its id, and many reject the next request if one is missing. A refused call that is silently dropped also leaves the model believing it ran:

ERROR: send_email: invalid arguments: missing required field 'body'.

That message is the model's only information about what went wrong, so write it for a reader that will act on it immediately. Three rules:

  1. Name the field and the fix. "invalid arguments" alone produces the same call again. "missing required field 'body'" produces a call with a body.
  2. List the alternatives. An unknown tool gets the list of real ones: ERROR: no tool named 'email_send'. Available: list_files, read_file, ....
  3. Never let the function run on arguments that failed the check. A tool that "does its best" with {"to": 3} has made a decision the model did not ask for.

How often does it matter? On the twenty lab tasks, qwen3:4b wrote well-formed, schema-valid arguments in all 120 runs (the build): no call refused, no crash. A small model on a tool list of thirteen gets the shape right. The checks are for the day it does not: a longer tool list, a quantised model, a truncated reply, or a schema with an enum the model has never seen. Those rare failures are the ones that crash an unattended loop at three in the morning.

Where the schema cannot help

A schema checks shape, not meaning. {"to": "attacker@evil.example", "subject": "Q3", "body": "..."} passes every rule above. Whether that recipient is allowed is policy, and Modules 3 and 13 put it at the gateway, where the agent cannot rewrite it. Some providers offer a strict mode that constrains decoding to the schema, so that malformed arguments cannot be produced (E2 lesson 4). That removes failure 1, and every failure 2 that the schema expresses, as far as the provider's strict mode supports its keywords. It removes none of the meaning problem.

Check yourself

  1. A tool's schema lists count as {"type": "integer"}. Which of 3, 3.0, "3", true pass, and why does the answer for true differ between JSON Schema and a naive Python check?
  2. Why should a refused call still produce a tool message with the call's id, rather than being dropped silently?
  3. Your tool's description says "date in ISO 8601", but the schema says only {"type": "string"}. A model sends "next Tuesday". Which layer should catch it, and what should the error say?
Answers
  1. 3 and 3.0 pass: JSON Schema treats any number with no fractional part as an integer. "3" fails (a string), and so does true (a boolean). A naive Python isinstance(v, int) accepts True, because bool is a subclass of int, so the check must exclude booleans explicitly.
  2. The model asked for that call and is waiting for its result. Without a result carrying the same id, the next request has an assistant turn with an unanswered call. Some servers reject that outright, and every model then has to guess whether the call happened. The error result tells it exactly what did.
  3. Code, not this loop's schema check. The schema says only "type": "string", and the keywords that could say more, pattern and format, are ones check_arguments ignores. So the tool's own parser must catch it (or an extension of the check), with an error that names the format: 'date' must be ISO 8601 (YYYY-MM-DD), got 'next Tuesday'. The description alone is a request, not a check.