Structured Outputs (JSON Mode)
Get consistent, parseable JSON responses from AI models. Learn schema definition, validation, and techniques for reliable structured data extraction.
Structured Outputs
Structured outputs ensure AI models return data in a specific, parseable format. JSON mode and schema enforcement eliminate parsing errors and make AI responses directly usable in applications.
Basic JSON Mode
Simple Object:
Return the following information as JSON:
{
"name": "string",
"age": "number",
"city": "string"
}
Text: "John is 32 years old and lives in Seattle."
Response:
{
"name": "John",
"age": 32,
"city": "Seattle"
}
Schema Definition
With JSON Schema:
{
"type": "object",
"properties": {
"summary": {
"type": "string",
"description": "Brief summary in 1-2 sentences"
},
"sentiment": {
"type": "string",
"enum": ["positive", "negative", "neutral"]
},
"key_topics": {
"type": "array",
"items": {"type": "string"},
"maxItems": 5
}
},
"required": ["summary", "sentiment", "key_topics"]
}
Extraction Patterns
Entity Extraction:
Extract entities from the text as JSON:
{
"people": [{"name": "string", "role": "string"}],
"organizations": ["string"],
"locations": ["string"],
"dates": ["string"],
"monetary_values": [{"amount": "number", "currency": "string"}]
}
Text: {input_text}
Data Transformation:
Convert the following unstructured text to structured data:
Input format: Free text
Output format:
{
"records": [
{
"field1": "value",
"field2": "value",
"field3": "value"
}
],
"total_count": "number",
"extraction_confidence": "number (0-1)"
}
Validation Techniques
Required Fields:
Return JSON with these REQUIRED fields:
- title (string)
- description (string)
- priority (number, 1-5)
Missing any required field = invalid response.
Enum Constraints:
Status must be one of: ["pending", "active", "completed", "cancelled"]
Priority must be one of: ["low", "medium", "high", "critical"]
Category must match: ["bug", "feature", "improvement", "documentation"]
Format Validation:
Dates in ISO 8601 format: "2025-01-15T10:30:00Z"
Emails must match: [email protected]
URLs must start with: https://
Common Patterns
| Pattern | Use Case |
|---|---|
| Single object | Extract one entity |
| Array of objects | Parse multiple items |
| Nested objects | Complex hierarchical data |
| Union types | Variable response structures |
Error Handling
If information is missing or unclear:
{
"extracted_data": {...},
"missing_fields": ["field1", "field2"],
"confidence": 0.85,
"notes": "Explanation of uncertainties"
}
Tips
- Be explicit about types - Specify string vs number vs boolean
- Use enums for fixed options - Reduces invalid values
- Include descriptions - Help model understand field purposes
- Set constraints - Min/max values, string lengths, array sizes
- Handle missing data - Define how to represent unknowns
Related Articles
Needle-in-Haystack: Finding Specifics in Massive Claude Contexts
Prompt patterns for targeted information retrieval from 200K token contexts. Multi-hop question answering, verification strategies, and techniques to ensure Claude finds what you're looking for in massive documents.
DeepSeek Tool Calls with Thinking: reasoning_content Management
Combine DeepSeek's tool calls with thinking mode. The mandatory reasoning_content passback in tool loops, strict mode for JSON schema enforcement, and error handling patterns to avoid 400 errors.
CoT vs Extended Thinking: When to Use Which
Compare chain-of-thought prompting vs Claude's extended thinking. Understand performance differences, use cases for each approach, and hybrid strategies that combine both for optimal results.