OpenAI Structured Output API: A Comprehensive Guide for Developers

Updated on Apr 06,2025

OpenAI has recently introduced a significant update to its API, specifically designed to enhance the way developers obtain structured outputs. This new functionality promises more reliable and predictable results, particularly when working with JSON schemas. This article will delve into the details of this update, providing comprehensive insights and practical code examples to help you leverage its capabilities effectively, improving the quality of your AI systems.

Key Points

The new Structured Output API allows developers to define specific JSON schemas for more reliable and type-safe outputs.

Structured outputs ensure responses adhere to predefined JSON schemas, eliminating missing keys or invalid enum values.

Explicit refusals can be detected programmatically, enhancing safety-based model refinements.

Simpler prompting achieves consistent formatting without complex prompts.

Structured Outputs offer a reliable type-safety, reducing the need for validation or incorrect response reattempts.

This update is the evolution of JSON mode, ensure schema adherence are also supported in various OpenAI APIs like Chat Completions, Assistants, Fine-tuning, and Batch.

Implementation examples using Python will be shared for practical understanding.

Understanding how to use structured output effectively can significantly improve your AI projects.

Understanding OpenAI's Structured Output API

What is Structured Output in the OpenAI API?

The Structured Output API is a significant update from OpenAI designed to allow developers to reliably get structured data back from their models. This is particularly useful in AI projects where consistent, predefined formats are crucial. The update focuses on ensuring that model outputs reliably adhere to developer-supplied JSON Schemas.

This means less time spent validating outputs and more time building robust AI applications.

This enhancement builds on previous methods like function calling and JSON mode, addressing their limitations by providing stricter adherence to the defined schemas. For instance, while JSON mode guarantees a valid JSON output, it doesn't enforce the content within the schema, which this new API now ensures. This is a big step toward making AI-driven systems more predictable and reliable. The new model gpt-4o-2024-06-06 is part of this feature and is designed to ensure model-generated outputs will exactly match JSON Schemas that the developers provides.

Key Benefits of Structured Outputs:

  • Reliable Type-Safety: Eliminates the need to re-validate or retry incorrectly formatted responses.
  • Explicit Refusals: Detects safety-based model refusals programmatically, improving model safety.
  • Simpler Prompting: Achieves consistent formatting without the need for complex prompts.

The primary aim is to guarantee that responses conform to a particular schema, giving developers more control and assurance over the data they receive. This is especially important in applications such as Data Extraction, answer generation, and multi-step agent workflows where structured data is critical.

Structured Outputs vs. JSON Mode: What's the Difference?

While both Structured Outputs and JSON Mode ensure valid JSON is produced, Structured Outputs go a step further by enforcing schema adherence.

This means that responses are not only valid JSON but also contain the specific keys, data types, and enum values as predefined in the schema. JSON mode ensures that the output is valid JSON, Structured Outputs guarantee that the content of the JSON also matches your pre-defined expectations.

Here’s a breakdown of the key differences:

  • JSON Mode:
    • Guarantees that the output is a valid JSON.
    • Does not enforce the structure or content of the JSON.
    • May still produce unexpected or invalid data.
  • Structured Outputs:
    • Guarantees that the output is a valid JSON.
    • Enforces adherence to a predefined JSON Schema.
    • Ensures responses contain the correct keys, data types, and enum values.

This makes Structured Outputs particularly useful when you need highly structured and predictable data, such as when integrating AI with existing systems or databases that require specific data formats.

When to Use Structured Outputs?

Structured Outputs are most beneficial in scenarios where data must conform to a specific format.

Consider using Structured Outputs when:

  • You need to extract data with predefined fields (e.g., extracting product information from customer reviews).
  • You require type-safe data to prevent runtime errors (e.g., ensuring numerical values are always numbers).
  • You want to enforce valid enum values to ensure data consistency (e.g., limiting sentiment values to 'positive', 'negative', or 'neutral').
  • Building agents that follow specific instructions and must return data in a certain format. Structured outputs are really effective at keeping a consistent format without really complex prompts.

