fix: handle None content and empty candidates in GeminiLLM parsing#4462
Merged
whysosaket merged 1 commit intomainfrom Mar 21, 2026
Merged
fix: handle None content and empty candidates in GeminiLLM parsing#4462whysosaket merged 1 commit intomainfrom
whysosaket merged 1 commit intomainfrom
Conversation
utkarsh240799
approved these changes
Mar 21, 2026
whysosaket
approved these changes
Mar 21, 2026
lukaj99
added a commit
to lukaj99/mem0
that referenced
this pull request
Mar 21, 2026
jamebobob
pushed a commit
to jamebobob/mem0-vigil-recall
that referenced
this pull request
Mar 29, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Fixes AttributeError: 'NoneType' object has no attribute 'parts' crash in GeminiLLM._parse_response() when Gemini returns a response where content is None (e.g., safety filter blocks, rate
limiting, thinking-only responses).
The existing null check in _parse_response() guarded against empty candidates but not against content itself being None. When Gemini blocks a response, response.candidates[0].content is None, so
accessing .parts on it crashes. This affected all three access points in the method (both the tools and no-tools code paths).
The fix extracts candidate and parts into guarded local variables at the top of the method, replacing three repeated unsafe access chains with a single safe extraction. Returns "" (not None) to
preserve the string-return contract shared by all other LLM providers.
Fixes #3410
Type of change
How Has This Been Tested?
Added 6 new edge case tests to tests/llms/test_gemini.py:
Run: hatch run dev_py_3_12:pytest tests/llms/test_gemini.py -v — all 8 tests pass (2 existing + 6 new).
Full suite: hatch run dev_py_3_12:pytest tests/ -v — 768 passed, 0 failures.
Checklist:
Maintainer Checklist