Skip to content
All posts

Getting reliable JSON from an LLM: structured output that your code can trust

How to get structured output from an LLM that your code can safely act on: tight schemas, validation, retries, handling refusals and testing the edge cases before users find them.

Shubham Somani 4 min read

Most useful AI features end with code acting on what the model said. Extract fields from an invoice, classify a ticket, pick the next step in a workflow, fill in a form. That only works if the output arrives in a shape your code understands, every time.

It mostly works in a demo. In production, sooner or later a response comes back with a missing field, an extra sentence before the JSON, or a value your code never expected. Here’s how we’d make structured output from an LLM boring and reliable.

Design the schema for the model, not just the database

  • Keep it small. Ask only for the fields this step needs. Every extra field is another chance to get one wrong.
  • Use enums for categories. “One of: billing, bug, feature_request, other” beats a free-text label you have to normalise later.
  • Always include an escape hatch, such as “other” or “unknown”, so the model isn’t forced to pick a wrong answer.
  • Name fields plainly and describe each one in a short line. due_date with “ISO date, or null if none is mentioned” is better than dd.
  • Allow null where the data may be missing. Otherwise the model may make something up to fill the slot.

Use the strictest mode your provider offers

Many model providers offer a way to request or constrain structured output, and they differ in how strictly it’s enforced. Use the strongest option available for your model, and read its documentation for what it does and doesn’t guarantee.

Even with a strict mode, treat it as a strong hint rather than a contract. Models change, settings get missed and providers get swapped. Your code should never assume the shape is right without checking.

Validate everything, then decide

Parse and validate every response against your schema in code before anything acts on it.

  • Reject unknown values, not just missing ones. An enum that comes back as “Billing ” with a trailing space is still wrong.
  • Check the business rules too. A valid date can still be in the wrong year; a valid amount can still be negative.
  • Keep the raw response in your logs alongside the parsed one, so you can see what went wrong.

This is the same rule we use for prompt injection: output shaped by the model is untrusted input until your code has checked it.

Plan for failure, not just success

Some responses will fail validation. Decide what happens before it does.

  • Retry once, with the validation error included, so the model can correct itself. More than one or two retries usually means the prompt or schema needs work.
  • Fall back safely. Route to a person, use a default that does no harm, or show the user a clear message. Never act on a half-parsed response.
  • Handle refusals and “I don’t know” explicitly, as a valid outcome with its own path, rather than as a parse error.
  • Count failures by type. A rising rate of one kind of error after a change tells you exactly where to look.

Split hard jobs into smaller calls

If one call has to read a long document, reason about it and fill in a large object, quality drops.

  • Extract first, decide second. Pull out the facts in one step, then make the judgement in another, with each step validated.
  • Let code do what code is good at. Totals, date maths and lookups are more reliable in your code than in the model’s output.

Test the awkward inputs

  • Build a test set of real examples, including empty documents, two items in one, foreign languages and inputs where the right answer is “unknown”.
  • Re-run it on every prompt, schema or model change. We cover how in test your LLM feature before users do.

The short checklist

  • Small schema, plain names, enums with an “other” or “unknown” option
  • Nulls allowed where data may be missing
  • The strictest structured output mode your model supports
  • Validation in code, including business rules, before anything acts
  • One retry with the error, then a safe fallback
  • A test set full of awkward inputs, re-run on every change

If you’re building an AI feature that has to feed reliable data into the rest of your product, see our fixed-scope offers or book a 30-minute call.