Structured outputs
Getting reliable JSON and other schema-shaped output from a model, and validating it.
Bookmark
Bookmarks are kept in your free account, so you can find this page again on any device.
Sign in to bookmarkOn this page
Overview
Most applications need model output they can parse: a JSON object with known fields, a list of labels, an extracted table. Structured outputs is the skill of getting that reliably, using a provider's schema-constrained output mode where available, and validating everything you receive either way.
You define the shape once, usually as a Pydantic model or Zod schema, pass it to the model, and validate the response before your code uses it. Practitioners also design schemas that are easy for a model to fill in: clear field names, descriptions on each field, enums instead of free text, and an explicit way to say "not found" so the model does not invent values.
What you will practise
- Define the output shape as a Pydantic model or Zod schema
- Use a provider's schema-constrained output mode where it exists
- Validate every response before your code relies on it
- Design fields with enums, descriptions and an explicit "not found"
Learning paths
- AI foundations
A free AI foundations course: the working vocabulary of modern AI, from prompting and structured output to retrieval, MCP, Agent Skills and prompt injection.
- AI for data analysts
An AI course for data analysts: clean spreadsheets, profile data with skills, go from SQL to a dashboard, extract PDF tables and catch invented numbers.
- Building with the Claude API
Learn to build with the Claude API from your own code: the Messages API, tool use, structured output and prompt caching, then evaluate and trace what you built.
- Prompt and context engineering
A prompt and context engineering course: go from single prompts to deciding what a model sees at each step, from instructions and examples to files and tools.
Tutorials
- Add input and output guardrails to an LLM app
Length, topic and PII checks on input, schema and policy checks on output, then measure false positives and latency on a test set.
- Transcribe and summarise audio locally with faster-whisper
Transcribe audio locally with faster-whisper in Python, fix misheard names, then summarise the transcript with a local model through Ollama, all offline.
- Get reliable JSON from an LLM with structured outputs
Describe the output as a schema, let the provider enforce it, validate it with Pydantic AI or the AI SDK, and handle failures.
Workflow recipes
- Extract structured data from PDFs
Turn a folder of PDF invoices into validated JSON with Docling, a Pydantic schema and structured outputs, plus a review file for every one that fails a check.
- From business question to SQL dashboard with AI
The assistant writes the SQL, you define the metrics and check every number before anyone acts.
Tools for this skill
AI SDK
TypeScript toolkit for streaming, tool calls and agents across model providers.
Docling
Converts PDFs and office documents into structured output for LLM pipelines.
DSPy
Write LLM programs as declarative modules and let optimisers tune the prompts.
Guardrails AI
Python framework of validators that check and correct LLM inputs and outputs.
Instructor
Library for getting validated, typed structured outputs from LLMs using Pydantic models.
Outlines
Python library for structured LLM outputs via constrained decoding.
Pydantic AI
Type-safe Python agent framework from the Pydantic team.
Key terms
Words you will meet while learning this, defined in the glossary.
- Constrained decoding
Masking out disallowed tokens during generation so a language model can only emit text matching a schema or grammar.
- Structured outputs
Constraining or validating model output so it matches a schema, usually JSON Schema, and can be parsed reliably by your code.