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]

fix(google_genai): price streamed generateContent with the provider that served it

Devin AI0c5583b83f03
diff --git a/litellm/google_genai/streaming_iterator.py b/litellm/google_genai/streaming_iterator.py
index e03f7ee745f..e2fac6a615b 100644
--- a/litellm/google_genai/streaming_iterator.py
+++ b/litellm/google_genai/streaming_iterator.py
@@ -2,6 +2,7 @@ import asyncio
from datetime import datetime
from typing import TYPE_CHECKING, Any, Final
+import litellm
from litellm.litellm_core_utils.litellm_logging import Logging as LiteLLMLoggingObj
from litellm.proxy.pass_through_endpoints.success_handler import (
PassThroughEndpointLogging,
@@ -65,6 +66,7 @@ class BaseGoogleGenAIGenerateContentStreamingIterator:
litellm_logging_obj: LiteLLMLoggingObj,
request_body: dict,
model: str,
+ custom_llm_provider: str,
hidden_params: dict[str, Any] | None = None,
):
self.litellm_logging_obj = litellm_logging_obj
@@ -72,6 +74,7 @@ class BaseGoogleGenAIGenerateContentStreamingIterator:
self.start_time = datetime.now()
self.collected_chunks: list[bytes] = []
self.model = model
+ self.custom_llm_provider = custom_llm_provider
self._hidden_params: dict[str, Any] = hidden_params or {}
async def _handle_async_streaming_logging(
@@ -83,13 +86,18 @@ class BaseGoogleGenAIGenerateContentStreamingIterator:
)
end_time: Final = datetime.now()
+ endpoint_type: Final = (
+ EndpointType.GEMINI
+ if self.custom_llm_provider == litellm.LlmProviders.GEMINI.value
+ else EndpointType.VERTEX_AI
+ )
asyncio.create_task(
PassThroughStreamingHandler._route_streaming_logging_to_handler(
litellm_logging_obj=self.litellm_logging_obj,
passthrough_success_handler_obj=GLOBAL_PASS_THROUGH_SUCCESS_HANDLER_OBJ,
url_route="/v1/generateContent",
request_body=self.request_body or {},
- endpoint_type=EndpointType.VERTEX_AI,
+ endpoint_type=endpoint_type,
start_time=self.start_time,
raw_bytes=self.collected_chunks,
end_time=end_time,
@@ -118,13 +126,13 @@ class GoogleGenAIGenerateContentStreamingIterator(BaseGoogleGenAIGenerateContent
litellm_logging_obj=logging_obj,
request_body=request_body or {},
model=model,
+ custom_llm_provider=custom_llm_provider,
hidden_params=hidden_params,
)
self.response = response
self.model = model
self.generate_content_provider_config = generate_content_provider_config
self.litellm_metadata = litellm_metadata
- self.custom_llm_provider = custom_llm_provider
# Gemini streamGenerateContent uses SSE line framing; iter_lines keeps
# large inlineData payloads (e.g. image/jpeg) intact within one event.
self.stream_iterator = response.iter_lines()
@@ -169,13 +177,13 @@ class AsyncGoogleGenAIGenerateContentStreamingIterator(BaseGoogleGenAIGenerateCo
litellm_logging_obj=logging_obj,
request_body=request_body or {},
model=model,
+ custom_llm_provider=custom_llm_provider,
hidden_params=hidden_params,
)
self.response = response
self.model = model
self.generate_content_provider_config = generate_content_provider_config
self.litellm_metadata = litellm_metadata
- self.custom_llm_provider = custom_llm_provider
# Gemini streamGenerateContent uses SSE line framing; aiter_lines keeps
# large inlineData payloads (e.g. image/jpeg) intact within one event.
self.stream_iterator = response.aiter_lines()
diff --git a/litellm/proxy/pass_through_endpoints/llm_provider_handlers/vertex_passthrough_logging_handler.py b/litellm/proxy/pass_through_endpoints/llm_provider_handlers/vertex_passthrough_logging_handler.py
index afd8684dd92..36455611c95 100644
--- a/litellm/proxy/pass_through_endpoints/llm_provider_handlers/vertex_passthrough_logging_handler.py
+++ b/litellm/proxy/pass_through_endpoints/llm_provider_handlers/vertex_passthrough_logging_handler.py
@@ -592,7 +592,7 @@ class VertexPassthroughLoggingHandler:
response_cost: Final = litellm.completion_cost(
completion_response=litellm_model_response,
model=model,
- custom_llm_provider="vertex_ai",
+ custom_llm_provider=custom_llm_provider,
)
kwargs["response_cost"] = response_cost
diff --git a/litellm/proxy/pass_through_endpoints/streaming_handler.py b/litellm/proxy/pass_through_endpoints/streaming_handler.py
index ff1c12d08d7..907c59d28cc 100644
--- a/litellm/proxy/pass_through_endpoints/streaming_handler.py
+++ b/litellm/proxy/pass_through_endpoints/streaming_handler.py
@@ -15,6 +15,9 @@ from litellm.types.utils import StandardPassThroughResponseObject
from .llm_provider_handlers.anthropic_passthrough_logging_handler import (
AnthropicPassthroughLoggingHandler,
)
+from .llm_provider_handlers.gemini_passthrough_logging_handler import (
+ GeminiPassthroughLoggingHandler,
+)
from .llm_provider_handlers.openai_passthrough_logging_handler import (
OpenAIPassthroughLoggingHandler,
)
@@ -221,6 +224,22 @@ class PassThroughStreamingHandler:
)
standard_logging_response_object = vertex_passthrough_logging_handler_result["result"]
kwargs = vertex_passthrough_logging_handler_result["kwargs"]
+ elif endpoint_type == EndpointType.GEMINI:
+ gemini_passthrough_logging_handler_result: Final = (
+ GeminiPassthroughLoggingHandler._handle_logging_gemini_collected_chunks(
+ litellm_logging_obj=litellm_logging_obj,
+ passthrough_success_handler_obj=passthrough_success_handler_obj,
+ url_route=url_route,
+ request_body=request_body,
+ endpoint_type=endpoint_type,
+ start_time=start_time,
+ all_chunks=all_chunks,
+ end_time=end_time,
+ model=model,
+ )
+ )
+ standard_logging_response_object = gemini_passthrough_logging_handler_result["result"]
+ kwargs = gemini_passthrough_logging_handler_result["kwargs"]
elif endpoint_type == EndpointType.OPENAI:
openai_passthrough_logging_handler_result: Final = (
OpenAIPassthroughLoggingHandler._handle_logging_openai_collected_chunks(
diff --git a/litellm/types/passthrough_endpoints/pass_through_endpoints.py b/litellm/types/passthrough_endpoints/pass_through_endpoints.py
index f59ca0d9041..548702e4139 100644
--- a/litellm/types/passthrough_endpoints/pass_through_endpoints.py
+++ b/litellm/types/passthrough_endpoints/pass_through_endpoints.py
@@ -22,6 +22,7 @@ LITELLM_PASS_THROUGH_ENDPOINT_MARKER: Final = "__litellm_pass_through_endpoint__
class EndpointType(str, Enum):
VERTEX_AI = "vertex-ai"
+ GEMINI = "gemini"
ANTHROPIC = "anthropic"
OPENAI = "openai"
GENERIC = "generic"
diff --git a/tests/test_litellm/google_genai/test_google_genai_streaming_iterator.py b/tests/test_litellm/google_genai/test_google_genai_streaming_iterator.py
index d74a05ec59c..91058767730 100644
--- a/tests/test_litellm/google_genai/test_google_genai_streaming_iterator.py
+++ b/tests/test_litellm/google_genai/test_google_genai_streaming_iterator.py
@@ -1,5 +1,6 @@
+import asyncio
import json
-from unittest.mock import MagicMock
+from unittest.mock import AsyncMock, MagicMock, patch
import pytest
@@ -8,6 +9,43 @@ from litellm.google_genai.streaming_iterator import (
GoogleGenAIGenerateContentStreamingIterator,
)
from litellm.litellm_core_utils.litellm_logging import Logging as LiteLLMLoggingObj
+from litellm.types.passthrough_endpoints.pass_through_endpoints import EndpointType
+
+
+ "custom_llm_provider, expected_endpoint_type",
+ [("gemini", EndpointType.GEMINI), ("vertex_ai", EndpointType.VERTEX_AI)],
+)
+async def test_streaming_logging_routes_to_the_provider_that_served_the_request(
+ custom_llm_provider, expected_endpoint_type
+):
+ """Routing every google stream through the vertex handler bills gemini/* at vertex_ai/ rates."""
+ mock_response = MagicMock()
+
+ async def _aiter_lines():
+ yield 'data: {"candidates": []}'
+
+ mock_response.aiter_lines = _aiter_lines
+
+ iterator = AsyncGoogleGenAIGenerateContentStreamingIterator(
+ response=mock_response,
+ model="gemini-3.1-flash-image",
+ logging_obj=MagicMock(spec=LiteLLMLoggingObj),
+ generate_content_provider_config=MagicMock(),
+ litellm_metadata={},
+ custom_llm_provider=custom_llm_provider,
+ )
+
+ with patch(
+ "litellm.proxy.pass_through_endpoints.streaming_handler.PassThroughStreamingHandler._route_streaming_logging_to_handler",
+ new=AsyncMock(),
+ ) as mock_route:
+ async for _ in iterator:
+ pass
+
+ await asyncio.sleep(0)
+ assert mock_route.call_args.kwargs["endpoint_type"] == expected_endpoint_type
def _large_inline_data_event() -> str:
diff --git a/tests/test_litellm/proxy/pass_through_endpoints/test_streaming_handler.py b/tests/test_litellm/proxy/pass_through_endpoints/test_streaming_handler.py
new file mode 100644
index 00000000000..d0c28fd60a9
--- /dev/null
+++ b/tests/test_litellm/proxy/pass_through_endpoints/test_streaming_handler.py
@@ -0,0 +1,99 @@
+import json
+from datetime import datetime
+from unittest.mock import MagicMock
+
+import pytest
+
+from litellm.litellm_core_utils.litellm_logging import Logging as LiteLLMLoggingObj
+from litellm.proxy.pass_through_endpoints.llm_provider_handlers.vertex_passthrough_logging_handler import (
+ VertexPassthroughLoggingHandler,
+)
+from litellm.proxy.pass_through_endpoints.streaming_handler import (
+ PassThroughStreamingHandler,
+)
+from litellm.proxy.pass_through_endpoints.success_handler import (
+ PassThroughEndpointLogging,
+)
+from litellm.types.passthrough_endpoints.pass_through_endpoints import EndpointType
+
+MODEL = "gemini-3.1-flash-image"
+
+# gemini/ rate card: 2.5e-07 in, 1.5e-06 out. vertex_ai/ rate card is exactly 2x that.
+GEMINI_COST = 1000 * 2.5e-07 + 1000 * 1.5e-06
+VERTEX_COST = 2 * GEMINI_COST
+
+
+def _chunks() -> list[str]:
+ payload = {
+ "candidates": [
+ {
+ "content": {"parts": [{"text": "hi"}], "role": "model"},
+ "finishReason": "STOP",
+ "index": 0,
+ }
+ ],
+ "usageMetadata": {
+ "promptTokenCount": 1000,
+ "candidatesTokenCount": 1000,
+ "totalTokenCount": 2000,
+ },
+ "modelVersion": MODEL,
+ }
+ return [f"data: {json.dumps(payload)}"]
+
+
+def _logging_obj() -> LiteLLMLoggingObj:
+ logging_obj = MagicMock(spec=LiteLLMLoggingObj)
+ logging_obj.model_call_details = {}
+ logging_obj.optional_params = {}
+ logging_obj.litellm_call_id = "test-call-id"
+ return logging_obj
+
+
+ "endpoint_type, expected_provider, expected_cost",
+ [
+ (EndpointType.GEMINI, "gemini", GEMINI_COST),
+ (EndpointType.VERTEX_AI, "vertex_ai", VERTEX_COST),
+ ],
+)
+def test_streaming_generate_content_bills_against_the_requested_provider(
+ endpoint_type, expected_provider, expected_cost
+):
+ """A streamed gemini/* request must not be priced off the vertex_ai/ rate card."""
+ logging_obj = _logging_obj()
+
+ _, kwargs = PassThroughStreamingHandler._build_passthrough_logging_result(
+ litellm_logging_obj=logging_obj,
+ passthrough_success_handler_obj=PassThroughEndpointLogging(),
+ url_route="/v1/generateContent",
+ request_body={},
+ endpoint_type=endpoint_type,
+ start_time=datetime.now(),
+ raw_bytes=[chunk.encode("utf-8") for chunk in _chunks()],
+ end_time=datetime.now(),
+ model=MODEL,
+ )
+
+ assert kwargs["response_cost"] == pytest.approx(expected_cost)
+ assert logging_obj.model_call_details["custom_llm_provider"] == expected_provider
+
+
+def test_vertex_generate_content_payload_prices_gemini_urls_at_gemini_rates():
+ """The AI Studio host resolves to `gemini`, so the cost must follow it, not the vertex_ai default."""
+ logging_obj = _logging_obj()
+
+ result = VertexPassthroughLoggingHandler._handle_logging_vertex_collected_chunks(
+ litellm_logging_obj=logging_obj,
+ passthrough_success_handler_obj=PassThroughEndpointLogging(),
+ url_route=f"https://generativelanguage.googleapis.com/v1beta/models/{MODEL}:streamGenerateContent",
+ request_body={},
+ endpoint_type=EndpointType.VERTEX_AI,
+ start_time=datetime.now(),
+ all_chunks=_chunks(),
+ model=MODEL,
+ end_time=datetime.now(),
+ )
+
+ assert result["kwargs"]["response_cost"] == pytest.approx(GEMINI_COST)
+ assert logging_obj.model_call_details["custom_llm_provider"] == "gemini"