OpenAI has announced a significant upgrade to its API with the introduction of Structured Outputs, ensuring model-generated outputs align perfectly with developer-supplied JSON Schemas.
This new feature, first hinted at during last year’s DevDay, builds on the previous JSON mode by guaranteeing that outputs will conform to specified schemas, enhancing the reliability and usability of AI-generated data.
Enhancing AI with Structured Outputs
Generating structured data from unstructured inputs is a core application for AI models. Developers frequently use OpenAI’s API to create powerful assistants capable of data retrieval, answering queries via function calling, extracting structured data, and constructing multi-step workflows. However, ensuring the generated data conforms to necessary formats has historically required extensive workaround methods. Structured Outputs addresses this challenge directly.
According to OpenAI, their latest model, gpt-4o-2024-08-06, achieves a perfect 100% compliance rate with JSON schema following evaluations, a significant improvement over earlier models like gpt-4-0613, which scored below 40%.
Utilizing Structured Outputs
Structured Outputs are available in two primary forms within the API:
- Function Calling: By setting strict compliance within a function definition, developers can ensure that model outputs match the provided tool definitions. This feature is compatible with all models that support tools, including gpt-4-0613 and gpt-3.5-turbo-0613.
- Response Formats: Developers can now provide a JSON Schema via a new response format parameter. This option is especially useful for non-tool-related responses, ensuring structured data output.
Safety and SDK Support
OpenAI ensures that the Structured Outputs feature adheres to existing safety policies, allowing models to refuse unsafe requests. Developers can detect refusals programmatically through a specific value in the API response.
The Python and Node SDKs now support Structured Outputs natively. Developers can use tools within these SDKs to supply schemas, with the SDKs managing the conversion and parsing automatically.
Availability and Benefits
Structured Outputs is generally available today across all models supporting function calling and response formats, including the latest gpt-4o and gpt-4o-mini models. By upgrading to the gpt-4o-2024-08-06 model, developers can achieve significant cost savings on token usage.
For more details on implementing Structured Outputs, developers can refer to the OpenAI documentation.