Tutorial2 min read

Structured Outputs: Getting Reliable JSON from LLMs

Most production AI features do not want prose. They want data: a category, extracted fields, a decision. Getting that reliably is an engineering problem with well-known solutions.

TrueCodeAI Engineering
Agents, Voice & ML practice
Published
LLMTutorialEngineering
Code editor showing markup on a dark screen

Use schemas, not instructions

"Reply in JSON" in a prompt works most of the time. Providers that support structured outputs let you pass a JSON schema and guarantee the output conforms to it. Use that wherever available; it removes a whole class of parsing failures.

Design schemas the model fills well

  • Use enums for categories — the model picks from your list instead of inventing labels.
  • Allow null or "unknown" for fields that may be absent, so the model is not forced to guess.
  • Add a short reasoning or evidence field before the decision field when accuracy matters.
  • Keep nesting shallow and field names descriptive.

Validate anyway

  1. Validate against the schema in code.
  2. Apply business rules: dates in range, totals consistent, IDs that exist.
  3. On failure, retry once with the validation error included.
  4. On repeated failure, route to a person — never silently accept.

Tool calls are structured outputs too

Tool arguments follow the same rules. A well-designed tool schema with enums and clear descriptions produces more accurate calls than a free-form parameter.

Frequently asked questions

Do structured outputs reduce quality?

Not noticeably for well-designed schemas. Overly complex schemas can hurt — keep them simple.

What about very long extractions?

Split into sections or pages and merge results in code.

Can small models do this?

Often yes, for well-defined extraction. Test on your data with evals.

Tell us what you want to exist.

We reply within 24 hours at hello@truecodeai.com with how we would build it.

Get a fixed price WhatsApp