Lesson 179 of 1596
Structured Output With Zod
Force an LLM to return JSON that matches a schema. Zod + tool-use or JSON mode makes this reliable.
Creators · AI-Assisted Coding · ~27 min read
Stop Parsing Prose
Asking an LLM for JSON and parsing the string is a trap. Use the AI SDK's generateObject with a Zod schema — you get validated, typed data every time.
generateObject validates the response against the schema and retries on failure. You get a typed object, not a string.
import { generateObject } from "ai"; import { anthropic } from "@ai-sdk/anthropic"; import { z } from "zod"; const ExtractSchema = z.object({ people: z.array( z.object({ name: z.string(), role: z.string().optional(), email: z.string().email().optional(), }) ), summary: z.string().max(240), }); export async function extract(text: string) { const { object } = await generateObject({ model: anthropic("claude-opus-4-7"), schema: ExtractSchema, prompt: `Extract people mentioned and a short summary from:\n\n${text}`, }); // object is fully typed from the schema console.log(`${object.people.length} people found`); return object; }With raw OpenAI instead
OpenAI's helpers.zod converts Zod to the JSON schema the API wants. parse() returns typed data.
import OpenAI from "openai"; import { zodResponseFormat } from "openai/helpers/zod"; const client = new OpenAI(); const r = await client.responses.parse({ model: "gpt-5", input: [{ role: "user", content: "Extract a person from: Ada Lovelace, mathematician" }], text: { format: zodResponseFormat(z.object({ name: z.string(), role: z.string() }), "person") }, }); console.log(r.output_parsed); // { name: "Ada Lovelace", role: "mathematician" }Understanding "Structured Output With Zod" in practice: AI-assisted coding shifts work from syntax recall to design thinking — models handle boilerplate so you focus on architecture. Force an LLM to return JSON that matches a schema. Zod + tool-use or JSON mode makes this reliable — and knowing how to apply this gives you a concrete advantage.
- Apply Zod in your ai-coding workflow to get better results
- Apply JSON schema in your ai-coding workflow to get better results
- Apply structured output in your ai-coding workflow to get better results
- Apply parse in your ai-coding workflow to get better results
- 1Use AI to generate unit tests for an existing function
- 2Ask AI to refactor a messy function and explain the changes
- 3Have AI suggest a code review for a recent pull request
Key terms in this lesson
The big idea: never parse JSON by hand from an LLM. Zod + generateObject turns the model into a typed function.
End-of-lesson quiz
Check what stuck
8 questions · Score saves to your progress.
Tutor
Curious about “Structured Output With Zod”?
Ask anything about this lesson. I’ll answer using just what you’re reading — short, friendly, grounded.
Progress saved locally in this browser. Sign in to sync across devices.
Related lessons
Keep going
Creators · 40 min
Agents vs. Autocomplete — the Mental Model Shift
Autocomplete is a suggestion. An agent is an actor. The mental model you bring to each is different, and conflating them is the number-one reason teams trip over AI coding.
Creators · 50 min
Test-Driven AI Development
TDD was already the gold standard. Paired with an agent, it becomes the tightest feedback loop in software. Here's the full workflow and the pitfalls.
Creators · 50 min
Vector DB Basics With pgvector
Store embeddings, search by similarity. The foundation of every RAG system. Postgres plus pgvector gets you there.
