Validation and Retry Loops
Layers of validation (design guidance, grounded in verified facts)
- Syntax and schema: structured outputs make valid JSON that matches the schema, so schema-violation retries are generally unnecessary (verified). Check
stop_reasonfirst:max_tokenscan truncate the JSON;refusalis a 200 response that may not match. - Semantic and business rules: things a schema cannot express: totals add up, dates in order, ids exist in your database, values within allowed ranges, cross-field consistency, quoted evidence actually appears in the source.
- Grounding: each extracted value traces to the source text (see 5.6).
Retry with feedback
When validation fails, retry with the specific failure so the model can correct it:
- include the original input, the previous output and the exact validation error ("
totalis 120.00 but line items sum to 110.00"); - ask for a corrected output only;
- bound the retries (for example 2 to 3) and keep a record of attempts.
When a retry cannot help (design guidance)
Retrying is pointless when the information is not in the input: the model will either repeat the gap or invent a value. Recognise these cases and do not loop:
| Failure | Right response |
|---|---|
| Format or arithmetic slip | Retry with the error |
Truncated output (max_tokens) | Retry with a higher limit |
| Required value absent from the source | Return null or "missing", flag for a human or ask for more input |
| Persistent disagreement after N tries | Escalate to a human with the attempts attached |
refusal | Read stop_details; use the documented fallback, not blind retries |
Transient vs deterministic failures
Retry transient API or tool errors (rate limit, overload, timeouts) with backoff; do not retry deterministic validation errors unchanged (2.2).
Exam traps
- Retrying the same prompt unchanged and hoping.
- Retry loops with no cap.
- Retrying to fill a field the source never contained, which produces fabrication.
- Treating a schema pass as full validation.
- Swallowing the final failure instead of surfacing it.
References
- Structured outputs — checked Oct 16, 2026
- Handling stop reasons — checked Oct 16, 2026
Check your understanding
A model-extracted invoice total does not equal the sum of its line items. Action?Show answerHide answer
Answer
Retry with the specific discrepancy, bounded, then escalate if it persists.The customer's phone number is not in the document and extraction keeps returning a fake one. Fix?Show answerHide answer
Answer
Allow null/missing; stop retrying; flag for review.Output ended mid-JSON with
stop_reason: max_tokens. Fix?Show answerHide answerAnswer
Increasemax_tokensand retry.