Lesson 2 · 7 min
Structured Output
JSON mode, schema-guided generation, and post-validation.
For machine-readable output, give Claude an example *and* a schema. JSON-mode + Zod validation in code is the safest combo: model produces JSON, code parses and validates, retry on parse error with the validator's message.
Production scenario
Real-world example: Resume parser at an ATS
An applicant tracking system parses uploaded resumes into structured records. The recipe that finally stuck in production:
const schema = z.object({
name: z.string(),
email: z.string().email(),
experience: z.array(z.object({
company: z.string(),
title: z.string(),
start: z.string(),
end: z.string().nullable(),
})),
skills: z.array(z.string()),
});
const parsed = await callWithRetry({
system: SYSTEM,
user: `Parse this resume into JSON matching the schema.\n${resumeText}`,
schema,
maxRetries: 1,
});The retry appends the Zod error to the prompt: "Last attempt failed because: end date for 2nd job was not a valid string." The model fixes it on attempt two ~95% of the time.
Why this matters: JSON-mode reduces malformed output; schema validation catches what slips through; one retry on the validator error closes the gap.
Knowledge points in this lesson
- Provide schema and an example
- JSON mode reduces but doesn't eliminate parse errors
- Validate with Zod after parse
- Retry once with validator error appended
- Use tool-call output for hardest cases
- XML tags help when output has sections
Quick check
Prompt EngineeringSelect one
How long does Anthropic's prompt cache persist by default?