In essence, if you find yourself repeatedly validating and reformatting AI outputs, Structured Outputs can significantly streamline your workflow and improve data quality.

Function Calling and Structured Outputs

Function calling is another method provided by OpenAI to interact with external APIs or execute specific code snippets based on model responses. Although both function calling and structured outputs provide ways to interact with external tools, there are key differences to consider.

Structured outputs are more focused on data extraction, validation and standardization. They are useful when the goal is to get structured data, not to make models take action.

With Function calling, you specify functions that the model can call. Structured output you define the format you want to have, so it can focus on those key details.

Practical Implementation of Structured Outputs in Python

Now, let's dive into a practical example of how to implement Structured Outputs using Python. To effectively use this API, you'll need the OpenAI Python library and a basic understanding of JSON schemas.

Step 1: Setting up Your Environment

First, ensure you have the OpenAI library installed. If not, you can install it using pip:

pip install openai

Step 2: Defining a JSON Schema

Next, define a JSON schema that represents the structure of the data you want to extract. This schema will guide the model in formatting its response.

json_schema = {
    "type": "object",
    "properties": {
        "message": {
            "type": "string",
            "description": "The extracted message from the text"
        },
        "sentiment": {
            "type": "string",
            "enum": ["positive", "negative", "neutral"],
            "description": "The sentiment of the message"
        },
        "category": {
            "type": "string",
            "description": "The category the message belongs to"
        }
    },
    "required": ["message", "sentiment", "category"]
}

Step 3: Interacting with the OpenAI API

Now, interact with the OpenAI API, specifying the JSON schema in the response_format parameter:

import openai

client = openai.OpenAI(
    api_key="YOUR_API_KEY",
)

def get_structured_response(query, schema):
    response = client.chat.completions.create(
        model="gpt-4o-2024-06-06",
        messages=[{
            "role": "system",
            "content": "You are an AI assistant tasked with extracting information and determining its sentiment and category."
        }, {
            "role": "user",
            "content": query
        }],
        response_format={"type": "json_object", "schema": schema}
    )
    return response.choices[0].message.content

# Example usage
query = "I am having trouble with my order, and the product is damaged."
schema = json_schema

structured_data = get_structured_response(query, schema)
print(structured_data)

This code snippet demonstrates how to set up the API request and retrieve a structured response based on your defined schema. Note that you should replace YOUR_API_KEY with your actual OpenAI API Key. gpt-4o-2024-06-06 is the model that supports structured outputs so you should select the correct one.

Supported Schemas and Data Types

Exploring Supported JSON Schemas

The Structured Output API supports a wide range of JSON schemas, enabling you to define complex data structures. The supported schemas include primitive types such as strings, numbers, booleans, and arrays. You can also define nested objects and enums to enforce specific data constraints.

{
    "type": "object",
    "properties": {
        "product_name": {"type": "string"},
        "price": {"type": "number"},
        "in_stock": {"type": "boolean"},
        "tags": {
            "type": "array",
            "items": {"type": "string"}
        },
        "category": {
            "type": "string",
            "enum": ["electronics", "clothing", "books"]
        }
    },
    "required": ["product_name", "price", "in_stock", "category"]
}

Explanation of the Schema:

  • product_name: A STRING representing the name of the product.
  • price: A number representing the price of the product.
  • in_stock: A boolean value indicating whether the product is in stock.
  • tags: An array of strings representing tags associated with the product.
  • category: A string that must be one of the specified enum values (electronics, clothing, books).

By leveraging these supported schemas, you can effectively tailor the API to extract and validate data according to your exact requirements. It’s important to design your schema thoughtfully to match the data you expect from the model, ensuring the highest quality and reliability of the output.

Step-by-Step Guide: Working with Structured Outputs

How to Define a JSON Schema?

Defining a precise JSON schema is the first and one of the most important steps.

Best Practices for schema definition:

  • Use the correct data types.
  • Provide descriptive descriptions for each field.
  • Use enums to limit values.
  • Mark mandatory fields as required.
{
    "type": "object",
    "properties": {
        "city": {
            "type": "string",
            "description": "The name of the city"
        },
        "temperature": {
            "type": "number",
            "description": "The temperature in Celsius"
        },
        "weather_condition": {
            "type": "string",
            "enum": ["sunny", "cloudy", "rainy", "snowy"],
            "description": "The weather condition"
        }
    },
    "required": ["city", "temperature", "weather_condition"]
}

This schema defines that a valid response must include the city name, temperature, and weather condition. The weather condition must be chosen from available values.

Prompt Engineering for Structured Outputs

Effective prompting is crucial for guiding the model to produce the desired structured output. This involves crafting clear and concise instructions that Align with your defined schema.

Tips for effective Prompt engineering:

  • Clearly state the desired output format.
  • Provide examples of the expected response.
  • Use specific keywords to guide the model's focus.
  • Iterate and refine your prompts based on the model's output.

For example:

  • "Extract the key entities and their relationships from the following text. Provide the output in JSON format according to the specified schema."
  • "Summarize the customer feedback and identify the sentiment and category. Respond in JSON with the fields message, sentiment, and category."

By following these prompt engineering strategies, you can significantly improve the model's ability to produce accurate and well-structured outputs.

Handling Complex Scenarios

Handling complex scenarios with Structured Outputs requires a combination of careful schema design and thoughtful prompt engineering. This might involve dealing with nested data structures, conditional fields, or multiple possible output formats.

Here are some strategies for managing complex scenarios:

  • Nested Schemas: Define nested objects in your schema to represent hierarchical data.
  • Conditional Logic: Use prompt engineering to guide the model to handle conditional fields based on specific conditions.
  • Schema Versioning: Maintain different versions of your schema to accommodate evolving data requirements.

For instance, if you're dealing with product data where some products have additional attributes, you can define a base schema with common attributes and an additional properties field to capture the variable attributes.

By thoughtfully addressing these challenges, you can effectively leverage Structured Outputs to handle complex scenarios and ensure high-quality, reliable data extraction.

Understanding OpenAI Pricing for Structured Outputs

Cost Considerations

OpenAI's pricing is based on token usage, which depends on the model, input prompt length, and output response length. When using Structured Outputs, it's essential to consider these factors to manage costs effectively. Costs will vary depending on the model used.

To optimize costs:

  • Optimize prompts: Keep prompts concise and clear to reduce token usage.
  • Streamline schemas: Design schemas that include only necessary fields to minimize response length.
  • Monitor usage: Regularly track your API usage to identify potential cost-saving opportunities.

For example, when extracting data from lengthy documents, consider summarizing or chunking the documents to reduce input token count. Streamlining schema can significantly reduce the number of tokens used in the output.

Advantages and Disadvantages of Using Structured Outputs

👍 Pros

Enhanced data reliability and type safety

Reduced need for data validation and reformatting

Programmatic detection of safety-based model refusals

Simplified prompting and consistent formatting

Improved data quality for AI projects

👎 Cons

Potentially higher token usage compared to basic JSON mode

Requires careful schema design and prompt engineering

Models will return errors if the data cannot be correctly extracted

Key Features of OpenAI's Structured Output API

Reliable and Type-Safe Outputs

One of the foremost key features of the Structured Output API is its reliable and type-safe output. This ensures that the data returned from the OpenAI models adheres strictly to the JSON schema defined by developers, reducing the risk of runtime errors and validation failures. This feature eliminates the need for re-validation or retrying responses, streamlining data processing and integration.

Explicit Refusals Detection

The API allows for programmatic detection of safety-based model refusals, thus enhancing safety and compliance of applications powered by OpenAI. With this feature, developers can more easily manage model behavior and implement responses that align with their safety guidelines. This guarantees not only safer, but also more compliant and transparent operations.

