EN

Language

Our editions

Written for each audience, not translated word for word.

Structured outputs: getting valid JSON from a language model

ExplainerBy Lucas Matías García TorresUpdated Data checked 5 min read

Reviewed by Lucas Matías García Torres on . How we work.

Leer en español

A data center aisle with streams of small orange data packets flowing between server racks

Short answer

Structured outputs, on October 23, 2026, make a model return JSON that matches a schema you define. OpenAI, Anthropic and Google enforce the schema while generating (constrained decoding), and Ollama accepts a schema locally. Each API supports only a subset of JSON Schema: OpenAI requires every field to be required and additionalProperties false, Anthropic allows 24 optional parameters per request, and Gemini ignores minLength and maxLength. A valid shape does not guarantee correct values.

Prices and limits change often: check the vendor's page before you pay. How we make money.

Structured outputs make a language model return JSON that matches a schema you define, so your code can parse every answer without retries. On October 23, 2026, OpenAI, Anthropic, Google and Ollama all support it, and the three cloud APIs enforce the schema during generation (constrained decoding) rather than just asking nicely in the prompt. This guide explains how structured outputs differ from the older JSON mode, how each API turns them on, which parts of JSON Schema they accept and the limits that still break real projects, based on each vendor's documentation read on October 23, 2026.

JSON mode is not the same thing

JSON mode only promises valid JSON. Structured outputs promise JSON that follows your schema: every required key present, every enum value allowed, every type correct. OpenAI describes structured outputs as the evolution of JSON mode and recommends always using them when possible. JSON mode also has a trap: OpenAI warns that if you forget to tell the model to produce JSON, it may emit an unending stream of whitespace until it hits the token limit.

The difference matters in production. Without a schema, a model can skip a field, invent a value outside your list or wrap the JSON in prose, and every one of those breaks a parser. With constrained decoding, the model can only choose tokens that keep the output valid against the schema. Anthropic describes the result as no more JSON.parse() errors and no retries for schema violations.

How to turn it on in each API

Table: structured outputs by vendor (official documentation, checked October 23, 2026)

Item OpenAI Anthropic (Claude) Google (Gemini API) Ollama (local)
Parameter text.format with type: "json_schema" and strict: true output_config.format with type: "json_schema" response_format with mime_type: "application/json" and a schema format with a JSON schema (or "json")
Enforcement constrained to the schema constrained decoding with a compiled grammar constrained decoding schema passed to the model
SDK helpers Pydantic (Python), Zod (JavaScript), Ruby Pydantic, Zod, C#, Go, Java, PHP, Ruby Pydantic, Zod Pydantic, Zod
For tool inputs strict function calling strict tool use (strict: true) function calling not stated

For tool calls the same idea applies to the arguments: OpenAI's strict mode in function calling and Anthropic's strict tool use guarantee that the inputs match the tool's schema. Our explainer on function calling covers when the model should call a tool instead of answering in a fixed format. Google's own rule of thumb is short: structured outputs format the final answer, function calling takes an action before the answer.

Locally, Ollama accepts a JSON schema in its format field and recommends also putting the schema in the prompt and lowering the temperature to 0. Its documentation notes that Ollama's cloud does not support structured outputs. If you run models on your own machine, our guide to running an LLM locally covers the setup.

What JSON Schema features are allowed

None of the vendors accepts the full JSON Schema standard. The rules that trip people up most:

  • Every field required (OpenAI). All fields must be listed in required. To make one optional, give it a union type with null.
  • additionalProperties: false. OpenAI requires it on every object, and Anthropic only accepts false.
  • Root object. OpenAI requires the root to be an object, not an anyOf, which is what a Zod discriminated union produces.
  • Recursion. OpenAI supports recursive schemas; Anthropic lists recursive schemas as not supported.
  • Ignored constraints. Gemini strips minLength, maxLength, minProperties, maxProperties and uniqueItems instead of enforcing them; Anthropic's SDK helpers move unsupported constraints such as minimum into the field description and validate them in your code.

Size limits differ too. OpenAI allows up to 5,000 object properties with up to 10 levels of nesting and up to 1,000 enum values per schema. Anthropic limits each request to 20 strict tools, 24 optional parameters and 16 parameters with union types across all strict schemas, and answers "Schema is too complex for compilation" when the grammar would be too large. Gemini rejects very large or deeply nested schemas with a 400 INVALID_ARGUMENT error.

Latency, caching and cost

The schema has to be compiled the first time. OpenAI says the first request with a new schema has extra latency and later ones do not. Anthropic caches compiled grammars for 24 hours from last use and invalidates the cache when the schema structure changes, but not when only names or descriptions change. Changing the output format on Claude also invalidates the prompt cache for that conversation. There is no separate price: the JSON is billed as ordinary output tokens, and OpenAI lists structured outputs as supported in its Batch API, so the 50% discount of the batch API applies.

What can still go wrong

  • Refusals. If the model declines for safety reasons, OpenAI returns a separate refusal field and Claude sets stop_reason: "refusal"; in both cases the output may not match your schema.
  • Truncation. If max_tokens runs out, the JSON is incomplete. Raise the limit and retry.
  • Wrong but valid. The schema guarantees the shape, not the truth. OpenAI notes that when the input has nothing to do with the schema, the model may fill it with invented values, and Google tells you to validate values in your application. Our guide to model hallucinations explains why.
  • Enum casing. Anthropic warns that Claude may return an enum value that differs only in capitalization. Compare enum values case-insensitively.
  • Incompatible features. On Claude, structured outputs cannot be combined with citations (400 error) or with prefilling the assistant message.

