Sub topic 5
Defining parameters with JSON Schema
This chapter assumes you can read JSON, know the basic JSON Schema types (string, integer, object, array), and have called an LLM API with a tool before. It covers the input_schema part of a tool definition.
What the schema does
The model never runs your tool. It reads the schema and writes a JSON object of arguments that it hopes matches. The schema is the only contract between the model and your code. The tighter and clearer it is, the fewer bad calls you get.
Most APIs expect the top level to be an object. Each argument is a key under properties. Anthropic-style APIs name this field input_schema. OpenAI-style APIs name it parameters.
A worked example
Here is the schema from a search_orders tool.
{
"name": "search_orders",
"description": "Search a customer's orders by status and date. Use this when the user does not give an order ID. If the user gives an order ID, use get_order instead. Returns up to `limit` orders, newest first, each with id, status, total, and created_at.",
"input_schema": {
"type": "object",
"properties": {
"customer_id": {
"type": "string",
"description": "Customer ID, for example 'cus_4821'. Ask the user if you do not know it."
},
"status": {
"type": "string",
"enum": ["pending", "shipped", "delivered", "cancelled"],
"description": "Only return orders with this status. Omit to include all statuses."
},
"created_after": {
"type": "string",
"description": "Only return orders created on or after this date, in YYYY-MM-DD format."
},
"limit": {
"type": "integer",
"minimum": 1,
"maximum": 50,
"description": "Maximum number of orders to return. Defaults to 10 if omitted."
}
},
"required": ["customer_id"]
}
}Each field does a specific job:
typetells the model what kind of value to write. Use the narrowest type that fits.integeris better thannumberfor counts.enumlists the only legal values. The model picks from the list instead of inventing"in_transit".minimumandmaximumbound the integer.requirednames the arguments the call cannot work without. Everything else is optional.descriptionstates formats, defaults, and what to do when a value is unknown.
Compare a weak schema
{
"name": "do_order_thing",
"description": "Handles orders.",
"input_schema": {
"type": "object",
"properties": {
"data": { "type": "string" },
"mode": { "type": "string" }
}
}
}The name and description say nothing about use or limits. data and mode are free text, so the model must guess what to send. Nothing is required, so an empty call looks valid.
Common errors
- Leaving out
typeon a property, which lets the model send anything. - Using a plain string where an
enumfits. - Marking an argument required when the model often cannot know it. It then makes up a value.
- Putting format rules only in your code. If dates must be
YYYY-MM-DD, say so in the description.
Exercise
A tool has a parameter priority that must be one of low, medium, or high. The model keeps sending "urgent". Which schema change fixes this, and what else should you do?
Show answer
Give priority the type string with "enum": ["low", "medium", "high"], and add a description saying what each level means. Also validate the value on the server, because the model's output is not guaranteed to follow the schema.