Pydantic integration seems to be broken in new responses.create API

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?

You can follow this example of sending, which I provided and verified 11 hours ago.

The support is provided by the OpenAI SDK library for Python, so you must pip install --upgrade openai too.

Yes, it works, thanks! It would be great to have this in the official docs too :slight_smile:

I’ll place a working example here in case someone else is looking for it:

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()
resp = client.responses.parse(
    model="gpt-4o",
    text_format=ResponseFormat,
    input=[
        {"role": "system", "content": "You are a helpful assistant."},
    ],
)

print(resp.output_text)
1 Like