A setup that holds up

Define the schema once in code with Pydantic or Zod and let the SDK generate the JSON Schema, as OpenAI recommends to avoid the two drifting apart. Keep the schema flat, make fields required wherever a default exists, describe each field in one plain sentence and tell the model what to return when the input does not fit, for example an empty list. Then validate the parsed values in your own code, because a valid shape is only half of a correct answer.

What we checked

  • OpenAI Structured Outputs ensure responses adhere to the supplied JSON Schema; they are the evolution of JSON mode, which only ensures valid JSON, and OpenAI recommends always using Structured Outputs when possible; JSON mode without an explicit instruction may produce an unending stream of whitespace; all fields must be required, optional fields are emulated with a null union; additionalProperties false is required; the root must be an object and not anyOf; recursive schemas are supported; up to 5,000 object properties and 10 levels of nesting, up to 1,000 enum values; the first request with a schema has extra latency; refusals are returned in a refusal field; supported in the Responses, Chat Completions, Assistants, Fine-tuning and Batch APIs; native SDK schema helpers are recommended to avoid divergence. (OpenAI, )
  • Anthropic structured outputs: JSON outputs via output_config.format and strict tool use via strict: true; constrained decoding with compiled grammars; first-request compilation latency; grammars cached 24 hours from last use, invalidated by schema structure changes but not by name or description changes; changing output_config.format invalidates the prompt cache; recursive schemas and additionalProperties other than false not supported; limits of 20 strict tools, 24 optional parameters and 16 union-type parameters per request; 'Schema is too complex for compilation' error; refusals set stop_reason refusal; enum capitalization is not guaranteed; incompatible with citations and message prefilling; SDK helpers move unsupported constraints into descriptions and validate responses. (Anthropic, )
  • Gemini API structured outputs: response_format with mime_type application/json and a schema; supported types string, number, integer, boolean, object, array and null; minLength, maxLength, minProperties, maxProperties, uniqueItems, contains, minContains and maxContains are stripped and not enforced, while minItems and maxItems are enforced; very large or deeply nested schemas are rejected with 400 INVALID_ARGUMENT; structured outputs format the final answer while function calling takes actions; combining structured outputs with built-in tools is a preview for Gemini 3 models; validate values in the application. (Google, )
  • Ollama structured outputs: pass a JSON schema (or json) in the format field; also pass the schema in the prompt; lower the temperature to 0 for more deterministic output; works through the OpenAI-compatible API via response_format; Ollama's cloud does not support structured outputs. (Ollama, )

What may change

  • Supported JSON Schema keywords and complexity limits change as vendors update their models and APIs. This page is reviewed quarterly.
  • The setup advice in the last section is our recommendation built on the vendors' guidance, not a vendor requirement.

Frequently asked questions

What are structured outputs?

A feature that makes a language model return JSON matching a schema you provide, with every required key present and every value of the right type. OpenAI, Anthropic and Google enforce the schema during generation, so the output always parses and follows the schema's shape.

What is the difference between JSON mode and structured outputs?

JSON mode only guarantees valid JSON; structured outputs also guarantee that the JSON follows your schema. OpenAI calls structured outputs the evolution of JSON mode and recommends using them whenever possible, and warns that JSON mode without an explicit instruction can produce endless whitespace.

Do structured outputs cost extra?

There is no separate fee: the JSON is billed as normal output tokens. The first request with a new schema is slower while it compiles; Anthropic caches compiled schemas for 24 hours from last use, and OpenAI supports structured outputs in its Batch API at the batch discount.

Why does my schema return a 400 error?

Because it uses an unsupported JSON Schema feature or is too complex. OpenAI requires additionalProperties false and all fields required, Anthropic limits each request to 24 optional parameters and 16 union-type parameters across strict schemas, and Gemini rejects very large or deeply nested schemas.

Can structured outputs still be wrong?

Yes. They guarantee the format, not the facts. A model can fill a valid schema with invented values, especially if the input does not fit the task, and a refusal or a token limit can leave the output incomplete. Validate the parsed values in your own code.

Sources

  1. Structured model outputs (OpenAI API guide): JSON Schema adherence, JSON mode, supported schemas, limits and refusals, OpenAI. Accessed October 23, 2026.
  2. Structured outputs with Claude: JSON outputs, strict tool use, grammar caching, schema limits and incompatibilities, Anthropic. Accessed October 23, 2026.
  3. Structured outputs with the Gemini API: JSON Schema support, unenforced constraints and limitations, Google. Accessed October 23, 2026.
  4. Structured Outputs (Ollama documentation): the format field, schemas and tips, Ollama. Accessed October 23, 2026.

Spotted an error or an outdated price? Tell us and we will fix it.

Change history

  • : First published. Checked against OpenAI's structured outputs guide, Anthropic's structured outputs page, Google's Gemini structured output guide and Ollama's structured outputs documentation on October 23, 2026.

Next review: .

Part of our LLM APIs & Costs guide.

Other topics