Skip to content
← Notebook

Agents & AI

Structured outputs with real outputs: what mistakes LLMs make when returning JSON and when to repair or retry

1200 real outputs from Llama, Qwen and Gemma: repairing locally avoids between 0% and 100% of retries depending on the model. With the real mix, 59.6% of the extra spend.

Date
Oct 20, 2026
Reading time
9 min
Result
1200 real outputs • 59.6% saved with the real mix • From 0% to 76% correct orders with constrained output on Llama 3.2

Translated from the Spanish original. The code, data and READMEs on GitHub are in Spanish.

With real outputs, what you save by repairing the JSON locally instead of retrying is almost exactly the percentage of your failures that are syntax errors. In 600 free-form outputs from four open models, repairing avoided 100% of retries on Gemma 3 and Qwen 2.5, 58.7% on Llama 3.1 and only 1.3% on Llama 3.2, which returned the schema instead of data. With the real mix of errors, the saving on extra spend was 59.6%. And schema-constrained generation eliminated every syntax failure and took Llama 3.2 from 0% to 76% correct orders.

This is part two of repairing the JSON locally or retrying, which used synthetic failures split evenly. On X we were rightly asked what happens with real outputs and whether one type of error appears much more than the others. Here’s the measured answer.

Setup

  • 4 open models running locally with Ollama: Llama 3.2 (3B), Qwen 2.5 (3B), Gemma 3 (4B) and Llama 3.1 (8B).
  • Task: extract the order (customer, lines with SKU and quantity, delivery, urgency, notes) from 30 emails in Spanish, with 5 seeds per email and model, at temperature 0.7.
  • Free-form generation: we ask for “only a valid JSON” with the schema in the instructions and save each output as is.
  • Repair first: json_repair and validation against the schema. If it isn’t valid, a real retry with the model, measuring its tokens and its time.
  • Constrained generation: the same email with the JSON Schema in Ollama’s format parameter, which constrains generation with a grammar (the same principle as Outlines).
  • Correct order: email, company, city, date, urgency and all lines (SKU and quantity) match the original email.

What errors really show up

Model Valid untouched Failures Dominant error
Gemma 3 (4B) 0 150 JSON inside a markdown block (150)
Qwen 2.5 (3B) 83 67 Closes the list of lines with } instead of ] (67)
Llama 3.1 (8B) 58 92 Text around the JSON (55) and refusals to answer (32)
Llama 3.2 (3B) 1 149 Returns the schema instead of the data (145)

No output was cut off by the token limit at this response size. The errors that can’t be repaired were others:

  • Copied schema: the model returns the JSON Schema, or the schema and the data mixed into another structure. It’s valid JSON, but useless.
  • Refusals: Llama 3.1 answered 32 times «Lo siento, pero no puedo cumplir con esa solicitud» (“I’m sorry, but I can’t comply with that request”), probably because the emails contain names and email addresses.

What gets repaired locally

Type of failure Cases Repaired locally
Markdown block 151 151
Wrong closing bracket 67 67
Text around the JSON 58 55
No JSON (refusals) 32 0
Copied schema 147 0
Valid JSON that doesn’t match the schema 3 0

Repair first vs always retry

Model Retries avoided Tokens per 1000 calls, always retrying Tokens per 1000 calls, repairing first Savings
Gemma 3 100% 2,132,127 922,687 56.7%
Qwen 2.5 100% 1,325,753 806,880 39.1%
Llama 3.1 58.7% 1,319,813 950,607 28.0%
Llama 3.2 1.3% 2,190,933 2,173,353 0.8%

“Always retrying” is the usual retry pattern in Pydantic, Instructor or n8n’s automatic format correction: any output that doesn’t validate as is gets requested again.

The challenge question: what if one type of error dominates?

With the measured repair rate for each type, the saving on the extra spend from retries depends on which one dominates:

If all your failures were… Savings from repairing first
Markdown blocks 100%
Wrong closing brackets 100%
Text around the JSON 94.8%
This experiment’s real mix 59.6%
Copied schema 0%
Refusals with no JSON 0%

Put another way: the saving is your percentage of syntax failures. If your model fails on format, repairing locally eliminates almost all the extra spend. If it fails on content (copies the schema, refuses, puts data where it doesn’t belong), repairing doesn’t help and you have to retry or, better, constrain generation. And if a response is cut off by the token limit, as we saw in part one, data is missing: you retry then too.

Schema-constrained generation

Model Correct orders, free and repairing Correct orders, constrained Time per call, free Time per call, constrained
Gemma 3 96.7% 96.7% 10,087.4 ms 8,415 ms
Qwen 2.5 85.3% 85.3% 7,018.9 ms 6,100.1 ms
Llama 3.1 64.7% 84.7% 6,977.5 ms 7,011 ms
Llama 3.2 0% 76% 6,416.8 ms 4,456.3 ms

All 600 constrained outputs were valid JSON and matched the schema. There are no syntax failures to repair and no retries, and on these models it wasn’t slower. What it doesn’t fix is content: if the model gets the city or the quantity wrong, it gets it wrong just the same inside a perfect JSON.

What it means

  • Measure your error mix before deciding. With a model that fails on format, json_repair (or a parser like BAML’s) saves you practically every retry. With one that fails on content, it saves you nothing.
  • If you control inference or your provider offers it, use schema-constrained output. It removes syntax errors at the root and, on small models, greatly improves content.
  • Always validate against the schema and compare with what’s expected. Valid JSON isn’t a correct order: Gemma 3 and Qwen 2.5 stayed at 96.7% and 85.3% correct orders even with perfect JSON.
  • Watch out for models that refuse. If your data contains names and email addresses, some models refuse to process it; that can’t be repaired, you change the model or the instructions.

Limitations

  • Small open models running locally. Large commercial models fail much less on format; the error mix will be different, and that’s exactly what this experiment teaches you to measure.
  • A single type of task and 30 synthetic emails in Spanish, with 5 seeds each at temperature 0.7.
  • Cost of retries that weren’t needed: on Gemma 3 and Qwen 2.5 everything was repaired and there were no real retries, so the cost of “always retrying” is estimated with the same formula (resend the email, the broken output and the error, and generate again). In the 185 real retries of the two Llamas, the real cost came in between 0.941 and 1.287 of that estimate (median per model), so the real saving on Gemma and Qwen could be somewhat higher.
  • Timings from a single laptop with other applications open.
  • BAML and Outlines weren’t measured as libraries; constrained output uses Ollama’s grammar, which applies the same principle.

How to reproduce it

Code, environment, the 1200 raw outputs and the analysis: yeswelab-laboratorio/structured-outputs-salidas-reales-errores-json-llm on GitHub. If you repeat it with other models, open an issue with your error mix.

Want to know your agents’ error mix and what it costs you? Commission the experiment.

#Ollama 0.40.5#Llama 3.1#Llama 3.2#Qwen 2.5#Gemma 3#json-repair#JSON Schema