Structured Output From LLMs: Reliable JSON Without Surprises
By Morteza Bazrafshan1 min readAIEngineeringPrompting
When a program, not a person, reads a model's answer, free text is a liability. Structured output turns the answer into data you can check.
Start with a schema
Write down the fields, types and allowed values before you write the prompt. Keep it small: every extra field is another place for mistakes. Prefer enumerations ("low" | "medium" | "high") over free text when you can.
Tell the model the shape
Put the schema in the prompt, say that the reply must contain only that JSON, and include one or two examples. Many providers also offer a structured-output or JSON mode that constrains the format; use it when available, and still validate.
Always validate
Parse the response, check it against the schema, and reject anything that does not match. Do not use the model's output in queries, file paths or commands without validation; see prompt injection and agent security.
Retry with the error
If validation fails, send the model its answer and the specific validation error and ask for a corrected version. One retry fixes most cases. After that, fail clearly instead of looping, because loops cost money (see AI costs).
Decide what missing means
State what to return for unknown values, usually null, and tell the model not to guess. Then handle null explicitly in your code.
Test it
Collect real inputs, including messy ones, and measure how often the output validates and how often it is correct. A format that parses can still be wrong, so keep checking accuracy as in how to evaluate AI output.
Frequently asked questions
Why validate model output if I asked for JSON?
Models occasionally return malformed or incomplete JSON, extra text or wrong types. Your program should never trust the output without checking it against a schema.
What should the model return when a field is missing?
Define it in the prompt, for example null for an absent value, and tell the model not to guess. Otherwise it may invent a plausible value.