Home / Blog / Dev notes

Before putting AI answers on screen, define structured output (a JSON schema) first

Dev notesSOL LABS AI3 min read

When you add an AI feature to a service, the demo stage often just shows the AI's answer as-is in a chat window. The trouble starts the moment you try to put that answer into a table, save it to a database, or pass it on to the next screen. Even for the same question, the shape of the answer changes a little every time.

This post covers structured outputs — a way to receive AI answers in a fixed format a program can read — based on Anthropic's and OpenAI's official documentation.

Why free-form answers break on screen

When a person reads it, "Summary: …" or "Three key points: …" is close enough. But a program has to find a value with the same name in exactly the same place. If one day the title line is missing, another day the list has four items, and another day an explanatory sentence is tacked on in front, the screen code can't read the answer.

So many teams start by writing "answer only in JSON" in the prompt. JSON is a data format that writes names and values in pairs. It works most of the time, but occasionally a bracket goes missing or a field name changes, and that one request fails. As users grow, that "occasionally" happens every day.

What structured outputs do differently

Structured outputs let you define the shape of the answer up front as a JSON schema (a blueprint stating which fields exist and whether each value is text or a number), and the model only produces answers that fit that blueprint.

ApproachWhat it guarantees
Asking for "JSON" in the promptNo guarantee; usually right
JSON modeValid JSON syntax
Structured outputs (schema specified)Syntax + the fields and value types you defined

Anthropic's documentation explains that structured outputs use constrained decoding (a method that prevents the model from choosing characters that break the defined format) to guarantee responses that match the schema. OpenAI's documentation draws the same line: JSON mode only ensures valid syntax, while structured outputs guarantee adherence to the schema you specify. This lets you cut down a lot of retry code that re-requests badly formatted answers.

Want to check this against your own data?AI Readiness Assessment · we review your material and map candidates and orderContact us

How to design the schema

It's easiest to work backward from your screens and storage structure. First write down what you'll show and what you'll save, then make only those values into fields.

  • Required fields only: include only the values the screen truly needs, and leave the rest out
  • Fixed choices as lists: group values with a fixed set of options, like category or status, into an enum (a list of allowed values)
  • Make room for "unknown": a field for when the source has no answer (e.g. found: false) reduces the chance the model fills the blank with something plausible-sounding
  • Keep descriptions short: a one-line description per field helps the model fill values more accurately

What structured outputs don't solve

A guaranteed format doesn't mean the content is correct. You still need to check separately whether the numbers or names in the fields are true.

A schema protects the "shape," not the "right answer." For important values like amounts and dates, add a step that checks them against the source.

It also helps to know the limits listed in the official documentation. Anthropic's documentation notes that some schema constraints, such as minimum/maximum values or string length limits, are not supported (the SDK re-validates the response it receives). OpenAI advises that in strict mode all fields be marked required and additionalProperties be set to false. And if the model refuses to answer for safety-policy reasons, that refusal is designed to be detectable separately in your program. Decide on the screen to show when that happens, too.

Getting started, in order

  1. Write down the values your screens and storage need
  2. Build a JSON schema from those values (required fields · allowed-value lists · a place for "unknown")
  3. Turn on structured outputs, and check the received values once more in code
  4. Add a step that checks important values against the source

Once AI answers start arriving in a fixed shape, you can treat them like ordinary data from then on — show them in tables, store them, and connect them to other features.

References

  • Anthropic, Structured outputs: https://platform.claude.com/docs/en/build-with-claude/structured-outputs
  • OpenAI, Structured model outputs: https://developers.openai.com/api/docs/guides/structured-outputs
Want to build something similar?

We review your current system and data, then outline the migration order and scope.

Talk to usStart with the 3-minute AI readiness check →
SOL LABS AI

We build web apps, mobile apps and business systems, then add and run the AI they need. We build and operate our own meeting notes, contract and company analysis AI.

See our projects
Get the next post by email?

Related posts