Merge pull request #17403 from BerriAI/litellm_streaming_gemini_3_fix

Fix gemini 3 last chunk thinking block
This commit is contained in:
Sameer Kankute 2025-12-03 20:48:13 +05:30 • committed by GitHub
commit fcc2855baa
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 25 additions and 0 deletions

View file

@ -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",

View file

@ -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"