I’m having issues when calling Gemini via openai API.
I tried to generate a structured nested output, so I used pydantic basemodel.
The code snippet:
class SellingPoint(BaseModel):
Content: str
Keywords: str
class ProductIntroduction(BaseModel):
Introduction: str
Selling_point_1: SellingPoint
Selling_point_2: SellingPoint
Selling_point_3: SellingPoint
Conclusion: str
response = client.beta.chat.completions.parse(
model="gemini-2.0-flash",
messages=[
{
"role": "system",
"content": sys_prompt},
{
"role": "user",
"content": [
{
"type": "text",
"text": prompt
},
{
"type": "image_url",
"image_url": {
"url": f"data:image/jpeg;base64,{e_img1}",
}
},
{
"type": "image_url",
"image_url": {
"url": f"data:image/jpeg;base64,{e_img2}",
}
},
{
"type": "image_url",
"image_url": {
"url": f"data:image/jpeg;base64,{e_img3}",
}
}
]
}],
response_format=ProductIntroduction
)
The error occured:
Traceback (most recent call last):
File "/data/home/hedy/ai_marketing_video/gemini.py", line 51, in <module>
response = client.beta.chat.completions.parse(
File "/home/hedy/anaconda3/envs/market/lib/python3.10/site-packages/openai/resources/beta/chat/completions.py", line 161, in parse
return self._post(
File "/home/hedy/anaconda3/envs/market/lib/python3.10/site-packages/openai/_base_client.py", line 1290, in post
return cast(ResponseT, self.request(cast_to, opts, stream=stream, stream_cls=stream_cls))
File "/home/hedy/anaconda3/envs/market/lib/python3.10/site-packages/openai/_base_client.py", line 967, in request
return self._request(
File "/home/hedy/anaconda3/envs/market/lib/python3.10/site-packages/openai/_base_client.py", line 1071, in _request
raise self._make_status_error_from_response(err.response) from None
openai.BadRequestError: Error code: 400 - [{'error': {'code': 400, 'message': 'Request contains an invalid argument.', 'status': 'INVALID_ARGUMENT'}}]
However, if I changed the output format into non-nested (flat) like below, it could execute well.
class ProductIntroduction(BaseModel):
Introduction: str
Selling_point_1: str
Selling_point_2: str
Selling_point_3: str
Conclusion: str
Other information:
python version: 3.10.16
openai version: 1.64.0
Has anyone run into the error?