JSON mode

JSON mode is an API setting that makes a language model reply with syntactically valid JSON, though, unlike structured outputs, it does not guarantee the JSON follows any particular schema.

Software that uses a language model often needs data it can parse, not prose. JSON, a common text format of named fields and lists, is the usual choice. Before dedicated settings existed, developers asked for JSON in the prompt and hoped: models would wrap it in chatty explanation, drop a closing bracket or add a comment, and the program reading it would fail. JSON mode constrains the reply so that it always parses as JSON.

OpenAI introduced JSON mode at its DevDay event on November 6, 2023 for GPT-4 Turbo and an updated GPT-3.5 Turbo, switched on through a new response_format parameter. Its current documentation lists the setting as the json_object format.

Valid JSON is not the same as the right JSON. A model in JSON mode can still return fields with different names, leave out a required field or put text where a number belongs. In August 2024 OpenAI noted that JSON mode does not guarantee a reply will conform to a schema, and launched Structured Outputs to fix that. It works by constrained decoding: the developer's schema is turned into a grammar and, at each step, tokens that would break it are ruled out. On OpenAI's test of following complex schemas, gpt-4o-2024-08-06 with Structured Outputs scored 100%, while gpt-4-0613 scored under 40%.

Take an invoice-reading tool. In JSON mode it will reliably return a parseable object, but one invoice might come back with total and another with amount_due. With a schema enforced, every reply carries the same named fields in the expected types.

OpenAI now recommends Structured Outputs over JSON mode wherever possible. Other providers offer similar options, and Google's documentation for its Gemini API advises developers to validate the values in their application even when the JSON is syntactically correct. That advice holds for every provider: a guaranteed format does not guarantee a correct answer.

Common questions

What is JSON mode?

It is a setting in some language model APIs that forces the model's reply to be valid JSON. OpenAI introduced it in November 2023. It ensures the output can be parsed, but not that it contains the fields or types an application expects.

What is the difference between JSON mode and structured outputs?

JSON mode guarantees valid JSON syntax only. Structured outputs also guarantee that the reply matches a schema the developer supplies, with the right field names and types. OpenAI recommends structured outputs whenever a model supports them.

How is JSON mode different from function calling?

JSON mode shapes the whole reply as JSON for the application to use as data. Function calling has the model choose a function and provide arguments that the application then runs. Both produce structured output, but they serve different purposes.

Does JSON mode make the data correct?

No. It guarantees the reply parses as JSON, not that the values inside are accurate or complete. Applications should still validate fields and check the content, just as they would with any other input.

Finding more

Search for "JSON mode", "structured outputs" or "constrained decoding" with the name of your model provider. The providers' API documentation is the authoritative source on which models support which settings and what each guarantees. Engineering write-ups on constrained decoding explain how schema enforcement works under the hood.

Working out what this means for your business?

Aileron Shift builds the systems behind this site: AI newsrooms that research, write and publish on their own, marketing automation and WordPress hosting for companies that need to move faster than their competitors.