Advanced Prompt Engineering / Output Control & Format

Structured Output

Intermediate [3/5]
JSON generation Schema-constrained output

Definition

Structured output constrains models to generate data in specific formats like JSON or XML. Many API providers offer "JSON mode" or schema enforcement that guarantees syntactically valid structured data.

This is essential for applications where LLM outputs feed into downstream systems that require parseable, typed data.

Key Concepts

  • JSON mode: API setting that forces valid JSON output
  • Schema enforcement: Output must match a defined structure
  • Type safety: Fields have expected types (string, number, array)
  • Parsing reliability: Output can always be parsed without errors

Examples

JSON Mode
API Configuration
response = client.chat.completions.create( model="gpt-4", response_format={"type": "json_object"}, messages=[{ "role": "user", "content": "Extract entities from: 'John lives in NYC'" }] ) # Guaranteed valid JSON: # {"name": "John", "location": "NYC"}
Schema Definition
Type Enforcement
Expected schema: { "product_name": string, "price": number, "in_stock": boolean, "categories": string[] } Model output will match this structure exactly.

Interactive Exercise

Design a Schema

Design a JSON schema for extracting information from job postings. Include fields for:

- Job title, company, location, salary range, required skills

Pro Tips
  • Always use JSON mode when you need to parse the output
  • Define optional vs required fields in your schema
  • Include type information in prompt even when using JSON mode
  • Have fallback handling for edge cases

Related Terms