Why it matters

Parsing free-form LLM output is a common source of production bugs. Structured output eliminates a class of parse errors and makes apps more robust.

Advertisement

The architecture

JSON schema mode: specify a schema, model constrained to produce matching JSON. Available in modern APIs (OpenAI, Anthropic, Gemini).

XML tags: instruct model to wrap output in specific tags. Parse tags from text.

Structured output pipelinePrompt with formatspec + schemaModel producesstructured outputParser validatesdownstream readyJSON mode + validation catches errors before they reach downstream systems
Structured output flow.
Advertisement

How it works end to end

JSON schema: specify types, required fields, enums. Model output guaranteed to match. Cleanest option when supported.

Function calling: define a function; model outputs a call with arguments matching the schema. Same enforcement as JSON schema, plus semantic meaning.

Fallback parsing: even with structured output, validate on receipt. Handle edge cases (missing fields, wrong types).