Skip to content

Commit 95404f2

Browse files
sararobcopybara-github
authored andcommitted
chore: Update user-agent header
PiperOrigin-RevId: 896025555
1 parent 0e5037d commit 95404f2

File tree

2 files changed

+26
-23
lines changed

2 files changed

+26
-23
lines changed

tests/unit/vertexai/genai/replays/conftest.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@
2323
client as vertexai_genai_client_module,
2424
)
2525
from vertexai._genai import _agent_engines_utils
26+
from vertexai._genai.client import (
27+
_GENAI_MODULES_TELEMETRY_HEADER,
28+
)
29+
from google.cloud.aiplatform import version as aip_version
2630
from google.cloud import storage, bigquery
2731
from google.genai import _replay_api_client
2832
from google.genai import types as genai_types
@@ -260,7 +264,10 @@ def client(use_vertex, replays_prefix, http_options, request):
260264
http_options = genai_types.HttpOptions()
261265
if http_options.headers is None:
262266
http_options.headers = {}
263-
http_options.headers["x-goog-vertex-sdk"] = "true"
267+
268+
tracking_label = f"{_GENAI_MODULES_TELEMETRY_HEADER}/{aip_version.__version__}"
269+
http_options.headers["user-agent"] = tracking_label
270+
http_options.headers["x-goog-api-client"] = tracking_label
264271

265272
replay_client = _replay_api_client.ReplayApiClient(
266273
mode=mode,

vertexai/_genai/client.py

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -41,27 +41,6 @@
4141

4242
_GENAI_MODULES_TELEMETRY_HEADER = "vertex-genai-modules"
4343

44-
genai_append_method = _api_client.append_library_version_headers
45-
46-
47-
def _add_tracking_headers(headers: dict[str, str]) -> None:
48-
"""Adds Vertex Gen AI tracking headers."""
49-
is_vertex = headers.get(
50-
"x-goog-vertex-sdk"
51-
) == "true" or "vertex-genai-modules" in headers.get("user-agent", "")
52-
if is_vertex:
53-
tracking_label = f"{_GENAI_MODULES_TELEMETRY_HEADER}/{aip_version.__version__}"
54-
55-
headers["user-agent"] = tracking_label
56-
headers["x-goog-api-client"] = tracking_label
57-
58-
headers.pop("x-goog-vertex-sdk", None)
59-
else:
60-
genai_append_method(headers)
61-
62-
63-
_api_client.append_library_version_headers = _add_tracking_headers
64-
6544

6645
class AsyncClient:
6746
"""Async Gen AI Client for the Vertex SDK."""
@@ -229,7 +208,24 @@ def __init__(
229208
http_options = types.HttpOptions()
230209
if http_options.headers is None:
231210
http_options.headers = {}
232-
http_options.headers["x-goog-vertex-sdk"] = "true"
211+
212+
tracking_label = (
213+
f"{_GENAI_MODULES_TELEMETRY_HEADER}/{aip_version.__version__}"
214+
)
215+
216+
if "user-agent" in http_options.headers:
217+
http_options.headers["user-agent"] = (
218+
f"{http_options.headers['user-agent']} {tracking_label}"
219+
)
220+
else:
221+
http_options.headers["user-agent"] = tracking_label
222+
223+
if "x-goog-api-client" in http_options.headers:
224+
http_options.headers["x-goog-api-client"] = (
225+
f"{http_options.headers['x-goog-api-client']} {tracking_label}"
226+
)
227+
else:
228+
http_options.headers["x-goog-api-client"] = tracking_label
233229

234230
self._api_client = genai_client.Client._get_api_client(
235231
vertexai=True,

0 commit comments

Comments
 (0)