Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion tests/unit/vertexai/genai/replays/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down
39 changes: 16 additions & 23 deletions vertexai/_genai/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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."""
Expand Down Expand Up @@ -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,
Expand Down
Loading