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:

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! 

Montage Portal

Montage Furniture Services provides furniture protection plans and claims processing services to a wide selection of furniture retailers and consumers.

Project Background

Montage was looking to build a new web portal for both Retailers and Consumers, which would integrate with Dynamics CRM and other legacy systems. The portal needed to be multi tenant and support branding and configuration for different Retailers. Trailhead architected the new Montage Platform, including the Portal and all of it’s back end integrations, did the UI/UX and then delivered the new system, along with enhancements to DevOps and processes.

Logistics

We’ve logged countless miles exploring the tech world. In doing so, we gained the experience that enables us to deliver your unique software and systems architecture needs. Our team of seasoned tech vets can provide you with:

Custom App and Software Development

We collaborate with you throughout the entire process because your customized tech should fit your needs, not just those of other clients.

Cloud and Mobile Applications

The modern world demands versatile technology, and this is exactly what your mobile and cloud-based apps will give you.

User Experience and Interface (UX/UI) Design

We want your end users to have optimal experiences with tech that is highly intuitive and responsive.

DevOps

This combination of Agile software development and IT operations provides you with high-quality software at reduced cost, time, and risk.

Trailhead stepped into a challenging project – building our new web architecture and redeveloping our portals at the same time the business was migrating from a legacy system to our new CRM solution. They were able to not only significantly improve our web development architecture but our development and deployment processes as well as the functionality and performance of our portals. The feedback from customers has been overwhelmingly positive. Trailhead has proven themselves to be a valuable partner.

– BOB DOERKSEN, Vice President of Technology Services
at Montage Furniture Services

Technologies Used

When you hit the trails, it is essential to bring appropriate gear. The same holds true for your digital technology needs. That’s why Trailhead builds custom solutions on trusted platforms like .NET, Angular, React, and Xamarin.

Expertise

We partner with businesses who need intuitive custom software, responsive mobile applications, and advanced cloud technologies. And our extensive experience in the tech field allows us to help you map out the right path for all your digital technology needs.

  • Project Management
  • Architecture
  • Web App Development
  • Cloud Development
  • DevOps
  • Process Improvements
  • Legacy System Integration
  • UI Design
  • Manual QA
  • Back end/API/Database development

We partner with businesses who need intuitive custom software, responsive mobile applications, and advanced cloud technologies. And our extensive experience in the tech field allows us to help you map out the right path for all your digital technology needs.

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

Ready to come with us? 

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 contact form. 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.