LLM Structured Outputs: JSON Mode, Schemas and Reliable Function Calling

LLM structured outputs force the model to return JSON matching your schema. JSON mode vs strict schemas vs function calling, and each provider's rules.

By Galactic News Editorial · Published 2026-11-16T10:36:33+00:00 · Updated 2026-11-16T10:36:33+00:00

LLM structured outputs: JSON mode, schemas and reliable function calling

LLM Structured Outputs: JSON Mode, Schemas and Reliable Function Calling

Asking a language model for JSON in plain text works until it does not. A missing comma, an extra key, or a value in the wrong type breaks the parser, and the code downstream fails at 2 a.m. Structured outputs fix this at the API level: the provider constrains the model's token sampling so the reply can only match the schema supplied. This tutorial covers the three mechanisms — JSON mode, schema-constrained outputs, and strict function calling — and how to use each reliably.

LLM Structured Outputs: Three Mechanisms, Three Promises

MechanismWhat it guaranteesWhen to use it
JSON modeValid JSON, nothing moreQuick prototypes, models with no better option
Structured outputsReply matches the supplied JSON SchemaData extraction, classification, routing
Strict function callingTool arguments match the function schemaWhen the model must trigger an action

The three mechanisms share schema machinery but do different jobs. JSON mode only promises parseable JSON. Structured outputs promise the shape of the reply. Strict function calling promises the shape of the arguments the model hands to code. OpenAI's docs recommend structured outputs over JSON mode whenever possible, and the same advice holds on the other providers.

JSON Mode: Quick, Valid, Loose

JSON mode is the simplest option. On OpenAI, set response_format to type json_object and the model returns valid JSON. On Gemini, setting response_mime_type to application/json without a schema acts as a strong hint, but Google's own docs warn it carries a small risk of malformed JSON. JSON mode guarantees nothing about the shape: keys can be missing, types can be wrong, and an enum value can be invented. Any code that consumes JSON mode output needs its own schema validation and a retry path. Use it for prototypes and for models that offer nothing better. Do not use it in production paths where a bad shape breaks a database write.

Structured Outputs With a Schema

Structured outputs attach a JSON Schema to the request, and the provider compiles it into a grammar that constrains decoding. The model physically cannot emit a token that violates the schema. On OpenAI's Chat Completions API, the field is response_format with type json_schema, a name, strict true, and the schema. On the newer Responses API, the same block lives under text.format. On Claude, the field is output_config.format with type json_schema, generally available since February 2026. On Gemini, set response_mime_type to application/json and supply a response_schema, or use the newer Interactions API with a response_format object.

Step 1: Write the schema as plain JSON Schema. Keep it flat where possible: objects, arrays, strings, numbers, booleans, and enums.

Step 2: Mark every property as required. On OpenAI's strict mode there is no optional field; a value that might be missing should be a nullable union type instead.

Step 3: Set additionalProperties to false on every object, so the model cannot invent extra keys.

Step 4: Keep the root an object. A top-level array or union is rejected in strict mode; wrap it in an object with a single array property.

Step 5: Check the schema against the provider's supported subset before shipping. OpenAI rejects unsupported keywords outright with strict true. Claude rejects numeric and string-length constraints such as minimum and maxLength. Gemini documents minimum, maximum, minItems, and maxItems as supported, and does not require every field.

Step 6: Parse the reply with a normal JSON parser and validate the values. The shape is guaranteed. The truth of the values is not.

Reliable Function Calling With Strict Mode

Function calling is a different job from shaping a reply. The model does not run the function. It returns the function's name and arguments, and the developer's code executes the call and feeds the result back. Without strict mode, the arguments are best effort: the model usually follows the schema, and sometimes it does not. With strict true on the tool definition, OpenAI constrains the arguments to the schema, and OpenAI's docs recommend always enabling it. Claude's equivalent is strict true on the tool definition, which guarantees the input object matches the input schema. A single request can carry both strict tools and a schema-constrained reply.

Choose by the job. If the model needs to trigger an action — look up an order, send a message, query a database — use function calling. If the code needs data back in a fixed shape — classify a ticket, extract fields from free text — use a structured response format. Mixing the two in one call is common: the model calls a tool to fetch data, then returns the final answer in a schema.

Keep schemas small. OpenAI caps a strict schema at roughly 5,000 properties total, 10 levels of nesting, and 1,000 enum values. Claude caps a request at 20 strict tools, 24 optional parameters across every strict schema, and 16 parameters using anyOf or nullable unions. Large schemas also cost more: schema tokens bill as input at normal rates. A schema with hundreds of properties is usually a sign the job should be split into smaller calls.

What Still Breaks — and How to Guard It

Structured outputs guarantee shape, not values. A schema that says a lead score is an integer between 0 and 100 can still return a plausible but wrong number. Validate values in code: ranges, enums, and cross-field rules such as a total equaling quantity times price. The schema cannot check arithmetic.

Refusals and truncation break the guarantee too. A safety refusal returns a refusal object instead of the schema, and a reply cut short by max_tokens returns partial JSON. Both arrive as normal HTTP 200 responses, so the code must detect them. Check for a refusal field before parsing, and treat a truncated reply as a failure to retry with a larger token budget.

Schema drift is the quietest failure. A provider changes a supported keyword, a model version drops support, or a copy of the schema in another service goes stale. Keep one canonical schema per job, version it, and test it against the live API in continuous integration. A schema that validated last month can be rejected today if the provider tightened its subset.

Bottom Line

Use JSON mode only when nothing better exists. Use structured outputs — response_format on OpenAI, output_config.format on Claude, response_schema on Gemini — whenever code consumes the reply. Use strict function calling whenever the model triggers an action. Keep every field required, set additionalProperties to false, and keep the root an object. Then validate the values anyway, because the guarantee covers shape, not truth. A pipeline that treats a schema-shaped reply as verified data is one bad value away from a corrupted database.