googleapis / googleapis/python-genai
Don't swallow structured data errors
- Dominant language
- Python
- Stars
- 4k
- Forks
- 1k
- Avg merge
- 2d 11h
- Merged PRs (30d)
- 40
Description
**Is your feature request related to a problem? Please describe.**
Gemini sometimes returns invalid JSON when it should be outputting structured data. But in types.py (https://github.com/googleapis/python-genai/blob/main/google/genai/types.py#L8119-L8127) you just swallow these errors and so `response.parsed` is set to `None`.
**Describe the solution you'd like**
If you can't parse the model's output with the provided Pydantic model, raise an error and print the text that the model returned.
That would have saved me half a day of troubleshooting (I assumed a code problem, not believing that the mighty `gemini-3.1-pro-preview` would fail to return basic JSON)
**Additional context**
This [Reddit post](https://www.reddit.com/r/Bard/comments/1r9oqzr/gemini_31_json_mode_is_a_complete_disaster/) has plenty of people noting how often Gemini fails to produce valid JSON.
Here's an example response I got from the model that I think _should_ have produced an informative error, but instead just resulted in a `None` response (note the text property is truncated JSON).
```
{
"candidates": [
{
"content": {
"parts": [
{
"text": "**Clarifying the Ambiguous Earnings Request**\n\nOkay, ...",
"thought": true
},
{
"text": "\n\n\n{\n \"should_respond\": true,\n \"body\": \"I would be happy to monitor quarterly earnings announcements for you. Could you please specify which company or companies you would like me to track?\",\n \"tasks_to_add\": [],\n \"tasks_to_update\": [],\n \"tasks_to_stop\":",
"thoughtSignature": "ErQHC...lbASFR"
}
],
"role": "model"
},
"finishReason": "STOP",
"index": 0,
"groundingMetadata": {}
}
],
"usageMetadata": {
"promptTokenCount": 2801,
"candidatesTokenCount": 75,
"totalTokenCount": 3101,
"promptTokensDetails": [
{
"modality": "TEXT",
"tokenCount": 2801
}
],
"thoughtsTokenCount": 225
},
"modelVersion": "gemini-3.1-pro-preview",
"responseId": "mvjmacq3EpbYjuMPssTQiAY"
}
```
Contributor guide
Assessment
This issue has not been assessed yet.