Structured output with Azure OpenAI

import os
from openai import AzureOpenAI
from pydantic import BaseModel

client = AzureOpenAI(
azure_endpoint = os.getenv(“AZURE_OPENAI_ENDPOINT”),
api_key=os.getenv(“AZURE_OPENAI_API_KEY”),
api_version=os.getenv(“AZURE_OPENAI_API_VERSION”)
)

class Step(BaseModel):
explanation: str
output: str

class MathReasoning(BaseModel):
steps: list[Step]
final_answer: str

response = client.chat.completions.create(
model=“gpt-4o-mini”,
messages=[
{“role”: “system”, “content”: “You are a helpful math tutor. Guide the user through the solution step by step.”},
{“role”: “user”, “content”: “how can I solve 8x + 7 = -23”}
],

 response_format=MathReasoning,

)

print(response.choices[0].message.parsed)

gives

TypeError: Object of type ModelMetaclass is not JSON serializable

2 Likes

Follow this article for using structured output with Azure OpenAI:

3 Likes

Have you even read the article? The title is misleading as the article only shows how to use tool calling…

Do you need to call model_rebuild() on the top-level model as mentioned in the docs?

Just today, Azure announced Structured Outputs availability for GPT-4o (not mini). Google “Introducing GPT-4o-2024-08-06 API with Structured Outputs on Azure”.