Structured Outputs is a new capability in the Chat Completions API and Assistants API that guarantees the model will always generate responses that adhere to your supplied JSON Schema. In this cookbook, we will illustrate this capability with a few examples.
Structured Outputs can be enabled by setting the parameter strict: true
in an API call with either a defined response format or function definitions.
Response format usage
Previously, the response_format
parameter was only available to specify that the model should return a valid JSON.
In addition to this, we are introducing a new way of specifying which JSON schema to follow.
Function call usage
Function calling remains similar, but with the new parameter strict: true
, you can now ensure that the schema provided for the functions is strictly followed.
Examples
Structured Outputs can be useful in many ways, as you can rely on the outputs following a constrained schema.
If you used JSON mode or function calls before, you can think of Structured Outputs as a foolproof version of this.
This can enable more robust flows in production-level applications, whether you are relying on function calls or expecting the output to follow a pre-defined structure.
Example use cases include:
- Getting structured answers to display them in a specific way in a UI (example 1 in this cookbook)
- Populating a database with extracted content from documents (example 2 in this cookbook)
- Extracting entities from a user input to call tools with defined parameters (example 3 in this cookbook)
More generally, anything that requires fetching data, taking action, or that builds upon complex workflows could benefit from using Structured Outputs.