Skip to content

Conversation

Sameerlite
Copy link
Collaborator

Title

Add streamGenerateContent cost tracking in passthrough

Pre-Submission checklist

Please complete all items before asking a LiteLLM maintainer to review your PR

  • I have Added testing in the tests/litellm/ directory, Adding at least 1 test is a hard requirement - see details
  • I have added a screenshot of my new test passing locally
  • My PR passes all unit tests on make test-unit
  • My PR's scope is as isolated as possible, it only solves 1 specific problem

Type

🐛 Bug Fix

Changes

Problem

Passthrough streaming requests for Gemini endpoints were not calculating costs correctly. The streaming cost calculation was missing the logging_obj parameter needed for cost injection, and Gemini's unique fragmented JSON streaming format was not being parsed correctly.

https://www.loom.com/share/f2b38813059b4ea3ab5259f3fae31ea5?sid=9a6b6978-553b-4d0c-a658-1a39c8c61bdb

Copy link

vercel bot commented Oct 4, 2025

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Preview Comments Updated (UTC)
litellm Ready Ready Preview Comment Oct 4, 2025 7:27pm

Copy link
Contributor

@ishaan-jaff ishaan-jaff left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@ishaan-jaff ishaan-jaff merged commit 8095de5 into main Oct 4, 2025
37 of 49 checks passed
or ("streamRawPredict") in url
):
# Check if it's Gemini (Google AI Studio) or Vertex AI
if parsed_url.hostname and parsed_url.hostname.endswith("generativelanguage.googleapis.com"):

Check failure

Code scanning / CodeQL

Incomplete URL substring sanitization High

The string
generativelanguage.googleapis.com
may be at an arbitrary position in the sanitized URL.

Copilot Autofix

AI about 14 hours ago

The intended behavior is likely to allow requests made to generativelanguage.googleapis.com or any of its true subdomains (e.g., sub.generativelanguage.googleapis.com). We should ensure the hostname is either exactly generativelanguage.googleapis.com or ends with .generativelanguage.googleapis.com (with a dot), and not simply any string ending with that substring. This prevents hostnames like notgenerativelanguage.googleapis.com.evil.com from matching. To fix, locate the line in get_endpoint_type that does parsed_url.hostname.endswith("generativelanguage.googleapis.com") and replace it with a safe comparison ensuring it matches either exactly or a dot-subdomain. This requires no new methods but does require changing the check.

Suggested changeset 1
litellm/proxy/pass_through_endpoints/pass_through_endpoints.py

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/litellm/proxy/pass_through_endpoints/pass_through_endpoints.py b/litellm/proxy/pass_through_endpoints/pass_through_endpoints.py
--- a/litellm/proxy/pass_through_endpoints/pass_through_endpoints.py
+++ b/litellm/proxy/pass_through_endpoints/pass_through_endpoints.py
@@ -302,7 +302,10 @@
             or ("streamRawPredict") in url
         ):
             # Check if it's Gemini (Google AI Studio) or Vertex AI
-            if parsed_url.hostname and parsed_url.hostname.endswith("generativelanguage.googleapis.com"):
+            if parsed_url.hostname and (
+                parsed_url.hostname == "generativelanguage.googleapis.com"
+                or parsed_url.hostname.endswith(".generativelanguage.googleapis.com")
+            ):
                 return EndpointType.GEMINI
             return EndpointType.VERTEX_AI
         elif parsed_url.hostname == "api.anthropic.com":
EOF
@@ -302,7 +302,10 @@
or ("streamRawPredict") in url
):
# Check if it's Gemini (Google AI Studio) or Vertex AI
if parsed_url.hostname and parsed_url.hostname.endswith("generativelanguage.googleapis.com"):
if parsed_url.hostname and (
parsed_url.hostname == "generativelanguage.googleapis.com"
or parsed_url.hostname.endswith(".generativelanguage.googleapis.com")
):
return EndpointType.GEMINI
return EndpointType.VERTEX_AI
elif parsed_url.hostname == "api.anthropic.com":
Copilot is powered by AI and may make mistakes. Always verify output.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants