Structured Outputs: A Deep Dive into GPT-4o’s August 2024 Enhancement

Introduction

In an era where artificial intelligence is becoming increasingly integral to various industries, models like GPT-4o and GPT-4o Mini are leading the charge. These models, particularly with the 2024 August update, are not only more powerful but also offer more refined features.

One such feature is “structured outputs,” which is revolutionizing how AI-driven responses are formatted and used. In this blog post, we’ll explore the benefits of “structured outputs” and provide an example of how to implement them using Azure AI Studio.

Structured Outputs

In traditional natural language processing, getting an AI model to produce human-like text is just the beginning. For many applications, especially in business and technical domains, the responses need to follow a specific structure. This is where “structured outputs” come into play, allowing for responses that are not just contextually accurate but also formatted in a way that integrates seamlessly with other systems.

Whether it’s generating formatted documents, structured data for databases, or specific response templates for chatbots, “structured outputs” can significantly enhance the utility and efficiency of AI-generated content. The 2024 August version of GPT-4o and GPT-4o Mini incorporates advanced capabilities to streamline this process, making it easier than ever to produce consistent and reliable outputs.

GPT-4o and GPT-4o Mini: A Brief Overview

GPT-4o and GPT-4o Mini are iterations of OpenAI’s GPT-4 model, optimized for various applications and resource constraints. While GPT-4o is designed for large-scale, high-complexity tasks requiring extensive computational power, GPT-4o Mini is a more compact and efficient version, suitable for environments with limited resources without significant sacrifice in performance.

An Example Without Structured Outputs

We can generate structured data with a prompt like this

Create a random list of 3 users for mocking purposes, include full name, email, phone, address and username. Format as a Json array.

The JSON payload sent to the chat/completions API endpoint might be something like this:

{
    "messages": [
        {
            "role": "user",
            "content": [
                {
                    "type": "text",
                    "text": "Create a random list of 3 users for mocking purposes, include full name, email, phone, address and username. Format as a Json array."
                }
            ]
        }
    ],
    "temperature": 0.7,
    "top_p": 0.95,
    "max_tokens": 800
}

And the response back from the API would be something like this:

{
  "message": {
    "content": [
      {
        "full_name": "Emily Johnson",
        "email": "emily.johnson@example.com",
        "phone": "555-123-4567",
        "address": "123 Maple Street, Springfield, IL 62704",
        "username": "emily_j"
      },
      {
        "full_name": "Michael Smith",
        "email": "michael.smith@example.com",
        "phone": "555-234-5678",
        "address": "456 Oak Avenue, Anytown, CA 90210",
        "username": "mike_smith"
      },
      {
        "full_name": "Jessica Brown",
        "email": "jessica.brown@example.com",
        "phone": "555-345-6789",
        "address": "789 Pine Road, Metropolis, NY 10001",
        "username": "jess_brown"
      }
    ],
    "role": "assistant"
  }
}

This is how we have been using it until now. While prompts can take you a long way, and even when the output looks okay, you will need to decode the response. You will usually encounter issues with schema adherence, consistency, and handling of complex scenarios.

Using Structured Outputs

Now, with the new structured outputs feature, we can use a JSON schema to define the format of the response. In the following API request, we define a JSON array where the items inside it are the users. Notice how the address is an object itself, and even though we didn’t change the prompt, it is populated accordingly. Note: if your JSON schema has any issues, you will receive an error message in response to your request.

{
    "messages": [
        {
            "role": "user",
            "content": [
                {
                    "type": "text",
                    "text": "Create a random list of 3 users for mocking purposes, include full name, email, phone, address and username."
                }
            ]
        }
    ],
    "temperature": 0.7,
    "top_p": 0.95,
    "max_tokens": 800,
    "response_format": {
        "type": "json_schema",
        "json_schema": {
            "name": "UserList",
            "strict": true,
            "schema": {
                "type": "object",
                "properties": {
                    "users": {
                        "type": "array",
                        "items": {
                            "type": "object",
                            "properties": {
                                "full_name": {
                                    "type": "string"
                                },
                                "email": {
                                    "type": "string"
                                },
                                "phone": {
                                    "type": "string",
                                    "description": "International format, starting with a '+' and country code"
                                },
                                "address": {
                                    "type": "object",
                                    "properties": {
                                        "street_address": {
                                            "type": "string"
                                        },
                                        "city": {
                                            "type": "string"
                                        },
                                        "state": {
                                            "type": "string"
                                        },
                                        "postal_code": {
                                            "type": "string"
                                        },
                                        "country": {
                                            "type": "string"
                                        }
                                    },
                                    "additionalProperties": false,
                                    "required": [
                                        "street_address",
                                        "city",
                                        "state",
                                        "postal_code",
                                        "country"
                                    ]
                                },
                                "username": {
                                    "type": "string"
                                }
                            },
                            "additionalProperties": false,
                            "required": [
                                "full_name",
                                "email",
                                "username",
                                "phone",
                                "address"
                            ]
                        }
                    }
                },
                "required": ["users"],
                "additionalProperties": false
            }
        }
    }
}

