-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Gemini 2.0 Flash structured output value repetition and missing fields #449
Comments
I have also experienced missing fields using a relatively simple schema for a list of multiple choice quiz questions. class QuestionAnswerSet(BaseModel): class QuestionAnswerList(BaseModel): |
We had this exact problem in a few-shot document classification/entity extraction use case (with Gemini 1.5-pro). For now, we were able to fix it by downgrading google-cloud-aiplatform:
We can reproduce the error reliably in sandboxed environments by switching the package versions back and forth - from looping text output until token limit is reached (latest version) to straight forward correct output (version 1.69.0). Sadly, the few shot documents used to create this problem are proprietary and I can't share them. I really can't imagine what the package is doing to influence the model outputs this badly, but that's what we're working with... Maybe this solves the issue for anyone else who has this problem! |
I have the same problem with structured output both in gemini-1.5-pro-002, but it's even worse in gemini-flash-2.0 |
@felixvor : Out of curiosity, do you know what could be the difference between 1.69.0 and whatever version you had before? |
No idea, I checked the changelogs but they gave me no clue insofar they seemed to have nothing to do with the way the model generates text. We are currently experimenting using the latest version again in combination with frequency penalty and presence penalty. Maybe that also helps in your case! Update: The problem now appears with |
I found a solution 🙌 Use the {
"type": "object",
"properties": {
"data": {
"type": "array",
"items": {
"type": "object",
"properties": {
"UkLWZg": {
"type": "string"
},
"gbHJdm": {
"type": "string"
},
"EfhxLZ": {
"type": "array",
"items": {
"type": "string"
}
},
"VqXmZF": {
"type": "string"
},
"uw2YK1": {
"type": "integer"
}
}
}
}
},
"required": ["UkLWZg", "gbHJdm", "EfhxLZ", "VqXmZF", "uw2YK1"]
} I hope this helps others who encounter this problem! |
@felixvor Thank you for your suggestion! I’ve added the |
Same issue here. we use Gemini 2.0 Flash - apparently this problem has been faced across other models in the past: I will go ahead and try the Required attributed - thanks @felixvor We currently use the Python data classes for structuring:
Is there a way to add the required attribute using the python data classes? or should we switch to a hard coded schema? |
@markmcd Thought this might be worth taking a look at. Thanks |
@karayanni Try to use pydantic Fields. You can use the ... (Ellipsis) to explicitly mark them as "required".
Let me know if that improves the model output! |
The SDK does mark them as required by default. Using your schema: >>> from google.genai import _transformers
>>> pprint(_transformers.t_schema(client, SomeObject).to_json_dict())
{'properties': {'field_1': {'type': 'STRING'},
'field_2': {'nullable': True, 'type': 'STRING'},
'field_3': {'type': 'INTEGER'}},
'required': ['field_1', 'field_2', 'field_3'],
'type': 'OBJECT'} I'm not sure if there's a way to turn it off though (Optional fields are represented as |
Thanks @markmcd and @felix-vorlaender-mittelstand-ai It appears that the default behavior with the required is not solving the structured output edge case with infinite loop. We are adding a retry with variants of the prompt to overcome this problem.. but it seems like there is a fundamental problem with the LLMs structured output flow If there are any additional tips / future improvements would love to be updated to figure out better approaches/solutions. |
@Yiling-J did you figure out a solution? This is making Gemini 2 flash not usable as almost 20% of the calls fail and waste so much tokens.. Our team now has a fall back strategy with 3 different prompts - with few shot examples, when it fails, it fails on all 3 different prompts! FYI - @markmcd please let us know if there are any updates - we would much appreciate it |
@karayanni Adding the I actually encountered errors using the OpenAI Go SDK, not the Python Gemini SDK. But I was able to reproduce it using the Gemini web as I reported in this issue. After applying the fix, both the SDK and web are now working as expected. |
do you add required filed by code? can you give a example? class SomeObject(BaseModel): class ListOfFirstObject(BaseModel): |
@xylophone21 According to #449 (comment), the google.genai SDK should automatically add the required fields. You can try printing the schema to verify. In my case, I do manually add the required fields in code since I'm working with a dynamic schema (not from an existing class or struct) but I'm using Go with the OpenAI Go SDK ,so unfortunately, I can’t offer much help with your Python question. |
Description of the bug:
Schema Used:
Prompt Used:
Actual vs expected behavior:
Repeated Values:
Missing Fields:
Using the same schema and prompt, GPT-4o generates a structured response without value repetition or missing fields.
I didn't test o3 mini or other OpenAI models due to OpenAI's restrictions. This report is based on tests using the GPT-4o model on GitHub Models.
Any other information you'd like to share?
No response
The text was updated successfully, but these errors were encountered: