Skip to content

Broken OpenAI composite Pydantic structured-output #641

Open
@ravwojdyla

Description

@ravwojdyla

Description of the bug:

Here's MRE:

# /// script
# requires-python = ">=3.10"
# dependencies = [
#     "google-generativeai==0.8.3",
#     "openai==1.55.3",
#     "pydantic==2.10.2",
# ]
# ///
import os
from typing import Annotated
from openai import OpenAI
from pydantic import BaseModel, Field


client = OpenAI(
    api_key=os.environ["GOOGLE_AI_API_KEY"],
    base_url="https://generativelanguage.googleapis.com/v1beta/openai/",
)

class FooWorking(BaseModel):
    foo: Annotated[str, Field()]

class Bar(BaseModel):
    bar: Annotated[str, Field()]

class FooNotWorking(BaseModel):
    foo: Annotated[Bar, Field()]

# Will work
client.beta.chat.completions.parse(
    model="gemini-1.5-flash",
    messages=[
        {"role": "user", "content": "What is bar?"},
    ],
    response_format=FooWorking,
)
print("First call worked!")

# Will NOT work!
client.beta.chat.completions.parse(
    model="gemini-1.5-flash",
    messages=[
        {"role": "user", "content": "What is bar?"},
    ],
    response_format=FooNotWorking,
)

Both calls work just fine on the OpenAI API. Just a hypothesis, but maybe handling of $defs is broken/unsupported on Google side. Another thing completely is that the vanilla 400 error message is not informative/helpful:

openai.BadRequestError: Error code: 400 - [{'error': {'code': 400, 'message': 'Request contains an invalid argument.', 'status': 'INVALID_ARGUMENT'}}]

Actual vs expected behavior:

2nd call should work. If this is a limitation, should that be documented in the docs:

Any other information you'd like to share?

No response

Metadata

Metadata

Assignees

Labels

component:apiIssues related to the API, not the SDK.status:triagedIssue/PR triaged to the corresponding sub-teamtype:bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions