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(streaming_handler.py): store finish reason, even if is_finished is false - allows storing early gemini finish reasons (#12250)
Krish Dholakia6717d67f3b2c
diff --git a/litellm/litellm_core_utils/streaming_handler.py b/litellm/litellm_core_utils/streaming_handler.pyindex 98fb94922f3..eeccfe53335 100644--- a/litellm/litellm_core_utils/streaming_handler.py+++ b/litellm/litellm_core_utils/streaming_handler.py@@ -758,6 +758,7 @@ class CustomStreamWrapper:is_chunk_non_empty = self.is_chunk_non_empty(completion_obj, model_response, response_obj)+if (is_chunk_non_empty): # cannot set content of an OpenAI Object to be an empty string@@ -1203,6 +1204,7 @@ class CustomStreamWrapper:if response_obj is None:returncompletion_obj["content"] = response_obj["text"]+ self.received_finish_reason = response_obj.get("finish_reason", None)if response_obj["is_finished"]:if response_obj["finish_reason"] == "error":raise Exception(@@ -1210,7 +1212,6 @@ class CustomStreamWrapper:self.custom_llm_provider, response_obj))- self.received_finish_reason = response_obj["finish_reason"]if response_obj.get("original_chunk", None) is not None:if hasattr(response_obj["original_chunk"], "id"):model_response = self.set_model_id(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.pyindex 20cf076d415..bc8844bdcb0 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@@ -1077,8 +1077,10 @@ class VertexGeminiConfig(VertexAIBaseConfig, BaseConfig):elif (finish_reason and finish_reason in mapped_finish_reason.keys()): # vertex ai+return mapped_finish_reason[finish_reason]else:+return "stop"@staticmethod@@ -1261,7 +1263,6 @@ class VertexGeminiConfig(VertexAIBaseConfig, BaseConfig):status_code=422,headers=raw_response.headers,)-return self._transform_google_generate_content_to_openai_model_response(completion_response=completion_response,@@ -1270,7 +1271,6 @@ class VertexGeminiConfig(VertexAIBaseConfig, BaseConfig):logging_obj=logging_obj,raw_response=raw_response,)-def _transform_google_generate_content_to_openai_model_response(self,diff --git a/tests/llm_translation/test_gemini.py b/tests/llm_translation/test_gemini.pyindex a42c12cafea..0029f53e07b 100644--- a/tests/llm_translation/test_gemini.py+++ b/tests/llm_translation/test_gemini.py@@ -379,3 +379,52 @@ async def test_claude_tool_use_with_gemini():has_usage_in_message_delta), "Usage should be present in message_delta with stop_reason"assert is_content_block_stop, "is_content_block_stop should be present"+++def test_gemini_tool_use():+ data = {+ "max_tokens": 8192,+ "stream": True,+ "temperature": 0.3,+ "messages": [+ {"role": "system", "content": "You are a helpful assistant."},+ {"role": "user", "content": "What's the weather like in Lima, Peru today?"},+ ],+ "model": "gemini/gemini-2.0-flash",+ "tools": [+ {+ "type": "function",+ "function": {+ "name": "get_weather",+ "description": "Retrieve current weather for a specific location",+ "parameters": {+ "type": "object",+ "properties": {+ "location": {+ "type": "string",+ "description": "City and country, e.g., Lima, Peru",+ },+ "unit": {+ "type": "string",+ "enum": ["celsius", "fahrenheit"],+ "description": "Temperature unit",+ },+ },+ "required": ["location"],+ },+ },+ }+ ],+ "stream_options": {"include_usage": True},+ }++ response = litellm.completion(**data)+ print(response)++ stop_reason = None+ for chunk in response:+ print(chunk)+ if chunk.choices[0].finish_reason:+ stop_reason = chunk.choices[0].finish_reason+ assert stop_reason is not None+ assert stop_reason == "tool_calls"