litellm - ✅(Solved) Fix [Bug]: `AttributeError: 'NoneType' object has no attribute 'startswith'` when Gemini returns image generation responses** [2 pull requests, 1 comments, 2 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
BerriAI/litellm#24385Fetched 2026-04-08 01:18:05
View on GitHub
Comments
1
Participants
2
Timeline
9
Reactions
0
Timeline (top)
cross-referenced ×3referenced ×3labeled ×2commented ×1

Error Message

A bug happened!When Gemini returns an image generation response, the parts array contains a text field whose JSON value is null. LiteLLM's get_assistant_content_message method checks for the key's existence (if "text" in part) but does not guard against a null value before calling .startswith() on it. This causes an AttributeError that LiteLLM incorrectly surfaces to the client as a 403 error.

Fix Action

Fixed

PR fix notes

PR #24390: fix(google): guard against null text in Gemini parts before startswith call

Description (problem / solution / changelog)

Description

When Gemini returns an image generation response, the parts array can contain {"text": null}. The existing if "text" in part guard does not prevent a subsequent .startswith() call from raising AttributeError: 'NoneType' object has no attribute 'startswith', which LiteLLM incorrectly surfaces as a 403 error.

Fix

Added an explicit is not None check in both get_assistant_content_message() and _extract_audio_response_from_parts() before dereferencing the text value.

Testing

Added two unit tests covering:

  • Parts with mixed null and non-null text (null parts are skipped)
  • Parts where all text values are null (returns None)

Fixes #24385

Changed files

  • litellm/llms/vertex_ai/gemini/vertex_and_google_ai_studio_gemini.py (modified, +2/-2)
  • tests/litellm/llms/vertex_ai/gemini/test_transformation.py (modified, +38/-1)

PR #24397: fix(gemini): handle null text in Gemini image generation responses

Description (problem / solution / changelog)

Guard against null text in get_assistant_content_message to prevent AttributeError when Gemini returns image generation responses with text: null.

Fixes #24385

Changed files

  • litellm/llms/vertex_ai/gemini/vertex_and_google_ai_studio_gemini.py (modified, +4/-1)
  • tests/test_litellm/llms/vertex_ai/gemini/test_vertex_and_google_ai_studio_gemini.py (modified, +20/-0)
RAW_BUFFERClick to expand / collapse

Check for existing issues

  • I have searched the existing issues and checked that my issue is not a duplicate.

What happened?

A bug happened!When Gemini returns an image generation response, the parts array contains a text field whose JSON value is null. LiteLLM's get_assistant_content_message method checks for the key's existence (if "text" in part) but does not guard against a null value before calling .startswith() on it. This causes an AttributeError that LiteLLM incorrectly surfaces to the client as a 403 error.

Steps to Reproduce

Relevant log output

What part of LiteLLM is this about?

No response

What LiteLLM version are you on ?

v1.82.6

Twitter / LinkedIn details

No response

extent analysis

Fix Plan

To fix the AttributeError caused by calling .startswith() on a null value, we need to add a null check before calling the method.

Code Changes

Here are the steps to fix the issue:

  • Modify the get_assistant_content_message method in LiteLLM to check for null values before calling .startswith():
def get_assistant_content_message(part):
    if "text" in part and part["text"] is not None:
        if part["text"].startswith(...):  # existing code
            # ...
    # ...

Alternatively, you can use the .get() method to provide a default value if the key is missing or its value is null:

def get_assistant_content_message(part):
    text = part.get("text")
    if text is not None and text.startswith(...):  # existing code
        # ...
    # ...

Verification

To verify that the fix worked, test the get_assistant_content_message method with a part object containing a null value for the text field. The method should no longer raise an AttributeError.

Extra Tips

  • Always check for null values when working with JSON data to avoid similar errors.
  • Consider adding additional logging or error handling to provide more informative error messages instead of surfacing a generic 403 error.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING

litellm - ✅(Solved) Fix [Bug]: `AttributeError: 'NoneType' object has no attribute 'startswith'` when Gemini returns image generation responses** [2 pull requests, 1 comments, 2 participants]