Structured Outputs (JSON Mode)

Get consistent, parseable JSON responses from AI models. Learn schema definition, validation, and techniques for reliable structured data extraction.

April 19, 2026
structured-outputsjsonschemadata-extractionprompt-engineering

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

PatternUse Case
Single objectExtract one entity
Array of objectsParse multiple items
Nested objectsComplex hierarchical data
Union typesVariable response structures

Error Handling

If information is missing or unclear:
{
  "extracted_data": {...},
  "missing_fields": ["field1", "field2"],
  "confidence": 0.85,
  "notes": "Explanation of uncertainties"
}

Tips

  1. Be explicit about types - Specify string vs number vs boolean
  2. Use enums for fixed options - Reduces invalid values
  3. Include descriptions - Help model understand field purposes
  4. Set constraints - Min/max values, string lengths, array sizes
  5. Handle missing data - Define how to represent unknowns