Skip to content

The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]

Fix gemini 3 last chunk thinking block

Sameer Kankute209e9e05aa3a
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 5fef8c1ec49..cfc13f5601d 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
@@ -1091,6 +1091,11 @@ class VertexGeminiConfig(VertexAIBaseConfig, BaseConfig):
if "thoughtSignature" in part:
part_copy = part.copy()
part_copy.pop("thoughtSignature")
+
+ text_content = part_copy.get("text")
+ if isinstance(text_content, str) and text_content.strip() == "":
+ continue
+
thinking_blocks.append(
ChatCompletionThinkingBlock(
type="thinking",
diff --git a/tests/local_testing/test_gemini_reasoning_content.py b/tests/local_testing/test_gemini_reasoning_content.py
new file mode 100644
index 00000000000..7e516ae8439
--- /dev/null
+++ b/tests/local_testing/test_gemini_reasoning_content.py
@@ -0,0 +1,20 @@
+import json
+from litellm.llms.vertex_ai.gemini.vertex_and_google_ai_studio_gemini import VertexGeminiConfig
+
+
+def test_empty_part_does_not_create_thinking_block():
+ parts = [{"text": "", "thoughtSignature": "sig-1"}]
+ config = VertexGeminiConfig()
+ thinking_blocks = config._extract_thinking_blocks_from_parts(parts)
+ assert thinking_blocks == []
+
+
+def test_non_empty_part_creates_thinking_block():
+ parts = [{"text": "Some thinking", "thoughtSignature": "sig-2"}]
+ config = VertexGeminiConfig()
+ thinking_blocks = config._extract_thinking_blocks_from_parts(parts)
+ assert len(thinking_blocks) == 1
+ block = thinking_blocks[0]
+ # thinking should be valid JSON containing the text
+ parsed = json.loads(block["thinking"]) if isinstance(block["thinking"], str) else None
+ assert parsed is not None and parsed.get("text") == "Some thinking"