Sub topic 10
Designing schemas that models fill in well
This chapter assumes you can write a Pydantic model and a basic JSON Schema, and that you know how schema-constrained output and validation work from earlier chapters. Here you will learn how the design of a schema changes how accurately a model fills it in.
A schema is both a validation rule and a set of instructions. The model reads field names, descriptions, and allowed values as part of its input. A small design change often fixes errors that prompt rewording does not.
Name fields for what they hold
Field names are the first instruction the model sees. A vague name invites a vague value.
- Prefer
customer_emailoverdata1orinfo. - Put the unit in the name when a number has one:
timeout_seconds,price_usd. - Use one convention for all fields, such as
snake_case.
Field order also matters when the output is generated left to right. Each token depends on the tokens before it. If you ask for a category before a summary, the model must pick the category before it has written anything about the ticket. Placing a short free-text field such as reasoning or summary earlier can improve the fields that follow, at the cost of more output tokens.
Use enums and limits to shrink the choices
An enum is a fixed list of allowed values. It removes whole classes of error, such as "Bug", "bug report", and "defect" all appearing for the same idea. Limits do the same for numbers and strings.
from typing import Literal
from pydantic import BaseModel, Field
class TicketTriage(BaseModel):
category: Literal["billing", "bug", "feature_request", "other"]
priority: int = Field(ge=1, le=3, description="1 is most urgent, 3 is least urgent")
summary: str = Field(max_length=200, description="One-sentence summary of the ticket")Two points about this model:
Literalbecomes anenumin the JSON Schema. Include an"other"value so the model has a valid choice when nothing fits. Without it, the model must pick a wrong value.ge,le, andmax_lengthbecomeminimum,maximum, andmaxLength. Providers that use constrained decoding often ignore some of these keywords. Keep validating in your own code, as covered in the validation chapter.
Write descriptions that carry the rules
A description is the right place for a rule the type cannot express. In the model above, "1 is most urgent, 3 is least urgent" stops the model from guessing the direction of the scale. Good descriptions state:
- The meaning of each value, especially for scales and booleans.
- The format, such as "ISO 8601 date, for example 2026-03-14".
- What to do when the information is missing.
Handle missing information on purpose
If a field is required and the source text lacks the value, the model will often invent one. Make absence a legal answer instead.
from pydantic import BaseModel, Field
class Invoice(BaseModel):
vendor: str = Field(description="Company that issued the invoice")
total_usd: float | None = Field(
description="Total in US dollars, or null if the invoice does not state a total"
)
due_date: str | None = Field(
description="Due date as YYYY-MM-DD, or null if no due date is given"
)
print(Invoice.model_json_schema()["required"])The fields have no default, so they stay required in the schema, but null is an allowed value. This matters because some providers require every field to appear in required. A nullable required field gives you that and still lets the model say "not stated".
Keep the structure simple
Models fill in flat, small schemas more reliably than large, deeply nested ones. Constrained decoding also has limits: providers cap nesting depth, property count, and supported keywords.
| Choice | Prefer | Avoid |
|---|---|---|
| Nesting | Two or three levels | Deep trees of objects |
| Alternatives | One object with a `type` enum | Many `anyOf` branches |
| Free-form data | A list of small objects | Objects with arbitrary keys |
| Size | Several small calls | One schema with dozens of fields |
If an extraction needs many fields, split it into two calls, each with a focused schema, and combine the results in code.
{
"type": "object",
"properties": {
"category": {
"type": "string",
"enum": ["billing", "bug", "feature_request", "other"]
}
},
"required": ["category"],
"additionalProperties": false
}Setting additionalProperties to false forbids extra keys. Many constrained-decoding providers require it on every object.
Common mistakes
- Enum values that overlap, such as
"bug"and"error". The model cannot tell them apart, and neither can you. - Descriptions that repeat the field name, such as "The priority" for
priority. - Required fields for information the input may not contain.
- Changing the schema without re-running your test examples.
Exercise
A model extracts contract_end_date from emails. Many emails never mention an end date, and the model keeps returning made-up dates. Give two schema changes that reduce this.
Show answer
Make the field nullable (str | None) while keeping it in required, so "not stated" is a valid answer. Add a description such as "End date as YYYY-MM-DD, or null if the email does not state one". Both give the model an honest option instead of forcing a guess.
Exercise
Your schema has status: Literal["open", "closed"], but tickets sometimes describe a case that is neither, and the model picks "open" every time. What would you change?
Show answer
Add a value such as "unknown" or "other" so the model has a valid choice when nothing fits, and describe when to use it. Then check the examples that previously failed.