Structured output
When code consumes the answer, ask for a schema and validate it. Do not parse prose.
Words on this page (9)
- schema
- : A description of the shape of data: which fields exist and what types they have.
- structured output
- : Model output that follows a schema, such as valid JSON with fixed fields.
- JSON
- : A text format for structured data: keys and values in braces, like {"name": "Ada"}.
- tool use
- : The model asks your program to run a function and reads the result.
- tool
- : A function the model can ask your program to run, described by a name, description and argument schema.
- skill
- : A folder of instructions and scripts an agent loads only when a task needs it.
- prompt
- : The input text you give a model, including instructions, examples and data.
- logarithm
- : The power you raise a base to in order to get a number. log base 2 of 8 is 3 because 2 to the power 3 is 8.
- token
- : A piece of text the model reads and writes, usually a word or part of a word. About 4 characters of English on average.
What it is
means the model returns that matches a you define. Providers offer this through schema-constrained generation or through whose arguments are the schema. Libraries add a validation step and retry the call with the error message when validation fails.
Why it matters
Free text breaks parsers in small ways: a trailing explanation, a missing field, a changed key name. A schema makes the contract explicit. Validation turns silent bad data into a loud error you can count and fix. Keep schemas small, give fields descriptions (the model reads them), and use enums instead of free strings when the set of values is known.
A worked example: a schema
Schema: name (string), years_experience (number), (list of strings), seniority (one of junior, mid, senior). : "Extract these fields from the resume." The model returns {"name": "Maya Lin", "years_experience": 5, "skills": ["Python", "SQL"], "seniority": "mid"}. Your code validates it. If seniority came back as "intermediate", validation fails and you retry with the error message.
Tips
Use enums for fixed choices. Make fields optional when the information may be missing, so the model is not pushed to invent a value. Describe each field in one sentence. every validation failure and read them weekly.
How the constraint works
With schema-constrained generation, the system filters the model's choices at every . The model scores all possible next tokens, and the system removes those that would break the schema. After {"years": only digits are allowed. After "seniority": only the start of junior, mid or senior is allowed. The model picks among what remains. The result always has a valid shape. It is not guaranteed to hold the right value.
Valid is not correct
Suppose the resume says 15 years and the model returns 5. The JSON is valid, validation passes and the data is wrong. Another case: a resume with no seniority stated, and the model picks "mid" because the schema forced a choice. The symptom is no errors but bad data. To confirm, sample 20 outputs, compare each field with the source by hand, and compute accuracy per field.
Trade-offs in schema design
A schema that is too strict forces an invented answer. Add an "unknown" option to enums and make uncertain fields optional. Deeply nested schemas fail more often, so flatten them when you can. If you ask for only a final answer, you may lose the benefit of thinking first. A common fix is a "reasoning" field placed before the "answer" field, so the model writes its working before it commits.
Try it yourself: five messy emails
Collect five event emails. Write a schema with four fields: name, date, place and a type chosen from a fixed list. Ask a chat model to fill it for each email and check the JSON in any free online validator. Count how many are valid and how many are correct. Expect most to be valid and a few wrong, usually dates written in an unusual way.
Common mistake
Asking for JSON in prose and then parsing with string tricks. Use the provider's structured output feature or a validation library.
A recruiting tool reads resumes and fills a database. If the model replies "Sure! The candidate has 5 years of experience", the code that expects a number crashes. If it must reply with JSON that fits a schema, such as {"years": 5}, the code works every time and bad answers are caught immediately.
Check yourself
Output fails schema validation. What should you do?
Not quite. Feeding the error back usually fixes it, and counting failures shows whether the prompt or schema needs work.
Why add a description to each schema field?
Not quite. Field names and descriptions are part of the prompt the model sees.
A resume has no years of experience listed. How should the schema treat that field?
Not quite. Optional fields avoid forcing the model to guess.
You got 0 of 3 right.
Go deeper
- Repo
Jason Liu · The most used library for validated structured outputs with retries.
- Repo
Pydantic · Typed agent framework built around validated outputs.
- Repo
.txt · Constrains generation so output always matches a schema or grammar.
Spotted a mistake or something unclear? Report it on GitHub.