From dd30420865841dc59b74c33344202aad7e0dad02 Mon Sep 17 00:00:00 2001 From: Sara Robinson Date: Thu, 9 Apr 2026 10:26:50 -0700 Subject: [PATCH] chore: Update user-agent header PiperOrigin-RevId: 897181899 --- tests/unit/vertexai/genai/replays/conftest.py | 9 ++++- vertexai/_genai/client.py | 39 ++++++++----------- 2 files changed, 24 insertions(+), 24 deletions(-) diff --git a/tests/unit/vertexai/genai/replays/conftest.py b/tests/unit/vertexai/genai/replays/conftest.py index efcf2d3002..0f9eed0c5b 100644 --- a/tests/unit/vertexai/genai/replays/conftest.py +++ b/tests/unit/vertexai/genai/replays/conftest.py @@ -23,6 +23,10 @@ client as vertexai_genai_client_module, ) from vertexai._genai import _agent_engines_utils +from vertexai._genai.client import ( + _GENAI_MODULES_TELEMETRY_HEADER, +) +from google.cloud.aiplatform import version as aip_version from google.cloud import storage, bigquery from google.genai import _replay_api_client from google.genai import types as genai_types @@ -260,7 +264,10 @@ def client(use_vertex, replays_prefix, http_options, request): http_options = genai_types.HttpOptions() if http_options.headers is None: http_options.headers = {} - http_options.headers["x-goog-vertex-sdk"] = "true" + + tracking_label = f"{_GENAI_MODULES_TELEMETRY_HEADER}/{aip_version.__version__}" + http_options.headers["user-agent"] = tracking_label + http_options.headers["x-goog-api-client"] = tracking_label replay_client = _replay_api_client.ReplayApiClient( mode=mode, diff --git a/vertexai/_genai/client.py b/vertexai/_genai/client.py index 29f81d2e0f..2e43782554 100644 --- a/vertexai/_genai/client.py +++ b/vertexai/_genai/client.py @@ -20,7 +20,6 @@ import google.auth from google.cloud.aiplatform import version as aip_version -from google.genai import _api_client from google.genai import _common from google.genai import client as genai_client from google.genai import types @@ -41,27 +40,6 @@ _GENAI_MODULES_TELEMETRY_HEADER = "vertex-genai-modules" -genai_append_method = _api_client.append_library_version_headers - - -def _add_tracking_headers(headers: dict[str, str]) -> None: - """Adds Vertex Gen AI tracking headers.""" - is_vertex = headers.get( - "x-goog-vertex-sdk" - ) == "true" or "vertex-genai-modules" in headers.get("user-agent", "") - if is_vertex: - tracking_label = f"{_GENAI_MODULES_TELEMETRY_HEADER}/{aip_version.__version__}" - - headers["user-agent"] = tracking_label - headers["x-goog-api-client"] = tracking_label - - headers.pop("x-goog-vertex-sdk", None) - else: - genai_append_method(headers) - - -_api_client.append_library_version_headers = _add_tracking_headers - class AsyncClient: """Async Gen AI Client for the Vertex SDK.""" @@ -229,7 +207,22 @@ def __init__( http_options = types.HttpOptions() if http_options.headers is None: http_options.headers = {} - http_options.headers["x-goog-vertex-sdk"] = "true" + + tracking_label = f"{_GENAI_MODULES_TELEMETRY_HEADER}/{aip_version.__version__}" + + if "user-agent" in http_options.headers: + http_options.headers["user-agent"] = ( + f"{http_options.headers['user-agent']} {tracking_label}" + ) + else: + http_options.headers["user-agent"] = tracking_label + + if "x-goog-api-client" in http_options.headers: + http_options.headers["x-goog-api-client"] = ( + f"{http_options.headers['x-goog-api-client']} {tracking_label}" + ) + else: + http_options.headers["x-goog-api-client"] = tracking_label self._api_client = genai_client.Client._get_api_client( vertexai=True,