Back to articles
OpenAI Structured Outputs vs Zod: which to use for LLM response validation in 2026

OpenAI Structured Outputs vs Zod: which to use for LLM response validation in 2026

via Dev.toAtlas Whoff

Two approaches to the same problem: making sure an LLM returns valid, typed JSON. One is baked into the API. The other is a validation library you apply after the fact. Here's when to use which. The problem You ask an LLM to return JSON. Sometimes you get valid JSON. Sometimes you get JSON wrapped in markdown. Sometimes you get a friendly explanation instead of JSON. Sometimes you get JSON with wrong field names or missing fields. // What you want { " sentiment " : " positive " , " confidence " : 0.92 , " topics " : [ " AI " , " automation " ] } // What you sometimes get " Sure! Here's the analysis: json { sentiment: 'positive' } Both Structured Outputs and Zod solve this. They solve it differently. OpenAI Structured Outputs Available in the OpenAI API with response_format: { type: "json_schema", json_schema: { ... } } . The model is constrained during generation to only produce valid JSON matching your schema. const response = await openai . chat . completions . create ({ model : " gp

Continue reading on Dev.to

Opens in a new tab

Read Full Article
1 views

Related Articles