Building with AI

JSON mode

4 min readintermediateUpdated 28 Sept 2026
1 · In one line

JSON mode is an API setting that asks a language model to reply in JSON, text a program can parse (read into data), and on some providers, such as OpenAI, enforces valid syntax, without fixing which keys or types appear.

1 · What it is

JSON mode is a switch on a model’s API, the doorway programs use to reach it. With the switch on, the model is pushed to reply in JSON. JSON, short for JavaScript Object Notation, is a way of writing data as plain text with strict rules: labels and values in pairs, wrapped in curly braces. Think of a form where every box has a label and an answer.

OpenAI’s Chat Completions API sets response_format to json_object, and Mistral uses the same value. Gemini on Google Cloud asks for the MIME type application/json. Ollama takes format: "json".

One way to keep the reply inside the rules is a grammar. A grammar is a set of syntax rules the output has to follow. It stops the model from adding a chatty “Sure, here you go!” before the opening brace.

Mistral’s own example shows where this stops. The prompt asks for “the name and the ingredients”. Yet the reply files Boeuf Bourguignon under the key meal. The syntax was correct, but the model picked the labels itself.

Two catches are worth knowing. First, OpenAI rejects a JSON-mode request if the messages never mention JSON. Without that nudge, a model may keep sending blank spaces, which JSON allows, until it hits its token limit, the most chunks of text it may send. Second, the promises differ by company. Google Cloud’s Gemini docs call JSON mode without a schema only a strong hint.

OpenAI calls structured outputs the next step after JSON mode and recommends them where possible. They add a schema: a declared list of the fields a reply must have and the type of each one.

2 · Why it exists

Programs need replies they can parse, or read into data, and free text often breaks the parser.

Extra wordsA friendly opening line such as "Sure, here it is" is not JSON, so the parser fails on it.
Broken syntaxWithout an output constraint, a reply can be malformed JSON that breaks the calling program.
Retry costsEvery failure means catching the error and asking again, which means extra error-handling code and retries.
3 · How it works

Follow one request for a French meal from prompt to parsed object.

Where enforced, the gate covers syntax, not shape: the model still chose the key meal when the prompt said name.
  1. 1 · askThe app sets the response format to JSON and also asks for JSON in the prompt.
  2. 2 · generateThe model writes its reply, steered toward JSON.
  3. 3 · constrainThe output is held to JSON syntax, for example by a grammar, a set of rules that only lets valid JSON through.
  4. 4 · checkThe app parses the reply, then validates keys and types itself and retries if they are wrong.

JSON mode promises text that parses, not an object with the fields your code expects.

4 · Where it's used
WhoWhat they askWhat it works with
Data pipeline“Pull the product name and price from this listing”A JSON object, checked against the expected keys afterwards
Local model user“Describe this country as JSON”Ollama's format field set to json
Chat backend“Return a reply the frontend can read without text-matching tricks”A response format of type json_object
Older integration“Get parseable output from a model without schema support”JSON mode plus a validation library and retries
5 · What it solves, and what it doesn't
solves
  • Replies parse as JSON on supported providers, apart from edge cases the app must detect.
  • Chatty words before the data are ruled out when syntax is enforced, because a JSON text must be a single value.
doesn't solve
  • It does not guarantee any particular keys, types or required fields.
  • A reply can be cut off, leaving incomplete JSON the app must catch.
  • It does not check that the values inside are true.
  • On some platforms it is only a strong hint, so malformed output is still possible.
6 · Go deeper

Sources used

This explainer is written in original language. The links below support its factual claims.

  1. docsStructured model outputs, OpenAI · read 28 Sept 2026
  2. docsJSON Mode, Mistral AI · read 28 Sept 2026
  3. docsStructured output, Google Cloud · read 28 Sept 2026
  4. docsStructured Outputs, Ollama · read 28 Sept 2026
  5. repoGBNF Guide (llama.cpp grammars README), ggml-org · read 28 Sept 2026
  6. officialRFC 8259: The JavaScript Object Notation (JSON) Data Interchange Format, IETF / RFC Editor · read 28 Sept 2026
  7. docsStructured outputs, Anthropic · read 28 Sept 2026