Pydantic datetime yields Invalid Schema

    class TestResponse(BaseModel):
        todays_date: Optional[datetime.datetime]

    completion = client.beta.chat.completions.parse(
        model="gpt-4o-2024-08-06",
        messages=[
            dict(role="user", content="What is today's date?")
        ],
        response_format=TestResponse
    )

yields

openai.BadRequestError: Error code: 400 - {'error': {'message': "Invalid schema for response_format 'TestResponse': In context=('properties', 'todays_date', 'anyOf', '0'), 'format' is not permitted", 'type': 'invalid_request_error', 'param': 'response_format', 'code': None}}

Also fails with just dates

    class TestResponse(BaseModel):
        todays_date: Optional[datetime.date]

What’s the best way to handle this? If I hack the schema output and remove ‘format’ it works… but that doesn’t seem ideal.

Snake: 3.11
Pydantic: 2.7.2
OpenAI: 1.40.2

1 Like

@gravityanalytica this is similar to this thread. I’ll paste the same answer here :slight_smile:

This is correct behaviour. According to the docs only the following types are supported:

  • String
  • Number
  • Boolean
  • Object
  • Array
  • Enum
  • anyOf

This is in line with JSON data type support, which doesn’t have support for datetime.

So the idea then is to use a String :slight_smile:

1 Like