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.
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.
- Define the JSON Schema for the output.
- Pass it to the model in a strict structured-output mode. Providers support only a subset of JSON Schema.
- Decoding masks any token that would break the schema.
- 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.
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.
- 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.