Why Production AI Runs on Structured Outputs (Pydantic & JSON Schema)
If you've ever asked an LLM to "reply only in JSON" and watched your FastAPI backend crash at 2 AM because the model added "Sure! Here is your JSON:" or turned an integer price into "$12.50", you already know why prompt-based formatting fails.
On November 30, 2026, OpenAI introduced enhanced function tooling with stricter schema validation and parallel tool calling—reporting up to 30% lower latency in multi-step workflows. Every major provider (OpenAI, Gemini, Anthropic, and local llama.cpp grammars) now supports Structured Outputs enforced at decoding time.
Prompted JSON vs. Schema-Enforced Structured Outputs
Prompted JSON ("Please output JSON"):
The model can still hallucinate extra keys, miss required fields, or wrap text in markdown backticks.Schema-Enforced Structured Outputs: You pass a Pydantic model or JSON Schema to the API or local grammar sampler. The decoder constrains token sampling so invalid fields or wrong types are syntactically impossible.
The Production Rule: Schema Enforcement + Server Validation
Even with strict JSON Schema enforcement, treat model output as untrusted user input:
Extract with a strict Pydantic schema: Let the LLM parse messy user intent (diet, budget, category, filters) into typed fields.
Validate business logic on the server: Never trust the LLM for prices, permissions, or database IDs—look up authoritative values in PostgreSQL or SQLite after parsing.
Fail fast with typed errors: If a required field is missing or out of range, catch it in Pydantic before it touches downstream SQL or payment logic.