How should an agent recover when the previous turn produced output that failed schema validation?
Show the model its failed output verbatim, the validator's structured error pointing at the exact failing field and constraint, the original task and evidence, and request only the corrected JSON, then cap retries
Imagine asking a friend to fill out a form and they get one field wrong. The wrong way to fix it is to hand them a blank form and say try again, they will probably make the same mistake. The right way is to hand them their filled-out form back, point at the wrong field, and say what the rule was. Now they can see exactly what to change. If they still cannot get it after one or two tries, do not keep asking forever, fall back to filling it in yourself or skipping the field.
Concept explanation~2 min read
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.
Concept explanation~2 min read
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.
Schema validation failures are an everyday reality of structured-output agents, and the way an agent responds to them is one of the clearest tests of good context engineering. The right repair turn gives the model specific structured information about the failure and asks for a minimal diff, not a regeneration. The wrong repair turn resends the same prompt with a generic try again and burns tokens on the same failure. This deep dive walks through the four context pieces, the retry policy, the fallback strategies, the role of provider structured-output modes, and the deeper principle that structured-output failures are usually context insufficiencies dressed up as model errors.
The four context pieces of a repair turn
A well-designed repair turn contains exactly four things.
The original task and any retrieved evidence. The model needs to know what it was asked to do and what source material it should ground in. If the task was 'extract these fields from this customer email,' the email is still in context. Stripping the grounding and just asking 'fix the JSON' loses the information that made the original answer plausible and forces the model to guess.
The failed output verbatim. The model needs to see exactly what it produced so the repair is a diff against that output. Without it, the model regenerates from scratch and the same error often recurs. Showing the failed output also lets the model use its own previous reasoning, most of the fields were probably correct, and the repair should preserve them.
The validator's structured error. This is the highest-leverage piece. Generic errors like 'JSON parsing failed' or 'schema validation failed' give the model nothing actionable. Structured errors name the field path, the constraint that failed, and what was received:
Validation error:
field: invoice.line_items[2].unit_price
constraint: must be positive number
received: -45.99
Validation error:
field: invoice.due_date
constraint: must be valid ISO 8601 date
received: "next Tuesday"
When multiple errors are present, list all of them so the model can fix them in one pass rather than fixing one and missing another.
A precise instruction. Something like 'Return only the corrected JSON. No commentary, no markdown fencing, no explanation. Change only the fields that failed validation; keep all other fields unchanged.' Each clause closes a common failure mode: 'no commentary' prevents prose preambles, 'no markdown fencing' prevents code-block wrapping, 'change only the fields that failed' enforces minimal repair.
Situations where this technique stops working.
2–4 min · Everything important, quickly.
Real products, models, and research that use this idea.
- Pydantic AI implements this repair pattern with structured error feedback across providers
- Instructor library wraps the failed output plus structured error pattern for Python applications
What an interviewer would ask next. Try answering before peeking at the approach.
QHow would you architect a structured-output pipeline that uses both a constrained-generation mode and a repair fallback?
First-line: use the provider's structured-output mode (Anthropic tool-use, OpenAI structured outputs, Gemini response mime type) with your JSON schema. Most outputs validate on the first call. For the few that fail, fall through to a repair turn that includes the failed output and the structured validator error. Cap the repair turn at one retry. If it still fails, route to the fallback path. Instrument first-attempt success rate, repair success rate, and fallback rate to monitor pipeline health.
Red flags & common mistakes
The phrases that signal junior thinking. Click to expand.
Red flags & common mistakes
The phrases that signal junior thinking. Click to expand.
Resending the same prompt with a generic try again message and watching the model produce the same broken JSON until the retry loop burns the budget.
60 second bullets to scan on the way to the call.
Name the four context pieces in a repair turn (task plus evidence, failed output, structured error, instruction)
Explain why a structured error beats a generic try again
Primary sources. Browse if you want the original framing.
Same topic, related formats. Practice these next.