Simpler and more consistent Prompting

The API simplifies prompting by enabling consistent formatting without the use of complex instructions. Structured output provides the means to obtain results with an easier approach to prompts, streamlining your AI application workflows. Prompts becomes easier to achieve a consistent output format, which simplifies your development workflow.

Guaranteed Schema Adherence

In contrast to the traditional JSON mode, Structured Outputs ensures schema adherence. The models now have been better trained to understand complicated schemas to reliably adhere to the pre-defined JSON schema, making AI driven systems more predictable and reliable.

Practical Use Cases for Structured Outputs

Customer Feedback Analysis

Structured Outputs can be used to extract and categorize customer feedback from reviews, surveys, and social media. By defining a JSON schema with fields such as message, sentiment, and category, you can automatically analyze feedback and gain insights into customer opinions. Example data might be:

{
    "message": "The product is great, but the shipping was slow",
    "sentiment": "negative",
    "category": "shipping"
}

This can help in understanding key data elements, the sentiment and then the general sentiment.

Product Data Extraction

You can use Structured Outputs to extract product data from e-commerce websites, catalogs, and databases. Define a schema with fields such as product_name, price, description, and specifications to automatically extract and structure product information. Example data might be:

{
    "product_name": "Wireless Mouse",
    "price": 25.00,
    "description": "Ergonomic wireless mouse with long battery life",
    "specifications": {
        "connectivity": "wireless",
        "battery_life": "24 months"
    }
}

This structured data can then be used in a variety of use cases, including:

  • E-commerce listing: Product attributes can be displayed in e-commerce stores to enhance customer experience.
  • Product comparison: Data can be used to compare attributes between similar products to assist customers in decision-making.
  • Data aggregation: Data can be used to aggregate data in a centralized location for future retrieval.
  • Content enhancement: Product features and benefits can be converted into SEO optimized paragraphs for more discoverable web pages.

Event Extraction and Categorization

Event extraction and categorization are made simple with Structured Outputs. Structured outputs can extract and categorize events from news articles, social media posts, and other sources. Create a schema with fields such as event_name, date, location, and participants to automatically extract and structure event information. Example data might be:

{
    "event_name": "AI Conference",
    "date": "2024-08-15",
    "location": "New York",
    "participants": ["Data Scientists", "AI Engineers"]
}

These events can then be displayed on a dynamic website or other platform and enhance user engagement.

FAQ

What models support Structured Outputs?
The newest models, such as gpt-4o-2024-06-06, support Structured Outputs. Ensure you select a compatible model when making API requests.
How does Structured Outputs improve data reliability?
Structured Outputs ensure that responses adhere to predefined JSON schemas, reducing the risk of missing keys, incorrect data types, or invalid enum values. This leads to more predictable and reliable data.
Can I use Structured Outputs with function calling?
Function calling and Structured Outputs serve different purposes. Function calling is used to trigger external APIs or code snippets, while Structured Outputs is used to enforce data structures and types. They are used for different types of tasks.
What if the model cannot extract data according to the schema?
If the model cannot extract the data, it will return an error indicating that the output could not be formatted according to the schema. Ensure your prompts are clear and provide enough context for the model to extract the required information.
Is there a cost difference between JSON Mode and Structured Outputs?
Both are billed based on token usage, but Structured Outputs may use more tokens due to the extra constraints. Optimizing prompts and streamlining schemas can help manage costs effectively.

Related Questions

How can I optimize prompts for Structured Outputs?
To optimize prompts for Structured Outputs, consider the following strategies: Clearly define the desired output format: Specify the required JSON schema in your prompt to guide the model in generating the expected structure. Provide context and examples: Include relevant context and examples to help the model understand the type of information to extract. Use keywords: Use keywords to focus the model's attention on the key elements to extract, such as entity names, attributes, or relationships. Iterate and refine: Test and refine your prompts based on the model's output to improve accuracy and reliability. Prompts will have to be improved as time goes on.

Most people like