Problem · 14 studies
How to get reliable JSON and structured output from an LLM
LLMs break structured output when a format instruction competes with reasoning, when schema fields are ambiguous, and when a closing format requirement fades by the end of a long answer. The fixes that measured best separate reasoning from formatting and turn the schema into explicit instructions with a self-check.
What unreliable structured output looks like
Structured output fails in two ways, and the second is easier to miss.
- The structure itself breaks. Unclosed brackets, stray text around the JSON, missing required fields, the same field filled in different formats from call to call, or a closing JSON block that never appears. A study of terminal format requirements saw up to 50% violations of "end with a JSON block"-type instructions on hard tasks.
- The JSON is valid but the content got worse. Asking for a format in the same request as the reasoning lowers answer quality. The Format Tax study found structured answers were on average 162 tokens shorter than free-form ones, with the lost reasoning showing up as errors.
- The model reads JSON badly, too. When the input is a large API response, a JSON-processing benchmark found GPT-4o topped out at 77% accuracy reading it directly.
Why format instructions fail
Three mechanisms recur across the papers.
Format competes with thinking. The model generates token by token and keeps the format requirement active from the first token. The Format Tax authors separated the prompt from the decoding mechanism and found the instruction itself caused most of the damage: 92% of the significant degradation came from the prompt. A compliance-agent case study describes the same conflict in agent loops, worst on weaker models.
Schemas written for programmers are ambiguous to models. A field typed only as "string" leaves the model to guess whether a price includes the currency symbol or whether a car model includes the trim. Constrained decoding guarantees valid syntax, not the right values, as the PARSE paper shows.
Deferred commitments fade. "Finish with JSON" has to be held in mind across the whole answer. In the salience study, o4-mini followed one format requirement 99% of the time and all five only 47.5% of the time, and its math accuracy fell from 93% to 27% with active format requirements.
Fixes ranked by measured effect
Ordered by the size of the measured effect, on the authors' models and data.
- Write the schema as instructions: descriptions, patterns, allowed values, then a self-check. PARSE adds a description and regex pattern to each field and has the model check completeness, grounding in the source and pattern compliance. On HTML extraction (SWDE), accuracy rose from 24.5% to 93.14%, and structured error feedback fixed 92% of errors on the first iteration. Schema-guided extraction used the same levers for nested lists without examples and got over 90% accuracy on 12 of 14 fields with Claude Opus 3.
- Ask for code instead of reading JSON. For filtering or aggregating a large response, have the model write a Python function and run it. The JSON-processing study reports up to 95% accuracy this way, and adding the JSON schema to the prompt added 12%.
- Reason first, format second. Get the analysis in free text, then convert it. Two-stage prompting reports up to 4x improvement in task success in an agent setting; the Format Tax study measured +6.8 pp accuracy on average for two requests. REAP applies the split to recall: draft the facts in slices, format at the end.
- Let the model choose tools in plain language. Natural Language Tools replaces JSON tool calls with "Tool – YES/NO" lines parsed by code; accuracy went from 69.1% to 87.5%.
- Frame the format requirement at both ends. An "IMPORTANT FORMAT REQUIREMENT" tag at the start and a reminder at the end raised compliance from 70–87% to 90–100% in the salience study. With several requirements, list them in the reminder.
- Box the model in. Exact field names, fixed order and explicit bans ("do not add any steps beyond those listed"). In the hyper-specific prompt study, GPT-4o produced an exactly correct YAML config in only 3 cases out of 100 from ordinary prompts. One filled-in example of text-to-JSON also helps, per the schema-plus-examples study.
- Edit with patches, not full regeneration. For large documents, ask for RFC 6902 add/remove/replace operations and apply them in code, as in JSON Whisperer; replace list indices with keyed entries so the model does not miscount positions.
A combined prompt stack
A pipeline that combines the strongest pieces:
- Request 1, no format: the task, answered in detail, with no mention of JSON (Format Tax).
- Request 2, format only: "take the answer above and format it as JSON", with a schema whose fields carry a description, an allowed-value list or pattern, and nesting written out.
- Self-check inside request 2: are all required fields filled, do the values appear in the source, do they match the patterns; if not, state the error and fix it (PARSE).
- Bracket the requirement: the format tag first, a specific reminder last (Salience Reminder).
- Downstream: validate with code, edit with patches, and process large JSON with generated code rather than by reading it.
In a long chat, the split also saves output budget: the output-stalling study estimates JSON markup adds +15% to the volume of an answer.
What does not work
- "Analyze and return JSON" in one request for anything that needs reasoning. This is the pattern every paper above was built to replace.
- Relying on constrained decoding or JSON mode alone. It fixes syntax, not meaning; ambiguous fields still get guessed values (PARSE).
- A generic "follow all requirements" with many constraints. Compliance still falls as requirements stack; name them in the reminder or split them across requests (salience study).
- Pasting huge JSON and asking questions about it. Mistral-large dropped by 91% as inputs grew to 80K tokens in the JSON-processing study.
- Over-tuning the schema. PARSE accuracy dropped after the 6th optimization iteration.
The size of the format tax differs by model. The Format Tax study found Claude Haiku 4.5, newer GPT-4o and Grok barely suffer and thinking modes remove it, while open models are hit hard; the two-stage split matters most for weaker and open models.
Fixes ranked by measured effect
| Study | Lab score | Finding | Effect |
|---|---|---|---|
| PARSE: Automatic JSON Schema Optimization for Accurate Data Extraction from TextOct 8, 2025 | 90 | Schemas rewritten with field descriptions and patterns plus a three-check extraction raised HTML extraction accuracy from 24.5% to 93.14%; structured feedback fixed 92% of errors on the first iteration. | Helps |
| Processing JSON with LLMs: When Code Beats WordsOct 10, 2025 | 89 | Writing a Python function to process JSON reached up to 95% accuracy where GPT-4o reading directly topped out at 77%; adding the schema gave +12%. | Helps |
| NLT (Natural Language Tools): Calling Tools in Plain Language Instead of JSONOct 16, 2025 | 91 | Selecting tools in natural language instead of JSON raised accuracy from 69.1% to 87.5%. | Helps |
| Two-stage prompting: think first, format second, for up to 4x fewer JSON errorsJul 8, 2025 | 80 | Splitting reasoning and structured output into two stages raised task success by up to 4x in an agent case study. | Helps |
| The Format Tax: Separate Reasoning from Formatting to Get Answer Quality BackApr 4, 2026 | 86 | The format instruction itself, not the decoding mechanism, drove most degradation; reasoning first and reformatting second gave +6.8 pp accuracy on average. | Helps |
| Salience Reminder: How to Keep a Model from "Forgetting" Format Requirements Under LoadMar 7, 2026 | 93 | Tagging the format requirement at the start and reminding at the end raised compliance from 70–87% to 90–100%. | Helps |
| Schema-Guided Extraction: A Blueprint Instead of a Request for Pulling Out Complex DataAug 6, 2026 | 76 | A schema with field descriptions, closed value lists and explicit nesting gave over 90% accuracy on 12 of 14 fields with no examples. | Helps |
| The Hyper-Specific Prompt: Corner the Model and It Stops Making Things UpJul 23, 2025 | 74 | With ordinary prompts GPT-4o generated an exactly correct YAML config in 3 of 100 cases; exhaustive detail and explicit bans close the gap. | Helps |
| JSON Whisperer: Diff Patches Instead of Regenerating the Whole JSONOct 6, 2025 | 82 | Generating RFC 6902 patches instead of regenerating a whole document avoids data loss; keyed entries replace error-prone list indices. | Helps |
| REAP: Separate Recalling a Fact From Formatting the Answer So the LLM Doesn't Get Confused and HallucinateAug 11, 2026 | 84 | Recalling facts in free text and formatting as a separate final step returns more complete lists than asking for JSON directly. | Helps |
| Two-Pass Generation: How to Rescue Text Quality from Formatting ConstraintsApr 14, 2026 | 91 | A formatting constraint makes the model plan a shorter answer; generating freely and rewriting under the constraint recovers 60–96% of the volume. | Helps |
| Two-Step Generation (OGC + Format-Cost Separation): How to Get a Long Document from an LLM When the Conversation Is Already LongApr 17, 2026 | 88 | JSON markup adds +15% to output volume; writing plain text first and formatting second helps when a long chat has shrunk the output budget. | Helps |
| Schema + examples: how to pull structure out of any text with no wasted wordsJul 7, 2025 | 77 | A schema plus one or two filled-in text-to-JSON examples makes extraction more predictable than instructions alone. | Helps |
| How You Structure Questions in a Prompt: Battery Saves 6x the Tokens, but Recency Bias Shifts Answers 20xDec 9, 2025 | 73 | Asking several questions in one JSON prompt saves 2-6x tokens, but the last answer option was chosen 20 times more often than the first on Llama-3.1-8B. | Mixed |
Prompts to copy
Reason first, format second (one request)
Task: {task}
Produce two blocks:
BLOCK 1 — ANALYSIS:
Work through the task in detail. Think out loud, give arguments, don't cut corners.
BLOCK 2 — RESULT IN FORMAT:
Take the conclusions from block 1 and format them strictly as {format}:
{format_schema}
From: The Format Tax: Separate Reasoning from Formatting to Get Answer Quality Back
Schema extraction with self-check
Extract {data_description} from the text according to the optimized schema.
Text:
{input_text}
Optimized schema:
{json_schema_with_descriptions_and_patterns}
Perform the extraction with checks:
1. Are all required fields filled?
2. Do the extracted values appear in the source text?
3. Do the values match the schema's patterns and rules?
If a check fails, state the specific error and fix it.
From: PARSE: Automatic JSON Schema Optimization for Accurate Data Extraction from Text
Format requirement reminder
IMPORTANT FORMAT REQUIREMENT: {format_requirement}
{main_task}
{additional_instructions_if_needed}
Make sure to follow ALL the format requirements stated above.
From: Salience Reminder: How to Keep a Model from "Forgetting" Format Requirements Under Load
Techniques that help
Questions people ask
Why does asking for JSON make the answer worse?
The format instruction competes with the task from the first token, and the model compresses its reasoning to fit the structure. The Format Tax study traced most of the loss to the instruction itself, not to constrained decoding. Reasoning in free text first and formatting in a second step recovers most of it.
Is JSON mode or constrained decoding enough?
It guarantees valid syntax, not correct values. Ambiguous fields still get guessed formats and boundaries. Add a description, allowed values or a pattern to each field, and have the model check completeness, grounding and format before returning.
Should I always split reasoning and formatting into two requests?
Not always. The gain is largest on open and weaker models and on reasoning-heavy tasks. Thinking modes and some newer closed models barely pay the format tax, and simple lists or short answers do not need the split.
How do I get answers out of a large JSON response?
Ask the model to write a function that filters or aggregates the data, give it the schema or a trimmed sample, and run the code. Reading large JSON directly loses accuracy as size and nesting grow.