25.7 Structured outputs: a schema on the response
- Approved by
- Panaversity ·
In everyday life. A clinic's registration form has a box for your date of birth. It has a list of blood groups to tick, with "not known" at the end. It has a line for a second phone number, marked "leave empty if none". You cannot write a paragraph in a tick list. You can still tick the wrong blood group. The form holds the shape of your answer and knows nothing about whether it is true.
What you will learn. You practice this in Lab 25, Task 3.
- What is a structured output?
- Why does the list end with "other"?
- How does the model say "not on the invoice"?
- What does a schema on the response cost?
- What can the schema still not do?
Why it matters. A person coped with any layout. Code cannot, so something must hold the form before the code reads it.
At work. With the red case fixed (25.6), the builder turns to INV-8340 and its "Net 30 days" (the overview). Lab 24's check refused that answer after it was written. The builder wants an answer that cannot be written that way at all.
What is a structured output?
A structured output is an answer the API holds to a JSON Schema that you send with the request. The builder could add the five terms to the prompt, and the model could still miss them (25.3). A schema sent this way is enforced while the answer is being written. At each step the model can only write what the schema allows next. The prep course's name for this is constrained decoding.1
On the Messages API the schema goes in output_config.format:2
{
"model": "claude-sonnet-5-5",
"max_tokens": 1024,
"output_config": {
"format": {
"type": "json_schema",
"schema": {
"type": "object",
"properties": {
"number": { "type": "string" },
"vendorId": { "type": "string" },
"invoiceDate": { "type": "string", "format": "date" },
"terms": {
"type": "string",
"enum": ["Due on receipt", "Net 15", "Net 30", "Net 45", "Net 60", "other"]
},
"poNumber": { "type": ["string", "null"] },
"subtotalCents": { "type": "integer" },
"taxCents": { "type": "integer" },
"amountCents": { "type": "integer" }
},
"required": ["number", "vendorId", "invoiceDate", "terms", "poNumber", "subtotalCents", "taxCents", "amountCents"],
"additionalProperties": false
}
}
}
}The answer comes back as JSON in the response's text block.2 "additionalProperties": false is required, and it means no other field may appear. With terms as an enum, a fixed list of allowed values, "Net 30 days" can no longer be written at all. This is 14.5's rule, "one of a fixed set". What changed is who holds it. Now the API holds the shape while the answer is written. Structured outputs are not offered in Managed Agents, which is why the extraction is a Messages API call from the portal's code.2
Ana labels INV-8340 as a new case, so the suite holds 42. The whole suite runs, and the new case passes as Net 30.
Why does the list end with "other"?
Because a form with no honest answer forces a dishonest one. Suppose an invoice prints terms that are not among the five. With only five values allowed, the model must pick one of them. The answer has the right shape and a wrong value, and the schema made it so.
"other" lets the model say "this is not one of yours" inside the form. If the model picks "other", the failure moves from shape to value (25.9).
How does the model say "not on the invoice"?
With a nullable field. A nullable field may hold null, which means "no value". Some invoices print no purchase order number. If poNumber must be a string, the model has to write something, and an invented PO number looks exactly like a real one. Written as "type": ["string", "null"] and still listed as required, the field must always be there, and the model can fill it with null. This is the same pattern as 21.2's tool inputs.
The person who read the answer could tell a blank from a guess. "other" and null are how the schema lets the model tell your code which one it is.
What does a schema on the response cost?
Four small costs, and one large one. The prep course names the four.1
- A slower first call. The first request with a new schema takes longer, because the API first compiles the schema, which means it turns it into rules it can enforce.
- A cache with a clock. The compiled schema is kept for 24 hours from its last use.2
- Slightly more input tokens. The API adds a system prompt of its own that describes the format.
- No prefill (25.3). It cannot be combined with structured outputs.2
There is a fifth cost, and it is the largest. The schema you send can hold less than your own: 25.9 says what is left out.
Asking for JSON in the prompt and checking it in code is still fine where no enforced schema is offered. Lab 23's morning report, written inside a hosted session, does exactly that.
What can the schema still not do?
Two things: it cannot make every answer match, and it cannot make any answer true.
It does not make every answer match. A decline or a cut answer may not fit the schema, so the code reads the stop reason first (25.10).2
It holds the shape, never the truth (25.9). The red case had every field in place and a wrong total, and a schema passes it.
The operating rule: let the API hold the shape. Give every fixed list an "other", and make every field that can be missing nullable. The model can then say "not on the invoice" inside the form. Last, check the values yourself.
Do this now. Ask your coding agent to draft the schema before any request is sent. Copy this line:
Write the JSON Schema for the invoice extraction as the API will hold it. Make the terms an enum of our five terms plus "other". Make every field that an invoice may leave out a string or null, and keep it required. Close every object to extra fields. Then list each rule in our code's own invoice schema that you left out, and why. Send no request.
Check: the list of rules left out should include the number range on the amount. Those are the rules your code must still check.
Check yourself
Question 1 / 7
0 answered
How does a schema sent as a structured output differ from listing the five terms in the prompt?
Sources
- Owner
- Panaversity