New Tool Radar

AI Concepts

How to Get JSON Matching a Schema from the OpenAI API

Use OpenAI Structured Outputs to get JSON that always matches your schema, instead of asking the model nicely and parsing whatever comes back.

Schematic illustration with three step boxes: write the JSON Schema, set the response format, and parse without retries.

Structured Outputs constrains the model’s completed, non-refusal output to your supplied JSON Schema. It is available in two forms: through function calling, and through a json_schema response format. OpenAI recommends always using Structured Outputs instead of JSON mode when possible, because JSON mode only guarantees valid JSON, while Structured Outputs guarantees the shape as well.

Before you start

An OpenAI API key and a model that supports Structured Outputs, such as gpt-4o-2024-08-06 or later, plus a schema for the data you need.

Write the JSON Schema

Define the object with its properties, types and required keys. Keep the nesting shallow and use enums for closed sets of values, such as order status, so the model cannot invent a new value.

Set additionalProperties to false on every object and type optional fields as string or null, which are strict-mode requirements. A schema that breaks these rules will be rejected by the API.

Set the response format

Send the schema as the response_format with type json_schema, or as a function definition when the model should call a tool. Do not also beg for JSON in the prompt: the format is enforced by the API, not by the wording.

Use the response_format form when you want the model to answer the user in the schema shape, and function calling when you want the model to trigger an action in your system.

Parse without retries

Parse the reply with a plain JSON parser and validate the values, not just the shape. Shape is guaranteed, but meaning is not: a model can return a valid age of minus 5, so check ranges and enums in your own code.

Log the raw string for the first hundred calls. If a parse ever fails, the log shows whether the schema or the model is at fault.

Practice check: fictional Maple Corner order lookup, 20 calls

The table below is fictional practice material, worked out by hand from the stated schema rules. It is the expected result for the practice calls, not a run of any API.

Check Result Verdict
Valid JSON returned 20 of 20 Pass
Required keys present 20 of 20 Pass
Enum values valid 20 of 20 Pass
Values in range 19 of 20 Fail – one age of minus 5

Shape checks pass 20 of 20 because the schema is enforced, but one value fails the range check. Structured Outputs guarantees shape, not sense, so value validation stays in your code.

Expected result. UNTESTED: the counts in the practice table are worked out by hand, not measured by running the OpenAI API. Send your own schema with the json_schema response format, parse 20 replies, and check shape and values separately.

Limits

  • Structured Outputs is only supported on recent model snapshots; older models may need JSON mode instead.
  • The model can still refuse, returning a refusal instead of JSON, so handle that case in your code.

Sources

See all