The editor of Downcodes will take you to understand the latest function of OpenAI API - structured output! This exciting new feature is designed to make the results generated by the model strictly follow the JSON schema provided by the developer, significantly improving the reliability of API output. In the past, developers often needed to use additional tools to ensure that the output conforms to the expected format. Now, the structured output function directly solves this problem at the model level, making development more efficient and convenient. This update not only improves the reliability of the model in generating valid JSON output, but also makes it easier to build reliable applications based on the OpenAI API.
OpenAI welcomes an exciting new feature - Structured Outputs, which is designed to enable the output generated by the model to strictly follow the JSON schema provided by the developer.
At last year's Developer Day (DevDay), OpenAI launched the JSON schema for the first time, which provides an important foundation for developers to build reliable applications. While JSON schemas improve the reliability of the model in producing valid JSON output, they do not ensure that the model's responses conform to a specific schema. Today's structured output function is designed to solve this problem.
As AI becomes more widely used in applications, generating structured data from unstructured input has become a core use case. Developers use the OpenAI API to build powerful assistants that can obtain data through function calls, answer questions, and extract structured data for data entry. In the past, developers often needed to use open source tools, repeated requests, etc. to ensure that the output of the model conforms to the format required by the system. Now, the structured output function greatly improves the reliability of the output by constraining the OpenAI model to match the pattern provided by the developer.
Complex JSON schema structured output score 100%
It is worth mentioning that in the evaluation of complex JSON schema, the new model gpt-4o-2024-08-06 received a perfect 100% score in structured output, while the previous gpt-4-0613 scored less than 40%. This means that the new model is better at generating output that fits a specific pattern.
How to use structured output
Using structured output is very simple. OpenAI has introduced two forms for the API: one is a function call through the tool, and the other is a new option for response format. Developers can provide JSON schema through the json_schema parameter. When strict mode is enabled, the model's output will strictly conform to the provided mode.
1. Function call: Structured output through tools
can be obtained by setting strict: true
in the function definition. This feature is available on all models that support the tool, including all models gpt-4-0613
and gpt-3.5-turbo-0613
and higher. When structured output is enabled, the model output will match the provided tool definition.
2. New option for 2.response_format
parameter: Developers can now provide JSON schema through json_schema
, which is a new option for response_format
parameter. This is useful when the model is not calling a tool, but responding to the user in a structured way. This feature is available for our latest GPT-4o models: gpt-4o-2024-08-06
and gpt-4o-mini-2024-07-18
released today. When response_format
is provided with strict: true
, the model output will match the provided schema.
Security is a top priority for OpenAI, new features will operate under existing security policies, and models can still reject unsafe requests. At the same time, in order to simplify the development process, we have added a rejection string value to help developers programmatically detect whether the model generates rejection information.
Native SDK support
OpenAI's Python and Node SDKs have also been updated to support native functionality for structured output. Developers only need to provide Pydantic or Zod objects, and OpenAI's SDK will automatically handle data type conversion and parsing of JSON responses.
Today’s press conference allows everyone to see that structured output will bring more convenience to developers’ work and help them create excellent applications more easily.
Official blog: https://openai.com/index/introducing-structured-outputs-in-the-api/
Highlight:
The structured output function is online, and the model output is more reliable and follows the JSON schema provided by the developer.
The new model gpt-4o-2024-08-06 achieved a perfect 100% score in the evaluation of complex JSON schemas.
The Python and Node SDKs have been updated to support structured output, simplifying the developer's workflow.
All in all, OpenAI's structured output function brings great convenience to developers and improves the reliability and efficiency of API usage. With more precise JSON schema control, developers can more easily build powerful AI applications. The editor of Downcodes recommends that you visit the official blog to learn more details and experience this exciting new feature!