mediumAI Engineering

How do you get reliable structured JSON output from an LLM?

1.1k views
01

Understand the problem

Schemas, constrained decoding, and the validate-repair loop production apps actually use.

structured-outputjson-schemavalidationreliability
02

Attempt it yourself

Sketch your approach before reading the solution — that's what interviews test.

Stuck? AI Nudge Available

Get a conceptual hint to guide your logic without spoiling the final implementation.

03

Study the solution

The solution is waiting

Give it an honest attempt first — then compare your thinking with the full walkthrough.

04

Read the code

Tool-as-schema + Zod validation
const Invoice = z.object({
  vendor: z.string(),
  date: z.string().regex(/^\d{4}-\d{2}-\d{2}$/),
  totalCents: z.number().int().nonnegative(),
  category: z.enum(["saas", "travel", "office", "other"]),
});

const msg = await client.messages.create({
  model, temperature: 0, max_tokens: 400,
  tools: [{ name: "record_invoice", description: "Record one parsed invoice",
            input_schema: zodToJsonSchema(Invoice) }],
  tool_choice: { type: "tool", name: "record_invoice" },
  messages: [{ role: "user", content: prompt }],
});

const parsed = Invoice.safeParse(toolInput(msg));
if (!parsed.success) return retryWithErrors(prompt, parsed.error); // once
05

Join the discussion

Discussion (0)

Sign in to join the discussion.

No responses yet. Be the first to share what you think.