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
Mastering Vehicle Design in Midjourney: Cars, Aircraft, Watercraft & More
Master vehicle design and creation with comprehensive Midjourney prompts. Learn advanced techniques for generating realistic and imaginative cars, aircraft, watercraft, and vehicles with precise control over design, style, and technical details.
Creative Writing with Claude: Prose, Dialogue & Worldbuilding
Prompts for creative writing with Claude — the model where Anthropic's literary strengths shine. Master prose, dialogue, narrative structure, and worldbuilding with Claude's unique creative capabilities.
Claude Context Window Economics: 200K vs RAG vs Summarization
Compare the costs, latency, and accuracy of using Claude's full 200K context vs RAG vs summarization chains. Decision frameworks for when each approach makes economic and technical sense.