Skip to content

fix: handle None content and empty candidates in GeminiLLM parsing#4462

Merged
whysosaket merged 1 commit intomainfrom
fix/gemini-none-content-3410
Mar 21, 2026
Merged

fix: handle None content and empty candidates in GeminiLLM parsing#4462
whysosaket merged 1 commit intomainfrom
fix/gemini-none-content-3410

Conversation

@kartik-mem0
Copy link
Copy Markdown
Contributor

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

  • Bug fix (non-breaking change which fixes an issue)

How Has This Been Tested?

  • Unit Test

Added 6 new edge case tests to tests/llms/test_gemini.py:

  • test_parse_response_none_content_no_tools — content=None without tools
  • test_parse_response_none_content_with_tools — content=None with tools
  • test_parse_response_empty_candidates — empty candidates list
  • test_parse_response_none_candidates — None candidates
  • test_parse_response_empty_parts_no_tools — empty parts list without tools
  • test_parse_response_empty_parts_with_tools — empty parts list with tools

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:

  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • Any dependent changes have been merged and published in downstream modules
  • I have checked my code and corrected any misspellings

Maintainer Checklist

@whysosaket whysosaket merged commit abdb07c into main Mar 21, 2026
8 checks passed
@whysosaket whysosaket deleted the fix/gemini-none-content-3410 branch March 21, 2026 08:22
jamebobob pushed a commit to jamebobob/mem0-vigil-recall that referenced this pull request Mar 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Invalid JSON response when using memory.add with gemini-2.5-flash

3 participants