How to Get Reliable Structured Output from LLMs
when AI extracts data for your systems, it must return it in a fixed format every time. Modern AI models can now guarantee the format. They can’t guarantee the values are right, so a reliable pipeline adds checks, confidence scores and human review on top.
The short answer: define a strict schema for every output, use the model’s native structured-output mode so the response always matches it, then validate the values with business rules, retry or repair what fails, mark missing values explicitly instead of letting the model guess, and add a confidence field so unsure records go to a person. Test the whole thing on a golden set field by field.
LLM structured output is the step inside every extraction pipeline where text becomes data. Our guide to extracting data from large, complex documents covers the pipeline around it; this post covers the step itself. It sits in our Agent-Ready Data work.
Why does free-form JSON break in production?
Ask a model to “reply in JSON” and most replies will parse. At volume, some won’t: a trailing comma, a missing field, a number written as text, an extra sentence before the brace. Each one is a failed record or, worse, a silently wrong one. Prompting alone can’t remove that tail.
Step 1: Design the schema first
A schema is the fixed list of fields you want, with their types and rules. Design it before writing any prompt:
| Field | Type | Rule |
|---|---|---|
| invoice_number | string | Required |
| invoice_date | date | ISO format, not in the future |
| total_amount | number | Required, two decimals |
| currency | enum | One of your supported codes |
| source_page | integer | Page the value came from |
| confidence | enum | high, medium or low |
Keep it flat where you can, use enums for closed lists, and give every field a short description: the model reads them.
Step 2: Use native structured-output modes
The major model providers now offer modes that constrain the response to your JSON schema, so the output always parses and every required field is present. Anthropic, for example, made structured outputs generally available on its platform and in Amazon Bedrock in February 2026. Two things to know:
- Each provider supports a subset of JSON Schema. Very complex schemas can be rejected, so keep them simple.
- The format is guaranteed, the content is not. A perfectly valid record can still hold a wrong date. That’s what the next steps are for.
Step 3: Validate values with business rules
Run plain checks after the model:
- dates are real and in range;
- totals equal the sum of line items;
- IDs match their format or check digit;
- values agree with other records, such as the purchase order or another document;
- the quoted source text actually appears on the cited page.
Step 4: Retry, repair or route
When a check fails, send the record back to the model once with the specific error (“total does not equal sum of lines”) and the relevant text. If it fails again, route it to a person. Never loop retries without a limit.
Step 5: Handle missing values honestly
The most common silent error is an invented value for a field that isn’t in the document. Prevent it:
- make every field nullable and say in its description “null if not stated”;
- distinguish not present from unreadable: they need different follow-ups;
- treat a value with no source page as suspect.
Step 6: Add confidence and route by risk
Ask for a confidence value per field, combine it with your rule results, and set thresholds per field by risk. A wrong description is a nuisance; a wrong bank account number is expensive.
How do you test structured output?
Keep a golden set of real documents with checked values, and track per field: exact accuracy, missing values, invented values, schema failures and review rate. Re-run it after every model, prompt or schema change. See how to test AI agents. Spreadsheets need extra handling before this step; see How to Make AI Agents Read Messy Spreadsheets.
Common mistakes
- Relying on “reply in JSON” in the prompt.
- Treating schema-valid as correct.
- Required fields with no way to say “not stated”.
- Unlimited retries.
- Measuring parse rate instead of field accuracy.
Product facts checked in November 2026.
How SHVX can help
Getting broken or wrong JSON in production? We’ll review your schema, validation and routing, and show you where wrong values slip through.
Learn more about Agent-Ready Data.
FAQ
How do I get an LLM to return valid JSON every time?
Use the provider’s native structured-output mode with a JSON schema. It guarantees the format; add validation for the values.
Does structured output stop hallucinations?
No. It guarantees shape, not truth. Validation rules, source citations and nullable fields reduce invented values.
What should an extraction schema include?
Each field’s type and rule, a source page, a confidence value, and a way to mark values as not present or unreadable.
What is the best way to extract structured data from documents with AI?
Structure the document first, extract section by section into a strict schema, validate with business rules and route low-confidence records to people.