I want to use structured output with Azure OpenAI.
I tried the following code, based on the code given in https://openai.com/index/introducing-structured-outputs-in-the-api/:
from pydantic import BaseModel
from openai import AzureOpenAI
class Step(BaseModel):
explanation: str
output: str
class MathResponse(BaseModel):
steps: list[Step]
final_answer: str
client = AzureOpenAI(api_key='[redacted]',
api_version='2024-05-01-preview',
azure_endpoint='[redacted]')
completion = client.beta.chat.completions.parse(
model="gpt-4omini-2024-07-18-name",
messages=[
{"role": "system", "content": "You are a helpful math tutor."},
{"role": "user", "content": "solve 8x + 31 = 2"},
],
response_format=MathResponse,
)
message = completion.choices[0].message
if message.parsed:
print(message.parsed.steps)
print(message.parsed.final_answer)
else:
print(message.refusal)
I get the error:
openai.BadRequestError: Error code: 400:
{
"error": {
"message": "Invalid parameter: response_format must be one of json_object, text.",
"type": "invalid_request_error",
"param": "response_format",
"code": "None"
}
}
How to fix it?
I ran pip install -U openai
: I use openai==1.40.1
and Python 3.11.