Prompt & Context Engineering·Question 56 of 68Quick-fire

How do you get reliable structured output (for example JSON) from an LLM?

Quick-fire answer from the Awesome Generative AI Guide (MIT)

Type it the way you would explain it to an interviewer. A few sentences is enough; you can't break anything.

Model answer: the part to lead with

Prefer native mechanisms over prompt-and-pray: use the provider's structured-output or tool-calling feature with a JSON schema so the model is constrained to valid shapes, and validate the result against that schema in code. When constrained decoding is unavailable, give an explicit schema and one example in the prompt, set a low temperature, and parse defensively. Always handle the failure path: a retry with the validation error fed back, or a fallback value. Do not extract fields with brittle regex or string slicing from free-form prose.

Full worked answer

Prefer native mechanisms over prompt-and-pray: use the provider's structured-output or tool-calling feature with a JSON schema so the model is constrained to valid shapes, and validate the result against that schema in code. When constrained decoding is unavailable, give an explicit schema and one example in the prompt, set a low temperature, and parse defensively. Always handle the failure path: a retry with the validation error fed back, or a fallback value. Do not extract fields with brittle regex or string slicing from free-form prose.

Learn more: OpenAI: Structured Outputs guide

What a good answer covers

  • Prefer native mechanisms over prompt-and-pray
  • When constrained decoding is unavailable, give an explicit schema and one example in the prompt, set a…
  • Always handle the failure path: a retry with the validation error fed back, or a fallback value
  • Do not extract fields with brittle regex or string slicing from free-form prose

Key terms: Learn more

Quick-fire answer from the Awesome Generative AI Guide (MIT) · answer text last updated 6 November 2026 · scoring and review schedule by LLM Prep