Skip to the page

LLM basics for builders · mid

Reliable JSON and structured output

Asking for JSON in the prompt fails sometimes. Structured output with a schema, or a tool call, makes the shape reliable, but your code must still check that the values are true.

The notes

What you must know

  • Prompt-only JSON fails some of the time: extra prose, markdown fences, trailing commas, or output cut off at the token limit.
  • JSON mode guarantees valid JSON, not your shape. Structured output with a JSON schema uses constrained decoding: at each step the server blocks tokens that would break the schema.
  • Providers support only a subset of JSON Schema. Often every field must be required, additionalProperties must be false, and some keywords such as pattern or recursive references are limited.
  • Tool calling gives the same guarantee when the output really is call this function with these arguments, and it lets the model choose between several tools.
  • A schema fixes the shape, not the truth. Validate values in code (Pydantic, zod), check that ids exist, use enums for labels, and allow null or unknown so the model is not forced to invent.
  • Add an other or needs_review value to an enum, and send unknown values to a default queue instead of dropping them.
  • Pin a dated model version. A floating alias can move to a new snapshot and change formatting, which broke parsers in real incidents.
  • After a model upgrade, run the eval set and watch parse-failure rate. Retry once with the validation error, then send the rest to human review.

The red pen

Common traps: mistake → fix

  • Mistake: JSON mode means my schema is followed

    Fix: it only means valid JSON; use a schema for the shape.

  • Mistake: If the schema matches, the data is correct

    Fix: the model can put a wrong amount in the right field; validate in code.

  • Mistake: A floating model alias is safe

    Fix: it can change behaviour without any deploy of yours.

Easy to hard

Questions interviewers ask, easy to hard

  1. How do you get JSON from a model every time?
  2. JSON mode, structured outputs or tool calling: how do you choose?
  3. A classifier returns labels that are not in your list. What do you do?
  4. After a model upgrade 4 percent of parses fail. What do you do?
  5. What checks do you add after the schema check?

Explain it out loud

Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.

Practise by voice

Your first interview is free

Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.

Start your free interview