Developer Technologies
Structured Outputs for LLM APIs: Reliable JSON by Design
Updated December 1, 2026
Why asking for JSON is not enough
A prompt that says “return JSON” is an instruction, not a parser or contract. The model may add prose, omit a field, use the wrong type, or return a syntactically valid object that does not make sense for the application. Parsing failures and semantic errors need different handling.
A structured-output feature lets an API accept a schema or constrained format, where supported. This can improve shape reliability and reduce ad hoc string parsing. The exact schema subset, response shape, and refusal behavior depend on the provider and model, so consult the endpoint’s current documentation.
Define a small schema
Start with the minimum fields the next stage needs. Mark required properties, constrain enums and ranges, and disallow unexpected fields where the provider supports it. A smaller schema is easier for a model to satisfy and easier for the application to validate.
Do not encode every business rule as a generation constraint. Some rules require database state, user permissions, or domain-specific validation after the response. Keep those checks in application code where they can be tested and enforced consistently.
const TicketSchema = {
type: 'object',
properties: {
category: { type: 'string', enum: ['billing', 'technical', 'other'] },
summary: { type: 'string' },
priority: { type: 'string', enum: ['low', 'normal', 'urgent'] },
},
required: ['category', 'summary', 'priority'],
additionalProperties: false,
};Handle outcomes explicitly
A request can fail, time out, be refused, or return a result that is incomplete for your application. Model SDKs may represent these cases differently from a successful structured payload. Do not convert every exceptional result into an empty object or a success-shaped default.
Parse the response according to the provider’s documented result type. Then apply a runtime schema validator and explicit domain checks. If validation fails, report a clear error or use a bounded correction attempt; do not silently keep malformed values.
- Distinguish provider errors, refusal, truncation, and schema violations.
- Validate identifiers and relationships against trusted application state.
- Set length and numeric bounds that make sense for the use case.
- Log a request identifier and failure class without exposing sensitive data.
Keep generation separate from authorization
A valid response can still request an unauthorized action. If a model returns a user ID, account status, or proposed operation, do not trust it as proof of identity or permission. Bind execution to the authenticated user and check access against the system of record.
For consequential actions, show the exact proposed operation and target to the user before execution. Use an idempotency key or equivalent mechanism when retries could otherwise duplicate an effect.
Test the contract over time
Keep a test set with ordinary, ambiguous, adversarial, and out-of-scope requests. Verify required properties, enum behavior, refusal handling, and downstream validation. When changing a model, prompt, or schema, compare results against the same cases before rollout.
Schemas evolve with applications. Version them deliberately and coordinate producer and consumer updates. If older clients still consume a response, preserve compatible behavior or provide a migration path rather than changing the meaning of a field silently.
When structured output is useful
Structured output is useful for classification, extraction, routing proposals, and data transformations consumed by code. It is not automatically the right choice for open-ended explanations or when the provider’s constraints cannot express the domain contract.
For content that must be machine-readable, combine a provider-supported constrained response with application-side validation. A schema generator can help draft a schema from an example; review the result and test it against real inputs.
Frequently asked questions
Does a valid JSON schema guarantee correct data?
No. It can constrain types and shape, but the values can still be factually wrong, unauthorized, or inconsistent with business rules.
Should I retry when validation fails?
Only with a bounded policy and a clear reason. Retry transient provider failures differently from invalid content; never retry a side effect without safe duplicate handling.
Related reading
References
About Ali Raza
Ali is a software engineering student at Riphah International University and the founder of the digital agency Ionix Growth. He writes about modern web architecture, developer tooling, and integrating AI workflows into web applications.