feat: Mark Vertex calls made from non-gemini models

PiperOrigin-RevId: 864253424
This commit is contained in:
Google Team Member
2026-02-02 02:23:56 -08:00
committed by Copybara-Service
parent 9290b96626
commit 7d58e0d2f3
8 changed files with 231 additions and 32 deletions
+2
View File
@@ -36,6 +36,7 @@ from google.genai import types
from pydantic import BaseModel
from typing_extensions import override
from ..utils._google_client_headers import get_tracking_headers
from .base_llm import BaseLlm
from .llm_response import LlmResponse
@@ -345,4 +346,5 @@ class Claude(AnthropicLlm):
return AsyncAnthropicVertex(
project_id=os.environ["GOOGLE_CLOUD_PROJECT"],
region=os.environ["GOOGLE_CLOUD_LOCATION"],
default_headers=get_tracking_headers(),
)
+8 -24
View File
@@ -30,7 +30,8 @@ from google.genai import types
from google.genai.errors import ClientError
from typing_extensions import override
from ..utils._client_labels_utils import get_client_labels
from ..utils._google_client_headers import get_tracking_headers
from ..utils._google_client_headers import merge_tracking_headers
from ..utils.context_utils import Aclosing
from ..utils.streaming_utils import StreamingResponseAggregator
from ..utils.variant_utils import GoogleLLMVariant
@@ -316,13 +317,7 @@ class Gemini(BaseLlm):
)
def _tracking_headers(self) -> dict[str, str]:
labels = get_client_labels()
header_value = ' '.join(labels)
tracking_headers = {
'x-goog-api-client': header_value,
'user-agent': header_value,
}
return tracking_headers
return get_tracking_headers()
@cached_property
def _live_api_version(self) -> str:
@@ -362,8 +357,10 @@ class Gemini(BaseLlm):
):
if not llm_request.live_connect_config.http_options.headers:
llm_request.live_connect_config.http_options.headers = {}
llm_request.live_connect_config.http_options.headers.update(
self._tracking_headers()
llm_request.live_connect_config.http_options.headers = (
self._merge_tracking_headers(
llm_request.live_connect_config.http_options.headers
)
)
llm_request.live_connect_config.http_options.api_version = (
self._live_api_version
@@ -456,20 +453,7 @@ class Gemini(BaseLlm):
def _merge_tracking_headers(self, headers: dict[str, str]) -> dict[str, str]:
"""Merge tracking headers to the given headers."""
headers = headers or {}
for key, tracking_header_value in self._tracking_headers().items():
custom_value = headers.get(key, None)
if not custom_value:
headers[key] = tracking_header_value
continue
# Merge tracking headers with existing headers and avoid duplicates.
value_parts = tracking_header_value.split(' ')
for custom_value_part in custom_value.split(' '):
if custom_value_part not in value_parts:
value_parts.append(custom_value_part)
headers[key] = ' '.join(value_parts)
return headers
return merge_tracking_headers(headers)
def _build_function_declaration_log(
+21
View File
@@ -51,6 +51,7 @@ from pydantic import BaseModel
from pydantic import Field
from typing_extensions import override
from ..utils._google_client_headers import merge_tracking_headers
from .base_llm import BaseLlm
from .llm_request import LlmRequest
from .llm_response import LlmResponse
@@ -1699,6 +1700,18 @@ Functions:
"""
def _is_litellm_vertex_model(model_string: str) -> bool:
"""Check if the model is a Vertex AI model accessed via LiteLLM.
Args:
model_string: A LiteLLM model string (e.g., "vertex_ai/gemini-2.5-flash")
Returns:
True if it's a Vertex AI model accessed via LiteLLM, False otherwise
"""
return model_string.startswith("vertex_ai/")
def _is_litellm_gemini_model(model_string: str) -> bool:
"""Check if the model is a Gemini model accessed via LiteLLM.
@@ -1867,6 +1880,14 @@ class LiteLlm(BaseLlm):
}
completion_args.update(self._additional_args)
# merge headers
if _is_litellm_vertex_model(effective_model) or _is_litellm_gemini_model(
effective_model
):
completion_args["headers"] = merge_tracking_headers(
completion_args.get("headers")
)
if generation_params:
completion_args.update(generation_params)
@@ -0,0 +1,56 @@
# Copyright 2025 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from __future__ import annotations
from ._client_labels_utils import get_client_labels
def get_tracking_headers() -> dict[str, str]:
"""Returns a dictionary of HTTP headers for tracking API requests.
These headers are used to identify HTTP calls made by ADK towards
Vertex AI LLM APIs.
"""
labels = get_client_labels()
header_value = " ".join(labels)
return {
"x-goog-api-client": header_value,
"user-agent": header_value,
}
def merge_tracking_headers(headers: dict[str, str] | None) -> dict[str, str]:
"""Merge tracking headers to the given headers.
Args:
headers: headers to merge tracking headers into.
Returns:
A dictionary of HTTP headers with tracking headers merged.
"""
new_headers = (headers or {}).copy()
for key, tracking_header_value in get_tracking_headers().items():
custom_value = new_headers.get(key, None)
if not custom_value:
new_headers[key] = tracking_header_value
continue
# Merge tracking headers with existing headers and avoid duplicates.
value_parts = tracking_header_value.split(" ")
for custom_value_part in custom_value.split(" "):
if custom_value_part not in value_parts:
value_parts.append(custom_value_part)
new_headers[key] = " ".join(value_parts)
return new_headers