/pattern/output-validation/

04 · ProductionGovernance & SafetyStructured Output ValidationSchema ValidationGuarded Output

Output Validation / Schema Enforcement.
Reject malformed model output before it pollutes the next step.

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

  • Downstream systems expect structured data.
  • Errors must be caught early.
  • Automated processing follows.

When it backfires

  • Free-form text is the actual goal.
  • Schemas inadmissibly restrict the answer.
  • Validation is purely syntactic while being blind to business logic.

The tradeoff

Higher reliability is gained against restrictions on expressive freedom.

The effect

What it actually does.

Output must conform to a schema before it is used.

outputschemavalidrejected
Pitfalls

Two ways this pattern will hurt you.

Schema passes; business logic fails

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.

Re-prompt loops forever on an unsatisfiable schema

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.

Framework support

Where Output Validation / Schema Enforcement is native.

OpenAI Structured Outputsconstrained decodingNative
LangGraphstructured output nodeNative
Microsoft Agent Frameworkschema-typed responsesNative
Google ADKcontrolled generationNative

Search

Search patterns, frameworks, and pages.