JSON mode
The model is constrained to emit parseable JSON. It decides the keys and types itself, guided only by the prompt.
- Always parses
- Keys and types can drift
- Required fields can be missing
- You validate the shape
A model can be forced to return valid JSON. Whether the values in it are right, and where they came from, is a separate job — and the one that decides if anyone can trust the table.
This guide explains the four levels of output control, how constrained decoding guarantees a schema, what the major model providers enforce and what they leave to you, how to design an extraction schema, the five layers of validation, and how to make every value point back to the words it came from.
Updated · 7 sources
Structured data extraction is reading named, typed values out of unstructured content — dates, amounts, parties, line items — and returning them as records that match a schema. With LLMs, you give the model a JSON Schema and the document; structured outputs (constrained decoding) guarantee the response fits the schema, and your own validation and citations establish that each value is correct and traceable to its source.
The two are often confused. OpenAI’s guide puts it plainly: both produce valid JSON, but only Structured Outputs ensures schema adherence.
The model is constrained to emit parseable JSON. It decides the keys and types itself, guided only by the prompt.
The model is constrained to emit JSON that matches a supplied JSON Schema — required keys, types and enums included.
A model writes one token at a time. Constrained decoding turns the schema into a grammar and, at every step, removes the tokens that would break it — so the only outputs possible are valid ones.
Fields, types, enums and required keys, written as JSON Schema — usually generated from a typed model in code.
The provider turns the schema into a grammar. Anthropic notes extra latency on first use; compiled grammars are cached.
While generating, tokens the grammar does not allow next are excluded from sampling.
The response parses and fits the schema — unless the model refused or ran out of output tokens.
The grammar knows shapes, not facts. A date in the right format can still be the wrong date.
{
"type": "object",
"properties": {
"invoice_date": {
"type": ["string", "null"],
"description": "Issue date, YYYY-MM-DD. Not the due date."
},
"total_due": {
"type": ["number", "null"],
"description": "Amount payable including tax."
},
"currency": { "type": "string", "enum": ["USD", "EUR", "GBP", "OTHER"] },
"evidence": {
"type": "array",
"items": {
"type": "object",
"properties": {
"field": { "type": "string" },
"quote": { "type": "string" },
"page": { "type": "integer" }
},
"required": ["field", "quote", "page"],
"additionalProperties": false
}
}
},
"required": ["invoice_date", "total_due", "currency", "evidence"],
"additionalProperties": false
}The schema is both the contract your database enforces and the instruction the model reads. Most extraction errors trace back to a field that was under-described or forced to have a value.
Format can cost accuracy. Tam et al. (2024) found stricter format constraints tended to lower accuracy on reasoning tasks, while helping on classification, and that the order of keys mattered. Where a field needs reasoning — “is this clause a termination right?” — give the model a field to reason in before the answer, or extract in two passes.
Constrained decoding closes the first layer. The next four are your code and your reference data, and the last is a person; together they catch the errors that matter to the business.
Which values may skip the last layer is a measured decision: see straight-through processing and confidence calibration.
A value without a source cannot be audited. There are three ways to get one, and they suit different jobs.
What are you producing?
A single “accuracy” number hides the failures that cost money. Measure at the level the business acts on.
Count extracted-and-wrong apart from missed. A nullable field that is always null scores high on precision and zero on recall.
Hundreds of labelled documents that mirror production, scored on every prompt or model change. See what is a golden dataset.
A score that means “right nine times in ten” decides which values a person must see.
On Botminds the schema is not code in a repository. It is the collection’s labels — and each label’s description is the instruction the extraction agent follows.
An intake agent receives the collection’s fields as its output schema, so one definition drives extraction, review screens, views and the API. Read AI data modeling and the Academy lesson on taxonomy, lifecycle, tags and events.
Write a JSON Schema for the fields you need, with a clear description for each. Send the document and the schema with structured outputs switched on, then validate the values in code, verify each value’s quoted evidence, and send failures or low-confidence values to a person.
JSON mode guarantees the response is valid JSON. Structured outputs guarantee it also matches your JSON Schema — required keys, types and enums. Neither guarantees the values are correct.
No. They constrain shape, not truth. A model can return a correctly formatted date that is the wrong date. Evidence fields, validation rules and review catch those.
OpenAI, Anthropic and Google all offer schema-constrained output in their APIs, each with its own set of enforced JSON Schema keywords. Check each provider’s documentation for the models and keywords it covers.
Add a quote and page number for each field to the schema, then check in code that the quote appears on that page and supports the value. Provider citation features suit prose answers better than schema-bound extraction.
Both are common: define the record as a typed model, generate the JSON Schema from it, and validate the response against the same model. That keeps the schema, the parser and your code in one definition.
It depends on the documents, the schema and the model, so measure it on a labelled golden set of your own documents — per field, and per document with every field right — before deciding what can skip review.
Usually the schema. A required non-null field pushes the model to invent a value; a vague description makes it pick the wrong one. Allow null where a value may be absent and describe what each field is not.
Bring one process and its documents. We’ll show you the agents, the controls and the record, running on your data.