Skip to content
← Notebook

Agents & AI

Structured outputs in AI agents: repair the JSON locally or retry (BAML, Outlines and retries)

Measured on 1000 broken JSON outputs: a tolerant parser repairs 100% in 0.12 ms and leaves 83.3% identical to the original. A retry costs 137.3% of the original call in tokens.

Date
Oct 20, 2026
Reading time
7 min
Result
100% repaired locally • 0.12 ms per repair • One retry = 137.3% of the call

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

Before asking the model to repeat a broken JSON, repair it locally: a tolerant parser fixed 100% of 1000 broken outputs in a median of 0.12 ms and left 83.3% identical to the original. A retry, on the other hand, costs 137.3% of the original call in tokens. The only exception is a response cut off by the token limit: data is missing there, and you do need to retry.

Measured on 20 October 2026 on a MacBook Pro M1 Max with Python and json_repair.

Part two, with real outputs: we repeated the test with 1200 outputs from Llama, Qwen and Gemma to see which errors really show up and what happens when one type dominates. Read the results.

The problem

When an agent returns JSON, sometimes it returns it wrong: an extra comma, single quotes, the JSON inside a markdown block, a comment, or a truncated response. The usual fix is the retry loop: max_retries in Pydantic and Instructor, or the option to auto-fix the format in n8n. It works, but every failure is another full call to the model, with its cost and its wait.

There are three alternatives:

  • Repair on the client with a tolerant parser (what we measure here with json_repair; BAML does something similar with its Schema-Aligned Parsing, which is also guided by the schema).
  • Constrain generation so the model can’t produce invalid JSON (constrained decoding, like Outlines with state machines on vLLM, or providers’ structured output modes).
  • Retry only when the above isn’t enough.

Setup

  • 1000 outputs from an agent that extracts orders (customer, lines with SKU, quantity and price, delivery, notes), generated with a fixed seed.
  • Six failures, split evenly: trailing comma, markdown block with a sentence, single quotes, truncated response, unquoted keys and a comment.
  • We measure: whether json.loads accepts it, whether json_repair turns it into a valid object, whether it ends up identical to the original and how long it takes (5 repetitions).
  • Cost of a retry: tokens to resend instructions, schema, broken output and error message, plus those of the new output, with GPT-4o’s o200k_base tokenizer.

Results

Failure Cases Repaired and identical to the original
Trailing comma 167 167
Markdown block with a sentence 167 167
Single quotes 167 167
Unquoted keys 166 166
Comment 166 166
Truncated response 167 0
Measure Result
Accepted by json.loads without repair 0
Repaired into valid JSON 1000 (100%)
Identical to the original 833 (83.3%)
Time per repair, median 0.122 ms
Time per repair, P95 0.425 ms
Tokens in a normal call, median 550.5
Tokens in a retry, median 745
Retry vs original call 137.3%

What it means

  • Five of the six typical failures are fixed without calling the model again, in a fraction of a millisecond and at no cost. Paying for another call to remove a comma is throwing money away.
  • A retry costs more than the original call (137.3%), because on top of repeating instructions and schema you resend the broken output and the error. And it adds another full model wait.
  • A truncated response can’t be properly repaired: the parser returns valid JSON, but data is missing. Detect it (for example, because the model stopped at the token limit) and in that case retry or raise the limit.
  • Always validate after repairing against the schema (Pydantic, JSON Schema): valid JSON isn’t correct JSON.

When to use each option

  • Client-side repair (json_repair, BAML): always as the first line of defence, with any model and any provider.
  • Constrained decoding (Outlines on vLLM, structured output modes): if you control inference or your provider offers it, it eliminates syntax failures at the root.
  • Retry: only for truncated responses or when schema validation fails on content, not syntax.

Limitations

  • Synthetic failures: we reproduced the six typical failures, not real outputs from a model. Each model’s failure rate wasn’t measured here.
  • BAML and Outlines weren’t measured. BAML requires generating its client from the schema and Outlines needs to run a model with constrained generation; they’re left for a future experiment. Their figures in the X thread come from another run and aren’t reproduced here.
  • Retry cost is calculated in tokens, not latency: the wait depends on the model and the provider.
  • A single medium-sized schema; with bigger schemas, retrying is even more expensive.

How to reproduce it

Code, environment and raw results: yeswelab-laboratorio/structured-outputs-agentes-ia-baml-outlines-vs-retries on GitHub. It takes less than a minute.

Are your agents burning tokens on retries? We measure where they go and how to cut it. Commission the experiment.

#json-repair#tiktoken#Python#BAML#Outlines#Pydantic#n8n