Skip to solution
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.

Nudge consolestandby

Stuck? Beam a request up — the console returns a conceptual nudge that guides your logic without spoiling the implementation.

03

Study the solution

Best-first order: use the provider's structured-output/JSON mode with a JSON Schema (constrained decoding guarantees syntax), or a tool/function definition whose parameters encode your schema. Reinforce with prompt-level instructions and a low temperature. Always validate the parsed result against the schema (e.g. Zod/

Solution ready — 2 min read

Classified // press E to declassify

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.

Transmission complete // awaiting log

KEEP THE
STREAK ALIVE.

Dossier 19 of 80 decoded in the AI Engineering track. One more won't hurt.

Back to track