Lesson 3 of 3 · Structured Outputs & Reliable AI
JSON Schema for AI
JSON Schema is a vocabulary for describing the structure of JSON data. It has been around since 2009, used primarily for API documentation and request validation. Structured Outputs repurposes it as a constraint language for AI model output -- and that change in context matters, because some JSON Schema features work differently when they are constraining a generative model versus validating a static document.
The Basics: Types and Properties
Every schema starts with a type and, for objects, a set of properties:
When using strict: true (which you should always use), two requirements apply that are not typical in standard JSON Schema usage: (1) Every property must be listed in required. (2) additionalProperties must be set to false. These are enforced -- your schema will be rejected if they are missing.
Supported Types
Structured Outputs supports the core JSON Schema types with some AI-specific considerations:
| Type | Example | AI Behavior Notes |
|---|---|---|
string | "hello" | The model generates free-form text within the field |
number | 42, 3.14 | Integers and floats; the model chooses based on context |
boolean | true, false | Reliable for binary classification tasks |
null | null | Used in union types for optional values |
object | {"a": 1} | Nested objects with their own property schemas |
array | [1, 2, 3] | Items schema defines the type of each element |
Enum Constraints
Enums are one of the most powerful features for AI output because they eliminate an entire class of parsing problems: value normalization.
Without enums, the model might return "Positive", "POSITIVE", "pos", "mostly positive", or "I would say positive." With enums, it returns exactly one of the four specified values. Every time.
Use enums for any field with a fixed set of valid values: categories, status codes, severity levels, rating scales, classification labels. Enums eliminate value normalization entirely and make downstream aggregation trivial.
Create enums with 50+ values or values that are ambiguously similar ('good' vs 'mostly_good' vs 'fairly_good'). The model may struggle to distinguish between very similar enum values, especially when the input is ambiguous. Keep enum sets clear and mutually exclusive.
Nested Objects
Real-world data is rarely flat. Structured Outputs handles nested objects naturally:
Every nested object needs its own required and additionalProperties: false in strict mode. This is verbose but explicit -- there is no ambiguity about what the model must produce.
Arrays with Typed Items
Arrays are essential for extraction tasks that produce variable-length results:
The model will return an array of objects, each matching the items schema. The array can be empty if no items are found -- which is valid and often preferable to the model inventing items.
Handling Optional Fields: The Union Pattern
In strict mode, all properties must be required. So how do you handle fields that may not exist in the source data? Use a union type with null:
The field phone is always present in the output (it is required), but its value can be either a string or null. This is how you tell the model: "this field exists in the schema, but if you cannot find the data, return null instead of making something up."
The Description Field: Guiding Model Behavior
JSON Schema's description field becomes a powerful steering tool when used with AI models. It does not affect schema validation, but it directly influences what the model generates:
Treat the description field as an inline prompt for each field. You can specify format ("ISO 8601"), length constraints ("2-3 sentences"), behavioral rules ("return null if not found"), and domain guidance ("use medical terminology"). These descriptions are processed by the model alongside its schema constraints, giving you fine-grained control over each field's content.
Schema Limitations to Know
Structured Outputs supports a subset of JSON Schema. Key limitations:
- No
minLength/maxLength: You cannot enforce string length at the schema level. Use thedescriptionfield to guide length. - No
minimum/maximum: You cannot enforce number ranges. Usedescriptionfor guidance. - No
pattern: Regex patterns are not supported. The model follows format hints indescription. - No
if/then/else: Conditional schemas are not supported. Use discriminated unions (anyOf) instead. - Max nesting depth: 5 levels of object nesting.
- Max properties per schema: 100 total across all levels.
Design a JSON Schema for extracting information from a job posting. Include: job title, company name, location (city, state, remote options), salary range (minimum and maximum, either or both may be missing), required skills (array of strings), experience level (enum: entry, mid, senior, lead, executive), and posting date. Handle all optional fields with union types. Add descriptions that guide the model to extract data accurately. Test your schema mentally against a job posting that lists only "competitive salary" with no numbers -- does your schema handle that gracefully?
Use ← → to navigate, Space to mark complete