Skip to main content
Technical Enough
Next: Give the model facts

Get output you can build on

Chapter 5 of 10 in Add AI Features · 18 min

The fridge feature had worked on every photo we set up for it, and we had used rules in the prompt to get rid of the "Sure! It looks like..." reply we caught during the build: items only, no greetings, RETAKE when the photo is too dark to read. Then we watched the first parent from outside our team point a phone at a real fridge, and the screen that should have shown a shopping list showed nothing at all. The request itself went perfectly: the photo uploaded, and the call came back in seconds with a 200, the status code for success. But the shelf held leftovers in unlabeled containers, a case none of our rules covered, and the model had replied, "I can only make out a few items clearly: milk, possibly butter, and two containers I can't identify." A person reading that would understand it. Our code was written to receive a list of items and show each one as a row, and given a hedging sentence instead, it had no rows to show. Nothing was wrong with the model or the photo: we had asked the way you would ask a person, and then tried to use the reply as data.

Your code needs structured data, not sentences

Our list screen is ordinary frontend code, the part of the app users see. It parses the response as JSON, the plain-text format of labeled fields you saw in Make your first model call, which means it reads the text as data, and it shows each entry in items as a row. Here is the reply it was written for, next to the one it got.

What the code expects:

{ "items": ["milk", "butter"] }

What came back:

I can only make out a few items clearly: milk, possibly butter, and two containers I can't identify.

Both name the same groceries, but only the first is data your code can use. Parsing the first gives an object (a set of labeled fields) whose items field holds the grocery names as strings (the programming word for pieces of text), and the screen shows a row for each. Parsing the second fails with an error at the very first character, because valid JSON cannot start with a word of prose, and the screen shows nothing. The tempting workaround is to scan the sentence for the words you expect, but that breaks on the next run, because the model produces a new reply every time, and the next one may start with "You've got." Your site already uses JSON from every other API it calls; a weather endpoint never replies "Sure! Looks like rain."

Free text is for people. When your code uses a model's reply, ask for data in a fixed structure: the same fields, each with the same type (text, number, list), on every call.

Write the structure down as a schema

A schema is the written list of the fields your code expects and the type of each, in a standard notation called JSON Schema that providers and coding tools use. The fridge schema has one field:

{
  "type": "object",
  "properties": {
    "items": { "type": "array", "items": { "type": "string" } }
  },
  "required": ["items"]
}

Read it as a sentence: the reply is an object, it must contain items, and items is a list of strings. Most features need more than strings; an expense tracker that reads receipt photos might require:

{
  "type": "object",
  "properties": {
    "merchant": { "type": "string" },
    "total":    { "type": "number" },
    "currency": { "type": "string", "enum": ["USD", "EUR", "GBP"] },
    "date":     { "type": "string", "description": "YYYY-MM-DD" }
  },
  "required": ["merchant", "total", "currency", "date"]
}

Here total is a number your code can add up without first taking the "$" off "$12.50", currency must be one of three allowed values (the enum line, short for enumeration, lists them), and a reply missing any required field does not match the schema, so it counts as invalid. You can write a schema without knowing how to program, because the fields are whatever your feature's code needs, and you know those once you know what the screen has to show.

"Reply with JSON only" is a request, not a guarantee

Prompt v1 already has a section for rules and format, so the obvious move is to add one more line, "Reply with JSON only, no other text," and stop there. The line helps, but here is a reply that came back with it in place:

Here is your shopping list in JSON format:

{"items": ["milk", "eggs", "spinach"]}

Let me know if you need anything else!

The JSON is in there, but wrapped in prose, so parsing fails on the first word, just as it did at the parent's fridge. Current models follow format instructions well, but roughly one call in a hundred still returns something your parser (the code that reads the reply as JSON) rejects, and at a thousand calls that is ten blank screens. Nobody on our team reads each list before a user sees it, so the first person to see each failure is a parent at their fridge.

A model's output is probabilistic (it can vary from run to run), so output your code uses needs a check in code: a structure the reply is forced to match, then validation (checks the values must pass before your code acts on them).

Ask for the setting by name: structured outputs

The first half of that check, forcing the structure, has a name you can ask for. All three major providers offer a request option called structured outputs (you will also hear "JSON schema mode"): you send your schema with the call, and the provider limits what the model can produce so that the reply always parses. As of mid-2026 it is generally available on OpenAI, Anthropic, and Google. The guarantee is mechanical: the model can only produce output that keeps the reply valid against your schema, so a reply starting with "Sure!" cannot be produced. It is one setting on the call, not a change to how your site is built.

The drawing shows the same answer coming back both ways. As free text, it reads well to a person, but your code has to guess where each item is. As JSON matched to a schema, your code reads the list of items directly.

Free text versus structured outputThe model can return the same answer two ways. As free text, it reads well to a person but your code has to guess where each item is, which is brittle. As JSON matched to a schema, your code reads the list of items directly, which is reliable. Caption: free text is for people, structured output is for your code.TWO WAYS TO GET THE ANSWER BACKTHEMODELFREE TEXT“Sure! You’ve got milk, adozen eggs, and some spinach.”YOUR CODEhas to guess whereeach item isJSON, TO A SCHEMA{ "items": ["milk", "eggs", "spinach"] }YOUR CODEreads the listdirectlyFree text is for people; structured output is for your code.

