Sub topic 8
Deciding what is required and what is optional
This chapter assumes you can read JSON Schema (type, properties, required) and short Python examples. It also assumes you have seen a tool definition with a name, description, and input schema.
What `required` tells the model
The required array lists the parameters the model must supply on every call. The model reads it as a rule. If a parameter is listed there and the user has not given a value, the model will either ask the user or guess. Guessing is the common failure, so be deliberate about what you list.
Every parameter outside required is optional. The model may include it or leave it out.
A rule for choosing
Make a parameter required only if the tool cannot do anything useful without it and no safe default exists. Use these questions:
- Can the tool run without it? If not, require it.
- Is there a default that is right for most calls? If so, make the parameter optional and state the default.
- Would a wrong guess cause harm, such as acting on the wrong customer? If so, require it and tell the model to ask the user when it is missing.
In search_orders, customer_id is required because a search with no customer makes no sense, and a guessed ID would return another person's data. The other three parameters are filters. Leaving one out means "do not filter on this."
{
"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."
},
"limit": {
"type": "integer",
"minimum": 1,
"maximum": 50,
"description": "Maximum number of orders to return. Defaults to 10 if omitted."
}
},
"required": ["customer_id"]
}Say what omission means
For every optional parameter, the description should state what happens when it is left out. "Omit to include all statuses" and "Defaults to 10 if omitted" both do this. Without that sentence, the model may invent a value to be safe, or it may skip a parameter it should have filled in.
Apply defaults on the server
Do not rely on the model to send the default. Apply it in your code, so the behavior matches what the description promises.
def search_orders(customer_id: str, status: str | None = None, limit: int = 10) -> dict:
limit = max(1, min(limit, 50))
return {"customer_id": customer_id, "status": status, "limit": limit}Common mistakes
- Requiring everything. The model then invents values for filters the user never mentioned, and results get narrower than intended.
- Requiring nothing. The model calls the tool with an empty object, and your code has to handle a missing ID.
- Optional parameters that depend on each other. If
start_dateonly makes sense withend_date, say so in both descriptions, and check the pair on the server. - Nullable stand-ins. Avoid making a parameter required and then telling the model to send an empty string when it has no value. Make it optional instead.
Exercise
A cancel_order tool takes order_id and an optional reason. A user says "cancel my order" without giving an ID. Should order_id be required, and what should the description of order_id tell the model to do?
Show answer
Yes, order_id should be required. Cancelling the wrong order is hard to undo, and no safe default exists. Its description should give the format, for example 'ord_1001', and tell the model to ask the user, or call a search tool, when it does not have the ID. The reason can stay optional because the cancellation works without it.