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.


