Structured Outputs

Learn how to generate structured data with your text models

Some models have the ability to generate structured data. This allows you to receive a response using a JSON schema that you have defined, which can be useful for a wide range of use cases. Such as parsing data from an image, generating a list of items, or extracting information from a document.

Introduction

JSON is one of the most popular data formats to exchange data between systems. It's a lightweight format that's easy to understand and parse.

Structured Outputs is a feature that ensures the model's response will always be in your provided JSON Schema. This prevents the model from forgetting a key or hallucinating an enum value.

Some benefits of Structured Outputs include:

  • Reliable type-safety: You can be sure that the response will match the JSON Schema you provided.
  • Simpler prompting: No need for long and complicated prompts to achieve consistent formatting.

We recommend using OpenAI's SDK for Python and JavaScript. Their SDK makes it easy to define object schemas using Pydantic and Zod. Below is an example on how to extract information from unstructured text that conforms to a JSON Schema.

Function Calling vs response_format

You can use Structured Outputs by using function calling or by providing a JSON Schema. Function calling is useful when you want the model to be able to interact with your application.

For example, using function calling you can give the model access to functions that query a database in order to build a chatbot that can help users place orders or look up information.

However, Structured Outputs via response_format is more suitable when you want to always return a specific structured schema.

For example, if you are creating a calendar app, you can use Structured Outputs via response_format to always return a JSON Schema that matches the CalendarEvent object.

To simplify, if you want the model to use tools, function, data, etc. in your system, then you should use function calling. Instead, if you want to always return a specific structured schema, then you should use Structured Outputs by using the response_format.

The remainder of this guide will focus on Structured Outputs via response_format. If you want to learn more about function calling, you can do so here.

Examples

Supported types

Next steps