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]

Merge branch 'main' into litellm_dev_09_01_2025_p1

Krish Dholakiaa3ffc3060e4f
diff --cc litellm/llms/vertex_ai/gemini/vertex_and_google_ai_studio_gemini.py
index a376e0dd06e,099b5c67069..9376b28cbec
--- 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
@@@ -794,11 -817,28 +818,29 @@@ class VertexGeminiConfig(VertexAIBaseCo
return content_str, reasoning_content_str
+ def _extract_thinking_blocks_from_parts(
+ self, parts: List[HttpxPartType]
+ ) -> List[ChatCompletionThinkingBlock]:
+ """Extract thinking blocks from parts if present"""
+ thinking_blocks: List[ChatCompletionThinkingBlock] = []
+ for part in parts:
+ if "thoughtSignature" in part:
+ part_copy = part.copy()
+ part_copy.pop("thoughtSignature")
+ thinking_blocks.append(
+ ChatCompletionThinkingBlock(
+ type="thinking",
+ thinking=json.dumps(part_copy),
+ signature=part["thoughtSignature"],
+ )
+ )
+ return thinking_blocks
+
def _extract_image_response_from_parts(
self, parts: List[HttpxPartType]
- ) -> Optional[ImageURLObject]:
+ ) -> Optional[List[ImageURLListItem]]:
"""Extract image response from parts if present"""
+ images: List[ImageURLListItem] = []
for part in parts:
if "inlineData" in part:
mime_type = part["inlineData"]["mimeType"]
diff --cc tests/llm_translation/test_gemini.py
index 726371bd4e7,9378c0305e6..5bb4440d4d3
--- a/tests/llm_translation/test_gemini.py
+++ b/tests/llm_translation/test_gemini.py
@@@ -625,12 -625,11 +625,11 @@@ async def test_gemini_image_generation_
async for chunk in response:
print("CHUNK: ", chunk)
if (
- hasattr(chunk.choices[0].delta, "image")
- and chunk.choices[0].delta.image is not None
+ hasattr(chunk.choices[0].delta, "images")
+ and chunk.choices[0].delta.images is not None
+ and len(chunk.choices[0].delta.images) > 0
):
- model_response_image = chunk.choices[0].delta.image
- print("MODEL_RESPONSE_IMAGE: ", model_response_image)
+ model_response_image = chunk.choices[0].delta.images[0]["image_url"]
- print("MODEL_RESPONSE_IMAGE: ", model_response_image)
assert model_response_image is not None
assert model_response_image["url"].startswith("data:image/png;base64,")
break
@@@ -660,3 -659,177 +659,176 @@@ def test_system_message_with_no_user_me
assert response is not None
assert response.choices[0].message.content is not None
-
-
++
+ def get_current_weather(location, unit="fahrenheit"):
+ """Get the current weather in a given location"""
+ if "tokyo" in location.lower():
+ return json.dumps({"location": "Tokyo", "temperature": "10", "unit": "celsius"})
+ elif "san francisco" in location.lower():
+ return json.dumps(
+ {"location": "San Francisco", "temperature": "72", "unit": "fahrenheit"}
+ )
+ elif "paris" in location.lower():
+ return json.dumps({"location": "Paris", "temperature": "22", "unit": "celsius"})
+ else:
+ return json.dumps({"location": location, "temperature": "unknown"})
+
+
+ def test_gemini_with_thinking():
+ from litellm import completion
+
+ litellm._turn_on_debug()
+ litellm.modify_params = True
+ model = "gemini/gemini-2.5-flash"
+ messages = [
+ {
+ "role": "user",
+ "content": "What's the weather like in San Francisco, Tokyo, and Paris? - give me 3 responses",
+ }
+ ]
+
+ tools = [
+ {
+ "type": "function",
+ "function": {
+ "name": "get_current_weather",
+ "description": "Get the current weather in a given location",
+ "parameters": {
+ "type": "object",
+ "properties": {
+ "location": {
+ "type": "string",
+ "description": "The city and state",
+ },
+ "unit": {
+ "type": "string",
+ "enum": ["celsius", "fahrenheit"],
+ },
+ },
+ "required": ["location"],
+ },
+ },
+ }
+ ]
+ response = litellm.completion(
+ model=model,
+ messages=messages,
+ tools=tools,
+ tool_choice="auto", # auto is default, but we'll be explicit
+ reasoning_effort="low",
+ )
+ print("Response\n", response)
+ response_message = response.choices[0].message
+ tool_calls = response_message.tool_calls
+
+ print("Expecting there to be 3 tool calls")
+ assert len(tool_calls) > 0 # this has to call the function for SF, Tokyo and paris
+
+ # Step 2: check if the model wanted to call a function
+ print(f"tool_calls: {tool_calls}")
+ if tool_calls:
+ # Step 3: call the function
+ # Note: the JSON response may not always be valid; be sure to handle errors
+ available_functions = {
+ "get_current_weather": get_current_weather,
+ } # only one function in this example, but you can have multiple
+ messages.append(response_message) # extend conversation with assistant's reply
+ print("Response message\n", response_message)
+ # Step 4: send the info for each function call and function response to the model
+ for tool_call in tool_calls:
+ function_name = tool_call.function.name
+ if function_name not in available_functions:
+ # the model called a function that does not exist in available_functions - don't try calling anything
+ return
+ function_to_call = available_functions[function_name]
+ function_args = json.loads(tool_call.function.arguments)
+ function_response = function_to_call(
+ location=function_args.get("location"),
+ unit=function_args.get("unit"),
+ )
+ messages.append(
+ {
+ "tool_call_id": tool_call.id,
+ "role": "tool",
+ "name": function_name,
+ "content": function_response,
+ }
+ ) # extend conversation with function response
+ print(f"messages: {messages}")
+ second_response = litellm.completion(
+ model=model,
+ messages=messages,
+ seed=22,
+ reasoning_effort="low",
+ tools=tools,
+ drop_params=True,
+ ) # get a new response from the model where it can see the function response
+ print("second response\n", second_response)
+
+
+ def test_gemini_reasoning_effort_minimal():
+ """
+ Test that reasoning_effort='minimal' correctly maps to model-specific minimum thinking budgets
+ """
+ from litellm.utils import return_raw_request
+ from litellm.types.utils import CallTypes
+ import json
+
+ # Test with different Gemini models to verify model-specific mapping
+ test_cases = [
+ ("gemini/gemini-2.5-flash", 1), # Flash: minimum 1 token
+ ("gemini/gemini-2.5-pro", 128), # Pro: minimum 128 tokens
+ ("gemini/gemini-2.5-flash-lite", 512), # Flash-Lite: minimum 512 tokens
+ ]
+
+ for model, expected_min_budget in test_cases:
+ # Get the raw request to verify the thinking budget mapping
+ raw_request = return_raw_request(
+ endpoint=CallTypes.completion,
+ kwargs={
+ "model": model,
+ "messages": [{"role": "user", "content": "Hello"}],
+ "reasoning_effort": "minimal",
+ },
+ )
+
+ # Verify that the thinking config is set correctly
+ request_body = raw_request["raw_request_body"]
+ assert "generationConfig" in request_body, f"Model {model} should have generationConfig"
+
+ generation_config = request_body["generationConfig"]
+ assert "thinkingConfig" in generation_config, f"Model {model} should have thinkingConfig"
+
+ thinking_config = generation_config["thinkingConfig"]
+ assert "thinkingBudget" in thinking_config, f"Model {model} should have thinkingBudget"
+
+ actual_budget = thinking_config["thinkingBudget"]
+ assert actual_budget == expected_min_budget, \
+ f"Model {model} should map 'minimal' to {expected_min_budget} tokens, got {actual_budget}"
+
+ # Verify that includeThoughts is True for minimal reasoning effort
+ assert thinking_config.get("includeThoughts", True), \
+ f"Model {model} should have includeThoughts=True for minimal reasoning effort"
+
+ # Test with unknown model (should use generic fallback)
+ try:
+ raw_request = return_raw_request(
+ endpoint=CallTypes.completion,
+ kwargs={
+ "model": "gemini/unknown-model",
+ "messages": [{"role": "user", "content": "Hello"}],
+ "reasoning_effort": "minimal",
+ },
+ )
+
+ request_body = raw_request["raw_request_body"]
+ generation_config = request_body["generationConfig"]
+ thinking_config = generation_config["thinkingConfig"]
+ # Should use generic fallback (128 tokens)
+ assert thinking_config["thinkingBudget"] == 128, \
+ "Unknown model should use generic fallback of 128 tokens"
+ except Exception as e:
+ # If return_raw_request doesn't work for unknown models, that's okay
+ # The important part is that our known models work correctly
+ print(f"Note: Unknown model test skipped due to: {e}")
+ pass