Hey! I was previously able to use Pydantic types and field descriptions to define a schema with the Completions API – it worked great!
Here’s an example that used to work:
from openai import OpenAI
from pydantic import BaseModel, Field
class ResponseFormat(BaseModel):
say_hi_like_a_royal_person_briefly: str = Field(
..., description="Respond in German!"
)
client = OpenAI()
completion = client.beta.chat.completions.parse(
model="gpt-4o-2024-11-20",
response_format=ResponseFormat,
messages=[
{"role": "system", "content": "You are a helpful assistant."},
],
)
print(completion.choices[0].message.parsed)
Now I’m trying to achieve the same result using the new responses.create()
API, but I can’t seem to get it working.
Here’s what I tried:
from openai import OpenAI
from pydantic import BaseModel, Field
import json
class ResponseFormat(BaseModel):
say_hi_like_a_royal_person_briefly: str = Field(
..., description="Respond in German!"
)
ResponseFormat.model_json_schema() # schema seems fine here
client = OpenAI()
resp = client.responses.create(
model="gpt-4o",
text={
"format": {
"type": "json_schema",
"name": "response_to_german",
"strict": True,
"schema": ResponseFormat.model_json_schema(),
}
},
input=[
{"role": "system", "content": "You are a helpful assistant."},
],
)
print(json.loads(resp.output_text))
Is there a correct way to use Pydantic models with the new responses.create()
endpoint? Or is this not supported now?