Structured Output with LLM APIs: JSON, Schemas, Validation, and Retries
Many AI applications need structured output, not prose. They need JSON, fields, labels, scores, tool arguments, or database-ready records.
Structured output is powerful, but it needs validation.
Common use cases
Structured output is used for:
- field extraction
- classification
- routing
- tool calls
- form filling
- product tagging
- lead scoring
- policy checks
Use schemas
Define exactly what the model should return:
{
"priority": "low | medium | high",
"category": "billing | technical | account",
"summary": "string",
"needs_human_review": true
}Then validate the output in code.
Expect failures
Models may return:
- invalid JSON
- missing fields
- extra fields
- wrong enum values
- natural language around JSON
- hallucinated data
Your application should handle these cases.
Retry with feedback
If validation fails, retry with a short correction prompt. Do not retry forever. Track validation failure rate by model and prompt.
Choose models carefully
Some models follow structured output instructions better than others. Evaluate models on schema pass rate, not just answer quality.
Final thoughts
Structured output makes LLMs useful in software workflows. Use schemas, validation, retries, and model-specific evaluation before relying on generated JSON in production.