When to reach for it
- Downstream systems expect structured data.
- Errors must be caught early.
- Automated processing follows.
/pattern/output-validation/
Model responses are validated against schemas, types, or business rules before use, and a failed check triggers a reject-and-re-prompt loop rather than letting the malformed output flow downstream.
In practiceAn order-processing agent validates that every model-generated JSON payload matches its Pydantic schema before inserting it into the database, rejecting and re-prompting on the first malformed response.
Without itWithout output validation, a hallucinated field or mistyped value from the model propagates silently into downstream systems and may corrupt database records or trigger unintended actions.
When to reach for it
When it backfires
The tradeoff
Higher reliability is gained against restrictions on expressive freedom.
Output must conform to a schema before it is used.
The JSON parses and matches the type, but the values violate an unstated rule — a negative quantity, a date in the past, a customer that doesn't exist.
Fix · Layer business validators on top of the schema. Treat syntactic validation as a precondition, not a guarantee.
The schema demands a field the model cannot produce for this input. Reject-and-re-prompt never converges, burning tokens and latency on a validation that can never pass.
Fix · Cap validation retries. On exhaustion, fall back to a typed error or human review rather than looping; log the schema that never validated so it can be fixed.
Keep going
Search patterns, frameworks, and pages.