From 84eac5de06d8995026c9b88205f87055c24005ee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=86=AF=E5=9F=BA=E9=AD=81?= <56265583+fengjikui@users.noreply.github.com> Date: Tue, 23 Jun 2026 21:06:26 +0800 Subject: [PATCH] fix(gemini): ignore null text response parts (#30962) --- .../vertex_and_google_ai_studio_gemini.py | 2 ++ ...test_vertex_and_google_ai_studio_gemini.py | 19 +++++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/litellm/llms/vertex_ai/gemini/vertex_and_google_ai_studio_gemini.py b/litellm/llms/vertex_ai/gemini/vertex_and_google_ai_studio_gemini.py index 678877c0721..818dc8d45f0 100644 --- a/litellm/llms/vertex_ai/gemini/vertex_and_google_ai_studio_gemini.py +++ b/litellm/llms/vertex_ai/gemini/vertex_and_google_ai_studio_gemini.py @@ -1380,6 +1380,8 @@ class VertexGeminiConfig(VertexAIBaseConfig, BaseConfig): _content_str = "" if "text" in part: text_content = part["text"] + if text_content is None: + continue # Check if text content is audio data URI - if so, exclude from text content if text_content.startswith("data:audio") and ";base64," in text_content: try: diff --git a/tests/test_litellm/llms/vertex_ai/gemini/test_vertex_and_google_ai_studio_gemini.py b/tests/test_litellm/llms/vertex_ai/gemini/test_vertex_and_google_ai_studio_gemini.py index 20e7aec377b..4e323fa2821 100644 --- a/tests/test_litellm/llms/vertex_ai/gemini/test_vertex_and_google_ai_studio_gemini.py +++ b/tests/test_litellm/llms/vertex_ai/gemini/test_vertex_and_google_ai_studio_gemini.py @@ -441,6 +441,25 @@ def test_vertex_ai_empty_content(): assert reasoning_content is None +def test_vertex_ai_null_text_part_is_ignored(): + from litellm.llms.vertex_ai.gemini.vertex_and_google_ai_studio_gemini import ( + VertexGeminiConfig, + ) + from litellm.types.llms.vertex_ai import HttpxPartType + + v = VertexGeminiConfig() + parts = cast( + List[HttpxPartType], + [ + {"text": None}, + {"text": "Generated image"}, + ], + ) + content, reasoning_content = v.get_assistant_content_message(parts=parts) + assert content == "Generated image" + assert reasoning_content is None + + @pytest.mark.parametrize( "usage_metadata, inclusive, expected_usage", [