Mid

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

How to answer it in an AI engineer interview: what is being tested, a step-by-step framework, a sample answer and the mistakes to avoid.

$19.99 on Gumroad, instant download40% off with code EARLYBF until Nov 3, 11:59 PM PTEARLYBF: 40% off until Nov 3

Smaller start: Score your answers against 50 rubrics, $9. 50 AI engineer questions, one rubric each, PDF on Gumroad.

Practiced one out loud? Paste your answer into the free self-check and get a score in 30 seconds

What the interviewer is testing

Whether you rely on enforcement and validation rather than prompt wording alone, and whether you design schemas that the model can fill correctly.

Answer framework

  1. Enforce the format: use the provider's structured output or JSON-schema mode, or constrained decoding (grammar-guided generation) when self-hosting.
  2. Validate every response against the schema in code, for example with Pydantic or a JSON Schema validator.
  3. Repair on failure: retry once or twice with the validation error included in the prompt, then fall back or escalate.
  4. Design the schema: keep it flat, use enums for categories, make optional fields explicit and allow null instead of forcing guesses.
  5. Monitor the parse failure rate and the rate of empty or default values in production.

Sample answer

I treat structured output as an engineering problem, not a prompting problem. First I enforce the format at generation time: hosted APIs offer a JSON-schema or structured output mode, and with a self-hosted model I use constrained decoding so only tokens that keep the output valid can be sampled. That removes most syntax errors.

Valid JSON can still be wrong, so I validate the parsed object against a Pydantic model: types, enums, ranges and required fields. If validation fails, I send one repair attempt that includes the original output and the exact validation error, and if that also fails I return a clear error or route to a fallback instead of passing bad data downstream.

Schema design matters as much as enforcement. For an invoice extractor I would use a flat object, an enum for currency, explicit nullable fields for values that may be missing, and a short description per field. If a model is forced to fill a required field it cannot find, it will invent one, so allowing null is safer. I then track parse failures and null rates per field as quality signals.

Likely follow-ups

Why not just ask for JSON in the prompt? Prompt-only instructions work most of the time, but at scale you get truncated objects, extra prose and wrong types; enforcement plus validation handles those cases.

Common mistakes

The PlaybookWant the full method?

The 0 to 1 AI Engineer Interview Playbook covers the answer frameworks, system-design walkthroughs and an 8-week plan.

Get the playbook 40% off with code EARLYBF until Nov 3, 11:59 PM PTEARLYBF: 40% off until Nov 3

Smaller start: Score your answers against 50 rubrics, $9. 50 AI engineer questions, one rubric each, PDF on Gumroad.

Interviewing for PM or TPM? See the full PM package, $59 on Gumroad: a resume system, a PM, TPM and PGM interview handbook, a 1:1 cheat sheet and a PM playbook. PM and TPM content, not AI engineer prep.

Related questions

Based on question #19, #96 in the full question bank.