Skip to content
Open
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
25 changes: 18 additions & 7 deletions vertexai/_genai/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,25 @@

_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:
"""Appends Vertex Gen AI modules tracking information to the request headers."""
if headers.get("x-goog-vertex-sdk", "") == "true":

tracking_label = f"{_GENAI_MODULES_TELEMETRY_HEADER}/{aip_version.__version__}"
tracking_label = f"{_GENAI_MODULES_TELEMETRY_HEADER}/{aip_version.__version__}"

user_agent = headers.get("user-agent", "")
if tracking_label not in user_agent:
headers["user-agent"] = f"{user_agent} {tracking_label}".strip()
user_agent = headers.get("user-agent", "")
if tracking_label not in user_agent:
headers["user-agent"] = f"{user_agent} {tracking_label}".strip()

api_client = headers.get("x-goog-api-client", "")
if tracking_label not in api_client:
headers["x-goog-api-client"] = f"{api_client} {tracking_label}".strip()
api_client = headers.get("x-goog-api-client", "")
if tracking_label not in api_client:
headers["x-goog-api-client"] = f"{api_client} {tracking_label}".strip()
del headers["x-goog-vertex-sdk"]
else:
genai_append_method(headers)


_api_client.append_library_version_headers = _add_tracking_headers
Expand Down Expand Up @@ -221,6 +227,11 @@ def __init__(
self._debug_config = debug_config or genai_client.DebugConfig()
if isinstance(http_options, dict):
http_options = types.HttpOptions(**http_options)
if http_options is None:
http_options = types.HttpOptions()
if http_options.headers is None:
http_options.headers = {}
http_options.headers["x-goog-vertex-sdk"] = "true"

self._api_client = genai_client.Client._get_api_client(
vertexai=True,
Expand Down
Loading