Sub topic 2
Describing arguments with JSON Schema
This chapter assumes you can read JSON and write basic Python. You should also know that chat APIs take a list of role-tagged messages. Here you will learn how a tool's arguments are described so the model produces them in the right shape.
Why the model needs a schema
The model writes the arguments for a tool call as JSON. It cannot see your function signature. It only sees the description you send. JSON Schema is the standard vocabulary for that description. It says which keys exist, what type each value has, and which keys are required.
The schema guides the model. It does not guarantee anything. Treat every set of arguments as untrusted input and check it before use. A later chapter covers validation.
The basic shape
Tool parameters are almost always an object schema. The object lists its properties.
{
"type": "object",
"properties": {
"city": {"type": "string", "description": "City name, for example Oslo"},
"unit": {"type": "string", "enum": ["celsius", "fahrenheit"]}
},
"required": ["city"],
"additionalProperties": false
}Each keyword has a job:
| Keyword | Meaning |
|---|---|
| `type` | The JSON type: `string`, `number`, `integer`, `boolean`, `array`, `object` |
| `properties` | A map from argument name to its own schema |
| `required` | Names that must be present |
| `enum` | The only allowed values |
| `description` | Plain text that tells the model what the value means |
| `additionalProperties` | When `false`, extra keys are not allowed |
In this example, city is required and unit is optional. A call with only {"city": "Oslo"} is valid. A call with {"city": "Oslo", "country": "NO"} is not, because of additionalProperties.
Useful constraints
Beyond the basics, a few keywords narrow what the model can send:
enumlimits a string to a fixed list. Use it whenever the set of choices is small and known.minimumandmaximumbound numbers.minLength,maxLengthandpatternconstrain strings.itemsdescribes the elements of anarray.defaultdocuments the value your code uses when the argument is missing.
Here is a schema that uses several of them:
{
"type": "object",
"properties": {
"query": {"type": "string", "minLength": 1},
"limit": {"type": "integer", "minimum": 1, "maximum": 20, "default": 5},
"tags": {"type": "array", "items": {"type": "string"}}
},
"required": ["query"],
"additionalProperties": false
}Providers differ in how much of JSON Schema they honor. Some ignore keywords such as pattern or minimum when they steer the model, and strict modes may accept only a subset. Check your provider's documentation, and keep the schema simple where you can.
Common errors
- Missing
required. If you leave it out, every argument is optional. The model may omit a value your function needs. - Vague descriptions. A property named
qwith no description invites guesses. Write what the value is and give an example. - Free text where an enum fits. If the model can send
"C","celsius"or"Celsius", your code must handle all of them. Anenumremoves that problem. - Trusting the output. The model can still send a wrong type or an out-of-range number, so validate.
Exercise
A tool takes a required order_id string and an optional priority that must be low, normal or high. No other keys are allowed. Write the parameters schema.
Show answer
{"type": "object", "properties": {"orderid": {"type": "string", "description": "Order identifier"}, "priority": {"type": "string", "enum": ["low", "normal", "high"]}}, "required": ["orderid"], "additionalProperties": false}