AI Engineering · LLM applications that ship
How do you get reliable structured output from an LLM?
Short answer
Use the provider's schema-constrained output or JSON mode, validate the result against the schema, and retry or repair only invalid responses. Prompting for JSON alone is not a guarantee.
Structured output turns free text into a contract. The strongest option is constrained decoding, where the provider only allows tokens that keep the output valid for your schema. Where that is unavailable, ask for JSON, parse it, and validate every field before using it.
{
"type": "object",
"required": ["sentiment", "confidence"],
"properties": {
"sentiment": { "enum": ["positive", "neutral", "negative"] },
"confidence": { "type": "number", "minimum": 0, "maximum": 1 }
},
"additionalProperties": false
}- Validate types, enums and ranges in code, not just JSON syntax.
- On failure, send the validation error back once with the original output; do not loop forever.
- Log invalid outputs so you can see whether the prompt or the schema is the problem.
Follow-ups you should expect
Interviewers use these to check whether the first answer was understanding or memorisation.
- 1What do you do when the model returns a plausible but invalid enum value?
- 2How would you make the schema backward compatible for older clients?
- 3When would you prefer a tool call to a JSON response?
Say it in the interview
I want the schema enforced as close to generation as possible, so I use the provider's structured-output mode when it exists. Then I validate the parsed object in code and check ranges and enums, not just that it is valid JSON. If validation fails, I retry once with the error attached, then fall back to a safe default. Prompting for JSON on its own is too fragile for production.
Use this as a model, not a script. Saying it in your own words is what makes it survive follow-up pressure.
Related questions
Reading is step one
Now say it out loud.
Understanding an answer and delivering it under pressure are different skills. Rehearse this topic in a live AI Engineering mock interview and get scored feedback on depth, clarity and confidence.