Definition
Schema-validated output is model-generated content that has been parsed and validated against a defined schema before downstream code consumes it. Validation catches hallucinated keys, wrong types, missing required fields, and out-of-range values that a model might produce despite being instructed to follow a format.
Two common implementation patterns:
-
Prompt + parse + validate: instruct the model to emit JSON, parse the response with
json.loads(), and validate with a JSON Schema library (jsonschema) or Pydantic. Retry if validation fails. -
Constrained decoding: some inference runtimes (Outlines, llama.cpp with grammar) constrain token sampling to only produce valid tokens at each step, making validation failures structurally impossible.
Anthropic’s structured output mode uses constrained decoding server-side to guarantee the response matches the provided JSON Schema.
When using prompt + validate, set temperature = 0 to reduce hallucination risk on structured fields. Keep schemas small; the model is more reliable on schemas with fewer required fields.
When it applies
Validate every LLM output that downstream code reads as structured data. Even with constrained decoding, validate business logic constraints that the schema cannot express (e.g., start_date < end_date, quantity > 0). Use Pydantic model_validate for idiomatic Python type checking in one step.
Example
from pydantic import BaseModel, field_validator
import anthropic, json
class ExtractedEvent(BaseModel):
title: str
date: str # YYYY-MM-DD
attendee_count: int
@field_validator("attendee_count")
@classmethod
def positive(cls, v):
assert v > 0, "attendee_count must be positive"
return v
client = anthropic.Anthropic()
response = client.messages.create(
model="claude-sonnet-4-5",
max_tokens=256,
messages=[{"role": "user", "content": "Extract the event JSON from: 'AI summit 2026-06-01, 350 attendees'"}]
)
data = json.loads(response.content[0].text)
event = ExtractedEvent.model_validate(data) # raises if invalidRelated concepts
- structured-output - the broader pattern of constraining model output to a format.
- tool-call - tool call arguments should be schema-validated before execution.
- hallucination - schema validation surfaces hallucinated field values before they propagate.
- structured-output - the deep-dive on structured output patterns and libraries.
- prompt-design - prompting techniques for reliable JSON output.
Citing this term
See Schema-Validated Output (llmbestpractices.com/glossary/schema-validated).