What is structured output?
Structured output is making an LLM return data in a strict, parseable format — usually JSON matching a schema — instead of free-form prose. It is how you turn a chatty model into a reliable component your code can consume programmatically.
Why it matters
Real applications need to act on a model's output: store it, branch on it, call another function with it. Free text is unreliable to parse. Structured output is the bridge from "the model said something" to "my program can use it," and it is essential for any LLM feature beyond a chat box.
What to learn
- Asking for JSON and specifying a schema
- Native structured-output and function-calling modes
- Validating output against a schema (e.g. with Pydantic)
- Handling parse failures and retries
- Constraining enums and types
- Keeping schemas small and focused
- Why validation is non-negotiable
Common pitfall
Trusting that the model's JSON is always valid and parsing it without a fallback. Even with structured-output modes, a model can occasionally return malformed or schema-violating data. Always validate against the schema and handle the failure — retry or repair — because one bad parse in production should not crash the feature.
Resources
Primary (free):
- OpenAI — Structured outputs · docs
- Pydantic — Documentation · docs
- Anthropic — Tool use · docs
Practice
Define a schema for some data you want from an LLM, prompt it to return JSON matching that schema, and validate the result with Pydantic. Add handling for a validation failure — a retry or a clear error. Done when malformed output is caught and handled instead of crashing your code.
Outcomes
- Make a model return schema-conformant JSON.
- Validate output against a schema.
- Handle parse and validation failures gracefully.
- Use structured output to integrate an LLM into code.