And here is the relevant section in the response:

{
  "message": {
    "content": {
      "users": [
        {
          "full_name": "Olivia Harper",
          "email": "olivia.harper@example.com",
          "phone": "+1-202-555-0131",
          "address": {
            "street_address": "123 Maple Street",
            "city": "Springfield",
            "state": "IL",
            "postal_code": "62701",
            "country": "USA"
          },
          "username": "oharper"
        },
        {
          "full_name": "Liam Chen",
          "email": "liam.chen@example.com",
          "phone": "+44-20-7946-0958",
          "address": {
            "street_address": "456 Elm Road",
            "city": "London",
            "state": "Greater London",
            "postal_code": "SW1A 1AA",
            "country": "UK"
          },
          "username": "lchen"
        },
        {
          "full_name": "Ava Müller",
          "email": "ava.mueller@example.com",
          "phone": "+49-30-1234-5678",
          "address": {
            "street_address": "789 Birkenweg",
            "city": "Berlin",
            "state": "Berlin",
            "postal_code": "10115",
            "country": "Germany"
          },
          "username": "amueller"
        }
      ]
    },
    "role": "assistant"
  }
}

For more details about the necessary formatting and available options in your Json schema, check out the links in the resources section below. These resources provide examples and best practices to help ensure you would get your desired outcome.

Conclusion

The introduction of “structured outputs” in the August 2024 versions of GPT-4o and GPT-4o Mini marks a significant advancement in AI-generated content. By utilizing JSON schemas to define response formats, developers can achieve more precise, consistent, and reliable outputs.

This enhancement simplifies integration with other systems and reduces the need for extensive data parsing. As AI technology progresses, leveraging features like “structured outputs” will be essential for maximizing efficiency and effectiveness. We encourage you to explore these new capabilities using the resources provided.

If you’re ready to explore how structured outputs, Chat GPT, or AI can transform your business, contact Trailhead today for expert guidance tailored to your needs. Whether you’re looking to integrate cutting-edge AI models or streamline your data workflows, we can help.

Resources

Picture of Rodrigo Juarez

Rodrigo Juarez

Rodrigo Juarez has over 25 years of experience in software development, specializing in Microsoft technologies. His mission is to solve complex problems for his clients, with a focus on results. He achieves this by applying the appropriate technology and using best practices to ensure the delivery of high-quality solutions. Over the past 6 years, Rodrigo has honed his skills in back-end development and in creating mobile applications, initially using Xamarin and now transitioning to .NET MAUI. He is a Lead Mobile Developer at Trailhead Technology Partners and the co-author, along with Jesse Liberty, of the book ".NET MAUI for C# Developers"

Free Consultation

Sign up for a FREE consultation with one of Trailhead's experts.

"*" indicates required fields

This field is for validation purposes and should be left unchanged.

Related Blog Posts

We hope you’ve found this to be helpful and are walking away with some new, useful insights. If you want to learn more, here are a couple of related articles that others also usually find to be interesting:

Deploying A GPT-4o Model to Azure OpenAI Service

Azure OpenAI Service enables easy deployment of powerful language models like GPT-4o. This guide walks you through the process, from securing access and creating resources in the Azure portal to navigating the updated Azure AI Studio for mo

Read More

Exploring What's New in .NET 8

In the early days of the .NET Framework, releases were infrequent and timed with Windows releases. The shift to an open-source platform brought a welcome change. Now, we anticipate an annual .NET release tied to the .NET Conf event every No

Read More

Our Gear Is Packed and We're Excited to Explore With You

Ready to come with us? 

Together, we can map your company’s software journey and start down the right trails. If you’re set to take the first step, simply fill out our contact form. We’ll be in touch quickly – and you’ll have a partner who is ready to help your company take the next step on its software journey. 

We can’t wait to hear from you! 

Main Contact

This field is for validation purposes and should be left unchanged.

Together, we can map your company’s tech journey and start down the trails. If you’re set to take the first step, simply fill out the form below. We’ll be in touch – and you’ll have a partner who cares about you and your company. 

We can’t wait to hear from you! 

Thank you for reaching out.

You’ll be getting an email from our team shortly. If you need immediate assistance, please call (616) 371-1037.