Strict structured outputs make a model emit JSON that matches a schema you supply. JSON mode guarantees only valid JSON, and tool calling without a strict flag gives weaker guarantees.

Structured output / JSON mode / function schemasDiagram with these labels, in reading order: Input JSON Schema; Constrained Decoding; Valid JSON Output; Tool/Function Calling; Programmatic Consumer.Input JSON SchemaConstrained DecodingValid JSON OutputTool/Function CallingProgrammatic Consumer
Flow of Structured Output Generation

Constrained decoding masks invalid tokens

Structured output starts with a JSON Schema that defines the expected structure and types. In strict modes the model uses constrained decoding: at each step, tokens that would produce invalid JSON are masked and cannot be chosen. If the schema requires an integer, the model cannot emit a string in that position.

The check happens at generation time. A typed API contract is enforced at compile time, which is the closest software analogue, but the model still picks the values probabilistically. A response can match the schema and still carry a wrong number.

  1. Define the JSON Schema for the output.
  2. Pass it to the model in a strict structured-output mode. Providers support only a subset of JSON Schema.
  3. Decoding masks any token that would break the schema.
  4. Your code receives data in the schema's shape.

Function calling fills tool arguments from the schema

Function calling builds on the same mechanism. The model picks a tool based on the conversation, then fills in its arguments according to the tool's schema. If a function expects a date string and an integer, the arguments are generated to those types. With strict schemas this lowers the chance of type mismatches reaching your code.

Why a "please return JSON" prompt fails

The obvious approach is a prompt that asks for JSON. The model may append trailing prose, wrap the answer in markdown fences, or leave a misplaced comma, and any of these makes a parser fail. Teams then add retry loops to repair the output.

Schema-constrained decoding removes these syntactic failures at the source for ordinary output. Two cases still need handling: a refusal, which may not follow your schema, and output cut off at the token limit, which leaves incomplete JSON. Without enforcement you handle malformed output on every code path that reads the response.

Validate values even when structure is guaranteed

Use strict output whenever the consumer is code: an API client, or an agent that needs exact formats. Use free text for chat, drafting and explanations. Even with a guaranteed structure, validate the values in your own code before acting on them, especially when they drive a tool call.

Check the values Without schema enforcement, JSON output can break your parser. With it, the shape is guaranteed but the content is not.

Example scenario: 25% of responses fail to parse

Suppose a service sees invalid JSON in 25% of model responses (an illustrative figure), and data processing fails on each one. Typical causes are strings where integers belong, or extra fields the schema does not define. Ambiguous prompts make both more likely when the schema is not enforced.

Structured output / JSON mode / function schemasDiagram with these labels, in reading order: Invalid JSON Output Rate; Schema Validation Failure; Function Call Errors; Tool Configuration Check; Input Data Quality Check; Update Function Schema; Revise Tool Settings.Invalid JSON Output RateSchema Validation FailureFunction Call ErrorsTool Configuration CheckInput Data Quality CheckUpdate Function SchemaRevise Tool Settings
Debugging JSON output issues
  • Read the logs for JSON validation errors and count how often each type occurs.
  • Review the prompts sent in the failing calls for ambiguity.
  • Replay controlled inputs known to produce valid output.
The call Make the prompt explicit about the expected structure, and move to a strict mode if the model supports it. Monitor the validation error rate and error types so a regression shows up early.

Back to the knowledge baseRequest an assessment