OpenAI has introduced an exciting new feature – Structured Outputs, designed to ensure that the model's generated outputs strictly adhere to JSON schemas provided by developers.

image.png

At last year's DevDay, OpenAI first introduced the JSON mode, which laid an important foundation for developers to build reliable applications. While JSON mode enhanced the reliability of the model generating valid JSON outputs, it did not ensure that the model's responses conformed to specific patterns. The Structured Outputs feature introduced today addresses this issue.

With the widespread application of AI, generating structured data from unstructured inputs has become a core use case. Developers use the OpenAI API to build powerful assistants that can fetch data through function calls, answer questions, and extract structured data for data entry. Previously, developers often had to rely on open-source tools and repeated requests to ensure the model's outputs matched the required formats. Now, the Structured Outputs feature significantly enhances the reliability of outputs by constraining OpenAI models to match the patterns provided by developers.

Complex JSON Schema Structured Output Score: 100%

It is worth noting that in evaluations against complex JSON schemas, the new model gpt-4o-2024-08-06 achieved a perfect score of 100%, while the previous gpt-4-0613 scored less than 40%. This indicates that the new model performs better in generating outputs that conform to specific patterns.

image.png

How to Use Structured Outputs

Using Structured Outputs is straightforward. OpenAI has introduced two forms for the API: one through function calls with tools, and another as a new option for response formats, where developers can provide JSON schemas via the json_schema parameter. When strict mode is enabled, the model's outputs will strictly conform to the provided schema.

1. Function Call: By setting strict: true in the function definition, structured outputs can be obtained through tools. This feature is available for all models that support tools, including all versions of gpt-4-0613 and gpt-3.5-turbo-0613 and later. After enabling structured outputs, the model's outputs will match the provided tool definitions.

2.response_format new option: Developers can now provide JSON schemas through the json_schema parameter, which is a new option for the response_format parameter. This is useful when the model responds to users in a structured manner rather than through tool calls. This feature is available for our latest GPT-4o models: the newly released gpt-4o-2024-08-06 and gpt-4o-mini-2024-07-18. When response_format is provided along with strict: true, the model's outputs will match the provided schema.

Security is a top priority for OpenAI, and the new feature will operate under existing safety policies, with models still able to reject unsafe requests. Additionally, to simplify the development process, we have added a rejection string value to help developers programmatically detect if the model has generated a rejection message.

Native SDK Support

OpenAI's Python and Node SDKs have also been updated to natively support structured outputs. Developers simply need to provide Pydantic or Zod objects, and OpenAI's SDKs will automatically handle data type conversions and parsing of JSON responses.

Today's announcement shows that Structured Outputs will bring more convenience to developers' work, helping them create outstanding applications more easily.

Official Blog: https://openai.com/index/introducing-structured-outputs-in-the-api/

Key Points:

🌟 Structured Outputs feature launched, making model outputs more reliable and conforming to JSON schemas provided by developers.

🔍 New model gpt-4o-2024-08-06 achieved a perfect score of 100% in evaluations against complex JSON schemas.

🔧 Python and Node SDKs updated to support structured outputs, simplifying the developer workflow.