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
Infographics & Diagrams: Nano Banana Prompts
Create educational graphics, process diagrams, and data visualizations with Nano Banana. Turn complex data into clear visuals.
Midjourney Character Creation: Master AI Prompts for Unique Designs
Master Midjourney character creation with AI prompts. Learn advanced techniques for generating compelling characters across diverse styles and genres, from portraits to fantasy beings, with detailed guides and examples.
Quick Prompts for Nano Banana: Ready-to-Use Templates
Fast, copy-paste prompts for social media, business, and trending formats. Get instant results with these ready-to-use Nano Banana templates.