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_11_2025_p1

Krish Dholakia12a1d081ee53
diff --cc tests/llm_translation/test_gemini.py
index 122429a87a5,871aebdc9fd..64e66f3914c
--- a/tests/llm_translation/test_gemini.py
+++ b/tests/llm_translation/test_gemini.py
@@@ -269,13 -267,122 +267,126 @@@ def test_gemini_image_generation()
assert len(response.choices[0].message.images) > 0
assert response.choices[0].message.images[0]["image_url"] is not None
assert response.choices[0].message.images[0]["image_url"]["url"] is not None
- assert response.choices[0].message.images[0]["image_url"]["url"].startswith("data:image/png;base64,")
+ assert (
+ response.choices[0]
+ .message.images[0]["image_url"]["url"]
+ .startswith("data:image/png;base64,")
+ )
+ def test_gemini_2_5_flash_image_preview():
+ """
+ Test for GitHub issue #14120 - gemini-2.5-flash-image-preview model routing fix
+ Validates that the model correctly routes to image generation instead of chat completion
+ """
+ from unittest.mock import patch, MagicMock
+ from litellm.types.utils import ImageResponse, ImageObject
+
+ # Mock successful response to avoid API limits
+ mock_response = ImageResponse()
+ mock_response.data = [ImageObject(b64_json="test_base64_data", url=None)]
+
+ with patch("litellm.llms.custom_httpx.llm_http_handler.HTTPHandler.post") as mock_post:
+ # Mock successful HTTP response
+ mock_http_response = MagicMock()
+ mock_http_response.json.return_value = {
+ "candidates": [
+ {
+ "content": {
+ "parts": [
+ {
+ "inlineData": {
+ "data": "test_base64_image_data"
+ }
+ }
+ ]
+ }
+ }
+ ]
+ }
+ mock_http_response.status_code = 200
+ mock_post.return_value = mock_http_response
+
+ # Test that the function works without throwing the original 400 error
+ response = litellm.image_generation(
+ model="gemini/gemini-2.5-flash-image-preview",
+ prompt="Generate a simple test image",
+ api_key="test_api_key"
+ )
+
+ # Validate response structure
+ assert response is not None
+ assert hasattr(response, 'data')
+ assert response.data is not None
+ assert len(response.data) > 0
+
+ # Validate the correct endpoint was called
+ mock_post.assert_called_once()
+ call_args = mock_post.call_args
+ called_url = call_args[0][0] if call_args[0] else call_args.kwargs.get('url', '')
+
+ # Verify it uses generateContent endpoint for gemini-2.5-flash-image-preview (not predict)
+ assert ":generateContent" in called_url
+ assert "gemini-2.5-flash-image-preview" in called_url
+
+ # Verify request format is Gemini format (not Imagen)
+ request_data = call_args.kwargs.get('json', {})
+ assert "contents" in request_data
+ assert "parts" in request_data["contents"][0]
+
+ # Verify response_modalities is set correctly for image generation
+ assert "generationConfig" in request_data
+ assert "response_modalities" in request_data["generationConfig"]
+ assert request_data["generationConfig"]["response_modalities"] == ["IMAGE", "TEXT"]
+
+
+ def test_gemini_imagen_models_use_predict_endpoint():
+ """
+ Test that Imagen models still use :predict endpoint (not broken by gemini-2.5-flash-image-preview fix)
+ """
+ from unittest.mock import patch, MagicMock
+ from litellm.types.utils import ImageResponse, ImageObject
+
+ with patch("litellm.llms.custom_httpx.llm_http_handler.HTTPHandler.post") as mock_post:
+ # Mock successful HTTP response for Imagen
+ mock_http_response = MagicMock()
+ mock_http_response.json.return_value = {
+ "predictions": [
+ {
+ "bytesBase64Encoded": "test_base64_image_data"
+ }
+ ]
+ }
+ mock_http_response.status_code = 200
+ mock_post.return_value = mock_http_response
+
+ # Test an Imagen model
+ response = litellm.image_generation(
+ model="gemini/imagen-3.0-generate-001",
+ prompt="Generate a simple test image",
+ api_key="test_api_key"
+ )
+
+ # Validate response structure
+ assert response is not None
+ assert hasattr(response, 'data')
+
+ # Validate the correct endpoint was called for Imagen models
+ mock_post.assert_called_once()
+ call_args = mock_post.call_args
+ called_url = call_args[0][0] if call_args[0] else call_args.kwargs.get('url', '')
+
+ # Verify Imagen models use predict endpoint (not generateContent)
+ assert ":predict" in called_url
+ assert "imagen-3.0-generate-001" in called_url
+ assert ":generateContent" not in called_url
+
+ # Verify request format is Imagen format (not Gemini)
+ request_data = call_args.kwargs.get('json', {})
+ assert "instances" in request_data
+ assert "parameters" in request_data
+
+
def test_gemini_thinking():
litellm._turn_on_debug()
from litellm.types.utils import Message, CallTypes