Blog / Build with AI /
JSON mode and schemas explained for builders
An AI model answers in text. Your code wants data. The gap between those two is where many small AI projects quietly break.
Ask a model to “list the product’s name, price and colour” and one day you get a tidy list. The next day you get a friendly sentence, a bullet list, or the tidy list wrapped in “Sure! Here you go:”. A person reads all three fine. A program that expects one exact shape does not.
That’s the problem JSON mode and schemas try to solve. At its DevDay event on 6 November, OpenAI announced a JSON mode for its API, so this is a timely one to understand.
What JSON is, in one paragraph
JSON is a plain-text way to write down structured data. It uses curly braces for objects, square brackets for lists, and pairs of names and values:
{
"name": "Blue ceramic mug",
"price": 12.5,
"in_stock": true,
"colours": ["blue", "white"]
}
Almost every website and app can read it. That’s why it’s the common language between systems, and why asking an AI for JSON is so useful. I covered how it fits into the bigger picture in what a web app is made of.
The problem with asking nicely
You can ask a model in a prompt: “Reply only in JSON”. It often works. But “often” isn’t good enough in a program. Common failures include:
- Extra words before or after the JSON.
- Code fences or comments around it.
- A missing comma or quote, which makes the whole thing invalid.
- A field with the wrong name or type.
- A field missing altogether.
Any of these will make your code throw an error. If your tool runs unattended, you’d find out when a customer does.
What JSON mode does
As announced, JSON mode is a setting in the API that makes the model produce valid JSON. In OpenAI’s description, you turn it on in the request, and you also need to mention JSON in your instructions, so the model knows what you want. It was introduced for the newer models announced at DevDay.
The key point to understand is what it guarantees and what it doesn’t.
| It helps with | It does not guarantee |
|---|---|
| The output is syntactically valid JSON that code can parse | That the fields have the names you wanted |
| No chatty text around the JSON | That the values are correct or true |
| Fewer broken-bracket failures | That every field you asked for is present |
Valid JSON isn’t the same as right JSON. A model can return {"cost": "cheap"} when you wanted {"price": 12.5}, and both are perfectly valid.
Schemas: describing the shape you want
A schema is a description of the shape of data you expect: which fields exist, what type each is, and which are required. You can think of it as a form for the model to fill in.
For the mug, a plain-words schema might be:
name: text, requiredprice: number, requiredin_stock: true or false, requiredcolours: list of text, optional
In the prompt, you can describe this and give an example. The clearer the example, the more consistent the output. That’s the same lesson as in giving AI examples: the fastest way to better output.
Then, importantly, your code checks the result against the schema. Don’t trust, verify. Most programming languages have libraries that validate JSON against a schema in a few lines. If the answer doesn’t match, you can retry, repair it, or flag it.
A simple pattern that works
- Write the schema first, in plain words or as a formal schema, with a short description of each field.
- Include one example output in the prompt.
- Turn on JSON mode if your model supports it, and say “respond in JSON” in your instructions.
- Parse the reply in your code.
- Validate it against your schema: right fields, right types, sensible ranges.
- Handle failure: retry once or twice, then fall back to a safe default or ask a human.
- Log bad outputs, so you can improve the prompt.
A prompt for extraction might look like this:
You extract product details from text. Reply with JSON only, using exactly these fields: name (string), price (number, no currency symbol), in_stock (true or false). If a value isn’t in the text, use null. Do not guess.
That final instruction, “use null, do not guess”, is worth gold. Models want to be helpful, which means they’ll fill in blanks with plausible inventions unless you give them a permitted way to say “I don’t know”. I explained why in AI hallucinations in code: spotting invented functions, and the same instinct applies to data.
How this relates to function calling
If you’ve read about function calling, you’ll see the connection. Function calling lets the model return structured arguments for a function you define. JSON mode is a simpler tool: it makes sure whatever the model writes is valid JSON. Which one to use depends on the job:
- JSON mode suits extraction, classification and formatting, where you want a structured answer.
- Function calling suits cases where the model decides which action to take and with what arguments.
Where it still goes wrong
Truncation. If the reply is cut off because of length limits, the JSON may be incomplete. Leave enough room for the answer, and check why the reply stopped.
Wrong but valid. A confident, well-formed, incorrect value will pass a syntax check. Validate ranges and types, and spot-check the content.
Over-long or deeply nested structures. The more complex the schema, the more room for error. Start small.
Sensitive data. Don’t send private information to an API unless you’ve thought about where it goes. Never put secrets in prompts; see what an API key is and why you should never share it.
Cost and speed. Retries cost money and time. Cache answers where you can; see caching AI answers to save money.
Quick example use cases
- Turn pasted supplier text into product fields for a spreadsheet.
- Classify support emails into categories with a confidence field.
- Extract dates, names and amounts from invoices.
- Convert messy notes into a tidy list of tasks.
- Tag blog posts with topics from a fixed list.
Each is small, repeatable and checkable, which is what makes them good first projects.
Key takeaways
- Models write text, but programs need predictable data.
- JSON mode makes the output valid JSON; it doesn’t make it correct.
- A schema describes the shape you expect, and your code should validate against it.
- Give an example, allow “null” for unknowns, and plan for failures.
Related reading
What’s one messy bit of text in your work that you’d turn into neat data?