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]
Python ai-gatewayanthropicazure-openaibedrockgatewaylangchainlitellmllmllm-gatewayllmopsmcp-gatewayopenaiopenai-proxyrustrust-aivertex-ai
fix(vertex_ai): normalize Gemini finish_reason enum through map_finis… (#25337)
abhyudayareddye6746270af12
diff --git a/litellm/litellm_core_utils/streaming_handler.py b/litellm/litellm_core_utils/streaming_handler.pyindex e402023d240..ad3aaddaf01 100644--- a/litellm/litellm_core_utils/streaming_handler.py+++ b/litellm/litellm_core_utils/streaming_handler.py@@ -1282,9 +1282,9 @@ class CustomStreamWrapper:and chunk.candidates[0].finish_reason.name # type: ignore!= "FINISH_REASON_UNSPECIFIED"): # every non-final chunk in vertex ai has this- self.received_finish_reason = chunk.candidates[ # type: ignore- 0- ].finish_reason.name+ self.received_finish_reason = map_finish_reason( # type: ignore+ chunk.candidates[0].finish_reason.name+ )except Exception:if chunk.candidates[0].finish_reason.name == "SAFETY": # type: ignoreraise Exception(diff --git a/tests/test_litellm/litellm_core_utils/test_streaming_handler.py b/tests/test_litellm/litellm_core_utils/test_streaming_handler.pyindex aad3de306c7..cdf38d71137 100644--- a/tests/test_litellm/litellm_core_utils/test_streaming_handler.py+++ b/tests/test_litellm/litellm_core_utils/test_streaming_handler.py@@ -1826,3 +1826,73 @@ async def test_custom_stream_wrapper_anext_exhaustion_raises_stop_async_iteratiopass # expected clean terminationexcept RuntimeError as e:pytest.fail(f"PEP 479 regression: StopIteration leaked as RuntimeError: {e}")+++def test_gemini_legacy_vertex_stop_finish_reason_normalised():+ """+ The legacy vertex_ai SDK streaming path sets finish_reason from a proto enum+ whose .name attribute is an uppercase string (e.g. "STOP", "MAX_TOKENS").+ Before the fix, received_finish_reason was stored as "STOP" which never+ matched "stop" in finish_reason_handler, silently breaking the tool_calls+ override. After the fix, map_finish_reason() is applied so the value is+ always an OpenAI-normalised lowercase string.+ """+ wrapper = CustomStreamWrapper(+ completion_stream=None,+ model="gemini-1.5-pro",+ logging_obj=MagicMock(),+ custom_llm_provider="vertex_ai",+ )++ # Simulate a proto-like chunk: .candidates[0].finish_reason.name == "STOP"+ mock_finish_reason = MagicMock()+ mock_finish_reason.name = "STOP"+ mock_candidate = MagicMock()+ mock_candidate.finish_reason = mock_finish_reason+ mock_chunk = MagicMock()+ mock_chunk.candidates = [mock_candidate]+ # Ensure the chunk is not treated as a ModelResponseStream+ mock_chunk.__class__ = type("FakeProtoChunk", (), {})++ with patch("litellm.litellm_core_utils.streaming_handler.proto", create=True):+ wrapper.chunk_creator(chunk=mock_chunk)++ assert wrapper.received_finish_reason == "stop", (+ f"Expected 'stop' but got {wrapper.received_finish_reason!r}. "+ "map_finish_reason() was not applied to the Gemini enum name."+ )+++def test_gemini_legacy_vertex_tool_calls_finish_reason_with_stop_enum():+ """+ When Gemini emits finish_reason STOP alongside tool-call content, the final+ chunk must report finish_reason='tool_calls'. This requires that the raw+ "STOP" enum name is first normalised to lowercase "stop" by map_finish_reason()+ so that finish_reason_handler's equality check fires correctly.+ """+ wrapper = CustomStreamWrapper(+ completion_stream=None,+ model="gemini-1.5-pro",+ logging_obj=MagicMock(),+ custom_llm_provider="vertex_ai",+ )++ mock_finish_reason = MagicMock()+ mock_finish_reason.name = "STOP"+ mock_candidate = MagicMock()+ mock_candidate.finish_reason = mock_finish_reason+ mock_chunk = MagicMock()+ mock_chunk.candidates = [mock_candidate]+ mock_chunk.__class__ = type("FakeProtoChunk", (), {})++ with patch("litellm.litellm_core_utils.streaming_handler.proto", create=True):+ wrapper.chunk_creator(chunk=mock_chunk)++ # Signal that tool_calls were present in the stream+ wrapper.tool_call = True++ final = wrapper.finish_reason_handler()+ assert final.choices[0].finish_reason == "tool_calls", (+ f"Expected 'tool_calls' but got {final.choices[0].finish_reason!r}. "+ "STOP enum was not normalised through map_finish_reason()."+ )