Sub topic 7
Using enums and constraints to rule out bad arguments
This chapter assumes you can read JSON Schema types (string, integer, object, array) and short Python examples. You should also know how a model calls a tool: it reads your schema and produces arguments as JSON.
Why constrain arguments
A free-text parameter lets the model send anything. A model may write "Shipped", "in transit", or "SHIPPED" for a status your code only knows as "shipped". Enums and constraints shrink the set of valid values, so the model has fewer ways to be wrong. Many providers can also use the schema to guide generation, which makes invalid values less likely. Still, treat the schema as a strong hint and not a guarantee. The validation chapter covers server-side checks.
Enums for closed sets
Use enum when the valid values are a fixed list. In the search_orders tool, status is an enum:
{
"status": {
"type": "string",
"enum": ["pending", "shipped", "delivered", "cancelled"],
"description": "Only return orders with this status. Omit to include all statuses."
}
}The model sees every allowed value. The description still matters: it says what happens when the parameter is omitted, which the enum alone cannot express.
Keep enum values distinct and self-explanatory. Names like "a" and "b" force the model to guess. Do not add an "all" value if omitting the parameter already means all.
Numeric and string constraints
Constraints cover ranges and shapes that an enum cannot list.
| Keyword | Applies to | Example use |
|---|---|---|
| `minimum`, `maximum` | integer, number | `limit` between 1 and 50 |
| `minLength`, `maxLength` | string | a search query of 1 to 200 characters |
| `pattern` | string | an ID such as `^cus_[0-9]+$` |
| `minItems`, `maxItems` | array | at most 10 IDs per call |
{
"limit": {
"type": "integer",
"minimum": 1,
"maximum": 50,
"description": "Maximum number of orders to return. Defaults to 10 if omitted."
}
}State the bounds in the description too when they matter. The model reads descriptions reliably, but some providers ignore or strip certain keywords.
Choosing the right tool
Common errors
- Enum too large. Hundreds of values inflate every request. Use a search tool or a lookup instead.
- Enum out of sync with code. If the backend adds
"returned"and the schema does not list it, the model cannot request it. Generate the enum from the same source as your code. - Over-tight constraints. A
patternthat rejects valid IDs makes the tool unusable for those cases. - Relying on constraints alone. Your server must still reject bad values and return a message the model can act on.
Exercise
A tool has a priority parameter typed as a plain string, and the model sometimes sends "urgent!!" while the backend accepts only low, medium, and high. What schema change fixes this, and what should you still do on the server?
Show answer
Add "enum": ["low", "medium", "high"] to the parameter and describe what each level means. On the server, still check the value and return an error naming the allowed values, because the model can still send an invalid value if the provider does not enforce the schema.