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
Midjourney Weapon Design: Master Prompts for Swords, Guns & More
Master Midjourney weapon design and creation. Learn to craft stunning swords, futuristic guns, and fantasy armaments with advanced prompting techniques and detailed examples.
Midjourney Environment Prompts: Master Creation & Design
Learn to create stunning environments in Midjourney with this comprehensive guide. Explore techniques for natural landscapes, urban settings, and interior spaces using effective prompts.
Midjourney Natural Landscape Prompts: Photorealistic Nature Scenes
Create stunning natural landscapes in Midjourney with photorealistic prompts. Master mountains, coastlines, forests, deserts, and seasonal variations with atmospheric depth.