Build a JSON Schema for Structured AI Outputs
Structured output APIs from OpenAI (json_schema mode) and Anthropic (tool use) require a JSON Schema definition that constrains the model's response format. When you provide a schema, the API guarantees that the response matches it — eliminating the need for JSON repair and making downstream parsing reliable. This example shows how to build a schema for a common use case: extracting structured information from unstructured text. The schema builder generates schemas compatible with both OpenAI's strict mode and Anthropic's tool definition format. Key differences: OpenAI strict mode requires additionalProperties: false on all nested objects and does not support all JSON Schema keywords. Anthropic tool definitions use the same field names but are wrapped in a tool definition object. The builder outputs both formats from a single schema definition. When designing schemas for LLM extraction, prefer flat structures over deep nesting — models fill in required fields more reliably when the schema is simple. Use enum constraints to force the model to choose from a predefined list rather than generating arbitrary strings. Mark fields as required rather than optional wherever possible, since models often skip optional fields even when the source text contains the relevant information.
Extract the following fields from a job posting: - title (string, required) - company (string, required) - location (string, required) - salary_range (object with min and max integers, optional) - employment_type (enum: full-time, part-time, contract, required) - remote (boolean, required) - required_skills (array of strings, required) - years_experience (integer, optional)
FAQ
- What is the difference between JSON Schema and OpenAI structured outputs?
- OpenAI structured outputs use JSON Schema as the specification format but with restrictions — additionalProperties must be false, and some keywords are unsupported. The API enforces schema compliance at the generation level, not just through post-processing validation.
- Should I use structured outputs or JSON mode?
- Structured outputs with a schema are strongly preferred. JSON mode only guarantees valid JSON syntax — the model can still produce any structure. Structured outputs guarantee the exact fields and types you defined.
- How do I handle optional fields in structured outputs?
- In OpenAI structured outputs, all fields must be listed in required and can use a union type like ["string", "null"] to indicate optionality. This is different from standard JSON Schema where fields are optional by default.
Related Examples
LLMs frequently return invalid JSON despite being instructed to produce valid JS...
Format CSV Data for AI Fine-TuningFine-tuning LLMs on custom datasets requires converting raw training data into t...
Build an OpenAI Chat Completion RequestThe Chat Completion API is the primary interface for all GPT models and the foun...