Skip to content

Command Palette

Search for a command to run...

← All concepts
Concept

Structured Output

Constraining LLM output to a defined schema (JSON) for downstream reliability.

Simple explanation

Instead of free text, you tell the LLM to output JSON matching a schema. This lets your code parse the response reliably. 'Return {summary, confidence, sources} as JSON.'

Engineering explanation

Structured output uses response_format={type: 'json_object'} (OpenAI) or tool calling to force valid JSON. Validate with Pydantic/Zod. Retry on validation failure. Production: schema is the single source of truth (used for both model definition and validation), repair function for common malformations.

Example

Schema: {summary: str, confidence: float, sources: list[str]}. Model returns valid JSON. Your code parses without try/catch on format.

When to use it

Any LLM output that downstream code needs to parse. Any RAG, extraction, classification or agent system.

Common misconception

Structured output is NOT guaranteed. Models still produce malformed JSON occasionally. Always validate + retry.

Related concepts
Learn this in depth