Skip to content

fix(google_genai): guard against None content.parts in _extract_response_text#5857

Closed
NIK-TIGER-BILL wants to merge 1 commit intogetsentry:masterfrom
NIK-TIGER-BILL:fix/google-genai-none-parts
Closed

fix(google_genai): guard against None content.parts in _extract_response_text#5857
NIK-TIGER-BILL wants to merge 1 commit intogetsentry:masterfrom
NIK-TIGER-BILL:fix/google-genai-none-parts

Conversation

@NIK-TIGER-BILL
Copy link

Description

When a Gemini API candidate's content.parts attribute is present but set to None (e.g. an interrupted generation or a model turn with no text), the existing hasattr(candidate.content, "parts") guard passed but the subsequent for part in candidate.content.parts loop raised:

TypeError: 'NoneType' object is not iterable

The fix replaces the hasattr sentinel with getattr(..., None), which evaluates falsy for both absent and None-valued attributes, matching the pattern used elsewhere in the file (e.g. line 220).

Changed files:

  • sentry_sdk/integrations/google_genai/utils.py — one-line guard fix
  • tests/integrations/google_genai/test_google_genai.py — regression test

Issues

Reminders

  • Tests added (test_response_with_none_content_parts)
  • Lint: no new code style issues introduced
  • PR title uses conventional commit style

…nse_text

When a Gemini candidate's content.parts attribute exists but is set to
None (e.g. interrupted generation or model returning an empty turn),
the previous hasattr() check passed but iterating over None raised:

    TypeError: 'NoneType' object is not iterable

Replace the hasattr sentinel with getattr(..., None) which evaluates
falsy for both absent and None-valued attributes.

Fixes getsentry#5854

Signed-off-by: NIK-TIGER-BILL <nik.tiger.bill@github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

TypeError: 'NoneType' object is not iterable in google_genai integration

2 participants