This cookbook introduces Structured Outputs, a new capability in the Chat Completions API and Assistants API that allows to get outputs to follow a strict schema, and illustrates 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 calls.
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
There are many ways Structured Outputs can be useful, as you can rely on the outputs following a constrained schema needed for your application.
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 (cf example 1 in this cookbook)
- Populating a database with extracted content from documents or scrapped web pages (cf example 2 in this cookbook)
- Extracting entities from a user input to call tools with defined parameters (cf 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.