Once you know the name, the request is short. If you build the feature yourself, ask your coding tool to pass the schema as the provider's structured outputs option. If engineers build it for you, ask them in one sentence, "turn on structured outputs with this schema," and hand over the schema. Here is the whole diff to our fridge call (the lines marked + are the ones added), written like the call in Make your first model call in a format not tied to any one provider:

  {
    "model": "(the model we chose)",
    "instructions": "(the fridge prompt)",
    "input": [ (the fridge photo) ],
+   "response_format": {
+     "type": "json_schema",
+     "schema": (the one-field schema above)
+   }
  }

Whether engineers build it or you do, you own the schema, because its fields are your feature's requirements. Our fridge call now sends the one-field schema along with a prompt line telling the model to return only items visible in the photo, and since then the screen has always shown rows, never a blank.

Check the values after the parse

Structured outputs stop the parse failures, but the harder problem remains: a guaranteed structure does not guarantee that the values inside it are right. {"items": ["caviar"]} matches the fridge schema perfectly and is wrong if there is no caviar in the photo; the model produces a well-formed wrong answer (one that fits the schema but is wrong) just as smoothly as a right one. Validation, checking the values, is the second half, and it is small. Ours is three lines in the route, the server code that makes the call:

const items = JSON.parse(reply).items;
if (items.length === 0) return askForRetake();
if (items.length > 60) return askForRetake();

An empty list means the model got nothing usable from the photo, so the feature asks for a retake instead of guessing, and a list of more than sixty items is suspicious enough that the feature asks for a retake rather than showing it. The receipt schema's checks are just as small: total is positive and under a sanity cap (an upper limit no real receipt would go over), date is a real calendar date, and the enum already limits currency to three values. Write one check for each field that could cause harm if its value were wrong, and run it after the parse, before your site acts on the value.

When prose is the right output

None of this applies when the reply goes to a person. A draft from Gmail's Help me write, or a meeting recap from Zoom's AI Companion, goes straight to a person who reads, edits, or discards it, and a schema would only add a step to turn the data back into text. The test takes a second: if code uses the reply, use a schema plus checks; if only a person reads it, let it be prose. Some features need both, such as a recap a person reads plus action items your code adds to a task list; give each output the treatment that fits it. The running club's support assistant, the chat box that answers members' questions, is one of these: the member reads the reply as prose, and the site's code has to know whether to send the question on to a person.

Try it now

The drill takes twenty minutes; the first path costs nothing and the second less than a cent. You bring Prompt v1 from Write the prompt as a spec, not a sentence. You leave with your schema, the fields your feature's code needs with a type on each, plus one validated output, and you carry both into the drills ahead.

No setup: Write your feature's schema on one page: every field your code needs, a type beside each, and allowed values wherever a wrong value would cause harm, using the receipt schema as your template. The running club's support assistant needs two fields: reply, the text the member reads, and needs_person, which is true when the question should go to a person at help@runclub.example and false when the assistant has answered it. Then act as the parser: try to pull each of your fields out of the hedging reply above by following fixed rules, the way code would, rather than by reading it as a person, and mark how few you can get reliably. Finish with one plain-English check per field, like "total is positive and under 500" or, for the club's assistant, "reply is not empty and is shorter than 1,200 characters."

With your tools: Give Claude Code your schema and Prompt v1, and ask it to pass the schema to your feature's model call as the provider's structured outputs option, with your value checks after the parse and a retry or retake when one fails. Run one real input and keep the reply that passes; that is your validated output. If your tools are not set up yet, the Setup Clinic gets you there in one sitting. In Codex or Cursor the move is the same: paste the schema, ask for the structured outputs setting plus checks, run one input.

Chapter Summary

  • A model reply that reads well to a person can be useless to your code: our fridge feature showed a blank screen because code written for a list of items received a sentence written for a person.
  • When your code uses the output, ask for data that matches a schema, the written list of field names and types your code expects on every call.
  • Prompt instructions like "reply with JSON only" make clean output more likely without guaranteeing it, and real users see the failures.
  • The fix has a name to ask for: structured outputs, also called JSON schema mode, one setting on the call at OpenAI, Anthropic, and Google, which limits what the model can produce so the reply always parses.
  • A guaranteed structure is not a guaranteed value; JSON that matches the schema perfectly can still contain a wrong answer.
  • Validation is a few lines of code after the parse that check numbers are in range, dates are real and options are allowed, and that send empty or absurd results to a retry or a retake.
  • When only a person reads the reply, prose is the right output; the schema is for replies that your code uses.
  • Carry your schema and your validated output forward; next, in Give the model the facts it wasn't trained on, you work on making the values right, not just well-formed.

Sources

  • OpenAI, Anthropic, and Google Gemini structured output documentation (last verified July 2026).
  • JSON Schema specification, json-schema.org.