Sub topic 5
Defining Parameters with JSON Schema
This chapter covers how to write the input_schema of a tool definition. It assumes you can call an LLM API, read JSON, and know the basic JSON Schema types: string, integer, object, and array. Later chapters cover parameter descriptions, enums, and required fields in more depth. Here the goal is the working model: what the schema is, what the model does with it, and where it goes wrong.
What the schema does
A tool definition has a name, a description, and a schema for its arguments. The schema is written in JSON Schema, a standard format for describing the shape of JSON data. The model reads it as part of its prompt. When it decides to call the tool, it produces a JSON object that it expects to match the schema.
Two facts shape everything that follows:
- The model sees the schema as text. It does not run a validator while it writes arguments, so a schema guides the model without guaranteeing valid output.
- Your server receives whatever the model produced. Some providers can constrain output to the schema, but you should still check arguments yourself.
The top-level object
Tool arguments are always a JSON object, so the schema starts with "type": "object". Each argument is an entry under properties. The required array lists the names the model must always supply.
Here is the well-formed definition from the plan.
{
"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"]
}
}Read it as the model would. customer_id is a string and is the only required field. status is one of four values. limit is an integer between 1 and 50. Every property has a description that says what it is, and the ones with formats or defaults state them.
Contrast with a weak schema
Now compare a schema that is technically valid but gives the model almost nothing to work with.
{
"name": "do_order_thing",
"description": "Handles orders.",
"input_schema": {
"type": "object",
"properties": {
"data": { "type": "string" },
"mode": { "type": "string" }
}
}
}This passes any JSON Schema validator. It still fails the model in four ways:
datais a free-text string, so the model must guess whether to send an ID, a search phrase, or a JSON blob.modehas no enum, so the model invents values like"lookup","search", or"get".- Nothing is marked required, so the model may omit both fields.
- No descriptions exist, so nothing explains formats or defaults.
Each guess the model makes is a chance for a bad call. A good schema removes guesses.
Choosing types
Pick the narrowest type that fits the data.
| Data | Schema type | Notes |
|---|---|---|
| IDs, names, free text | `string` | Describe the format, for example `'cus_4821'`. |
| Counts, limits, page numbers | `integer` | Use `number` only when fractions are valid. |
| Flags | `boolean` | Say what `true` does and what the default is. |
| Fixed choices | `string` with `enum` | Lists every allowed value. |
| Dates | `string` | Name the format, such as YYYY-MM-DD. |
| Lists | `array` with `items` | Always give `items` a type. |
JSON Schema has a format keyword, for example "format": "date". Support for it varies by provider and by validator, and some APIs ignore it. The safe approach is to state the format in the description as well. The example above does this for created_after.
Arrays and nested objects
You can nest objects and arrays. Keep the structure only as deep as the task needs, because models make more mistakes as nesting grows.
{
"name": "get_orders_by_ids",
"description": "Fetch several orders at once by ID. Use this when you already have two or more order IDs. For a single ID, use get_order.",
"input_schema": {
"type": "object",
"properties": {
"order_ids": {
"type": "array",
"items": { "type": "string" },
"minItems": 1,
"maxItems": 20,
"description": "Order IDs such as 'ord_1001'. Between 1 and 20 IDs."
}
},
"required": ["order_ids"]
}
}The items entry tells the model what each element looks like. minItems and maxItems bound the list. If an array holds objects, give items its own type, properties, and required.
Prefer flat parameters when you can. Four top-level fields are easier for a model to fill than one object that contains four fields. Use nesting when the data really has a grouped structure, such as an address.
Extra properties and strictness
By default, JSON Schema allows properties you did not list. A model may add fields you never defined. Setting "additionalProperties": false on the object says no unlisted properties are allowed.
Some providers require this setting when you turn on strict or structured tool calling. In that mode, they may also require every property to appear in required, with optional values expressed as a type that allows null. Check your provider's documentation for the exact rules, because they differ and change.
Schema as guidance, code as enforcement
Constraints in the schema, such as minimum, maximum, and enum, steer the model. They do not protect your system. A model can still send "limit": 500 or "status": "Shipped", particularly when the API does not enforce the schema.
So treat every argument as untrusted input. Check types, ranges, and formats in your own code, and return a clear error when something fails. The get_order function below does this for IDs.
ORDERS = {
"ord_1001": {"id": "ord_1001", "status": "shipped", "total": 42.5},
}
def get_order(order_id: str) -> dict:
if not isinstance(order_id, str) or not order_id.startswith("ord_"):
return {
"is_error": True,
"message": (
f"Invalid order_id {order_id!r}. IDs look like 'ord_1001'. "
"If you do not have an ID, call search_orders first."
),
}
order = ORDERS.get(order_id)
if order is None:
return {
"is_error": True,
"message": f"No order found with id {order_id!r}. Check the ID with the user or use search_orders.",
}
return {"is_error": False, "order": order}The error text names the bad value, shows the correct format, and points to another tool. That gives the model enough to correct itself on the next turn.
How the pieces fit
Common mistakes
- Using
stringfor everything. Useinteger,boolean,enum, andarrayso the model has fewer choices. - Leaving arrays without
items. The model then guesses the element type. - Marking too much as required. The model will invent values for fields it cannot know. Require only what the tool cannot run without.
- Over-nesting. Flatten where the data allows.
- Relying on the schema alone. Validate on the server.
- Using unsupported keywords. Check your provider, then test.
Exercise
A tool has the parameter "priority": { "type": "string" }. The model sometimes sends "urgent", sometimes "High", and your code only accepts "low", "medium", and "high". Rewrite the parameter definition so the model is steered to valid values, then name one thing your server code must still do.
Show answer
Use an enum and a description: "priority": { "type": "string", "enum": ["low", "medium", "high"], "description": "Priority level of the ticket. Defaults to 'medium' if omitted." }. The server must still validate the value, because the model can send something outside the enum, and return a clear error that lists the allowed values.
Exercise
Write the schema for a tool get_orders_by_ids that takes a list of 1 to 20 order IDs. Which keywords bound the list size, and which keyword defines the type of each element?
Show answer
Use "order_ids": { "type": "array", "items": { "type": "string" }, "minItems": 1, "maxItems": 20 } and list order_ids under required. minItems and maxItems bound the size. items defines the element type.