Structured Output
Making LLMs Speak the Language of Code
Learn how to get reliable, parseable, schema-validated outputs from LLMs instead of unpredictable free text. The bridge between AI and your application code.
Why Structured Output Matters
LLMs Speak English. Your Code Speaks JSON.
The Core Problem:
LLMs generate free-form text by default. But your application needs structured data - JSON objects, typed fields, validated formats. Without structured output, you end up writing fragile regex parsers to extract data from LLM responses, and they break every time the LLM changes its response format.
Think of it like this: you order from Zomato and expect the order details in a structured format (item name, quantity, price, address). If Zomato sent you a paragraph of text describing your order in prose, your payment system would not know what to charge. Structured output solves this for AI systems.
Real-World Impact:
- Without Structured Output: Parse errors, broken pipelines, inconsistent responses, hours debugging regex patterns
- With Structured Output: Guaranteed JSON schema compliance, type-safe integration, zero parse errors, clean data pipelines
Three Approaches to Structured Output:
| Approach | Reliability | Flexibility | Provider Support |
|---|---|---|---|
| Prompt Engineering (ask nicely for JSON) | Low (60-80%) | High | All LLMs |
| JSON Mode | High (95%+) | Medium | OpenAI, Anthropic, Gemini |
| Function Calling / Structured Outputs | Very High (99%+) | Schema-bound | OpenAI, Anthropic, Gemini |
Note: Structured output is the foundation of every production AI application. Without it, you cannot reliably integrate LLMs into your code. This is the most practically important topic in prompt engineering.
JSON Mode - Guaranteed JSON Responses
Tell the LLM: You MUST Return Valid JSON
How JSON Mode Works:
JSON Mode is a parameter you set when calling the API. When enabled, the LLM constrains its token generation to only produce valid JSON. It will never output text that is not valid JSON. This happens at the model level, not through post-processing.
Important distinction: JSON Mode guarantees valid JSON syntax, but does NOT guarantee the JSON matches a specific schema. You might get valid JSON with unexpected keys or types.
JSON Mode Across Providers:
| Provider | Parameter | Notes |
|---|---|---|
| OpenAI | response_format: {type: "json_object"} | Must include "JSON" in system prompt |
| Anthropic | Prompt engineering + prefilled response | No native JSON mode; use prefill trick |
| Google Gemini | response_mime_type: "application/json" | Supports JSON schema too |
JSON Mode Limitations:
- No Schema Enforcement: You get valid JSON but the structure might differ from what you expect
- Extra Fields: LLM might add fields you did not ask for
- Wrong Types: A number field might come as a string
- Nested Issues: Deeply nested structures are less reliable
Note: JSON Mode is good for simple use cases. For production systems that need schema compliance, use Function Calling or Structured Outputs instead.
Function Calling - Schema-Validated Structured Output
The Gold Standard for Reliable Structured Output
How Function Calling Works:
You define a function schema (name, description, parameters with JSON Schema types) and pass it to the API. The LLM generates a structured response that matches the schema exactly. The output is guaranteed to be valid against the schema - correct types, required fields present, enum values respected.
Think of it like a Google Form. The form defines the fields, types, and validation rules. The respondent can only submit data that matches the form structure. Function calling is like giving the LLM a Google Form to fill out.
Function Calling is NOT Just for Calling Functions:
Despite the name, function calling is used for two purposes:
- Tool Use: Agent needs to call an external API or function (the original purpose)
- Structured Data Extraction: You just want structured output without actually calling any function. You define a "fake" function schema that describes the output structure you want.
OpenAI Structured Outputs (strict mode):
OpenAI introduced strict: true in function calling, which guarantees 100% schema compliance. The model uses constrained decoding to only generate tokens that are valid according to the schema. This is the most reliable approach available.
- Supports all JSON Schema types: string, number, boolean, array, object
- Supports enums, required fields, nested objects
- First request is slower (schema compilation), subsequent requests are fast
Comparison Table:
| Feature | JSON Mode | Function Calling | Strict Structured Output |
|---|---|---|---|
| Valid JSON | Yes | Yes | Yes |
| Schema Match | No | Usually | Guaranteed |
| Type Safety | No | Partial | Full |
| Production Ready | Simple cases | Yes | Best option |
Note: If your provider supports strict structured outputs, always use it. The 100% schema guarantee eliminates an entire class of bugs from your application.
Practical Patterns for Structured Output
Real-World Use Cases and Implementation Patterns
Pattern 1: Data Extraction from Unstructured Text
Extract structured data from resumes, invoices, emails, or documents.
Schema: {
"name": string,
"email": string,
"phone": string,
"skills": string[],
"experience_years": number,
"education": { "degree": string, "institution": string }
}
Input: A resume in PDF text format
Output: Clean, structured JSON matching the schema exactlyPattern 2: Classification with Confidence
Classify input into predefined categories with confidence scores.
Schema: {
"category": enum["bug", "feature", "question", "docs"],
"confidence": number (0-1),
"reasoning": string,
"priority": enum["low", "medium", "high", "critical"]
}Pattern 3: Multi-Step Extraction Pipeline
For complex documents, chain multiple extraction steps:
- Step 1: Extract all entities (names, dates, amounts)
- Step 2: Classify relationships between entities
- Step 3: Validate against business rules
- Step 4: Output final structured document
Error Handling Best Practices:
- Schema Validation: Always validate the output against the schema even with strict mode (defense in depth)
- Retry Logic: If validation fails, retry with an error message appended to the prompt
- Default Values: Define sensible defaults for optional fields
- Partial Extraction: If the LLM cannot fill all fields, return what it can with null for unknowns
Note: Data extraction from unstructured text is the number one use case for structured output in enterprise AI. It replaces expensive manual data entry across industries.
Gotchas and Production Considerations
Things That Break in Production
Gotcha 1: Schema Complexity Limits
Extremely complex schemas with deep nesting (5+ levels), many optional fields, or recursive types can confuse the LLM. Even in strict mode, complex schemas increase latency significantly because the model has to navigate more constraints during generation.
Fix: Keep schemas flat and simple. Split complex schemas into multiple extraction steps.
Gotcha 2: Hallucinated Values in Correct Format
Structured output guarantees the FORMAT is correct, not that the VALUES are correct. The LLM can still hallucinate a fake email address that is a perfectly formatted string. Schema validation catches format issues, not factual correctness.
Fix: Add semantic validation on top of schema validation. Verify extracted data against known sources when possible.
Gotcha 3: Token Cost of Schema
The function/tool definition itself consumes tokens from your context window. Complex schemas with many fields and descriptions can use 500-1000+ tokens just for the schema definition, leaving less room for actual content.
Fix: Keep field descriptions concise. Remove optional fields you do not need for a specific task.
Gotcha 4: Provider Lock-in
Each provider (OpenAI, Anthropic, Google) has slightly different APIs for structured output. Switching providers means rewriting your integration layer.
Fix: Use an abstraction library (Instructor, LiteLLM) that normalizes structured output across providers.
Note: Structured output guarantees correct FORMAT, not correct VALUES. Always add business logic validation on top of schema validation.
Interview Questions - Structured Output
Q: What is the difference between JSON Mode and Function Calling?
JSON Mode guarantees valid JSON syntax but not schema compliance. The LLM might return valid JSON with unexpected keys or types. Function Calling provides a JSON Schema that the output must match, giving you type safety, required fields, and enum constraints. Strict mode in Function Calling guarantees 100% schema compliance through constrained decoding.
Q: Does structured output prevent hallucination?
No. Structured output guarantees the output FORMAT is correct (valid JSON, correct types, required fields), but the VALUES can still be hallucinated. For example, the LLM might output a perfectly formatted email field with a fake email address. You need separate validation for factual correctness.
Q: How does constrained decoding work in strict mode?
During token generation, the model maintains a state machine based on the JSON Schema. At each step, only tokens that would produce valid output according to the schema are allowed. For example, if a field is an enum with values ["low", "medium", "high"], the model can only generate those exact strings for that field. This happens at the token level, making it mathematically impossible to violate the schema.
Q: What are the limitations of structured output with complex schemas?
(1) Deep nesting (5+ levels) increases latency and reduces reliability. (2) Schema definitions consume context window tokens. (3) Recursive types may not be supported. (4) Very large schemas slow down the first request due to schema compilation. Best practice: keep schemas flat, split complex extractions into multiple steps.
Q: Why is Function Calling used for data extraction even when no function is called?
Function Calling provides the best schema enforcement mechanism available. For data extraction, you define a "virtual" function whose parameters match the data structure you want to extract. The LLM fills in the parameters (your structured data) without any function actually being executed. It is a clever reuse of the tool-calling infrastructure for structured data extraction.
Frequently Asked Questions
What is Structured Output?
Learn how to get reliable, parseable, schema-validated outputs from LLMs instead of unpredictable free text. The bridge between AI and your application code.
How does Structured Output work?
LLMs Speak English. Your Code Speaks JSON.
Related topics
Practice this on DevInterviewMaster
Read the full Structured Output breakdown with interactive demos, quizzes, and Hinglish notes.
800+ system-design, LLD, coding, and design-pattern topics. Unlock everything with Pro (₹499, one-time) or Ultimate (₹999, one-time) — lifetime access, no subscription.