Skip to content

Commit 4c8f516

Browse files
sararobcopybara-github
authored andcommitted
chore: Update user-agent header
PiperOrigin-RevId: 896025555
1 parent a8085e5 commit 4c8f516

File tree

2 files changed

+41
-16
lines changed

2 files changed

+41
-16
lines changed

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
client as vertexai_genai_client_module,
2424
)
2525
from vertexai._genai import _agent_engines_utils
26+
from vertexai._genai.client import _GENAI_MODULES_TELEMETRY_HEADER
27+
from google.cloud.aiplatform import version as aip_version
2628
from google.cloud import storage, bigquery
2729
from google.genai import _replay_api_client
2830
from google.genai import types as genai_types
@@ -260,7 +262,10 @@ def client(use_vertex, replays_prefix, http_options, request):
260262
http_options = genai_types.HttpOptions()
261263
if http_options.headers is None:
262264
http_options.headers = {}
263-
http_options.headers["x-goog-vertex-sdk"] = "true"
265+
266+
tracking_label = f"{_GENAI_MODULES_TELEMETRY_HEADER}/{aip_version.__version__}"
267+
http_options.headers["user-agent"] = tracking_label
268+
http_options.headers["x-goog-api-client"] = tracking_label
264269

265270
replay_client = _replay_api_client.ReplayApiClient(
266271
mode=mode,

vertexai/_genai/client.py

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -41,26 +41,29 @@
4141

4242
_GENAI_MODULES_TELEMETRY_HEADER = "vertex-genai-modules"
4343

44-
genai_append_method = _api_client.append_library_version_headers
44+
# genai_append_method = _api_client.append_library_version_headers
4545

4646

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__}"
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 = (
54+
# f"{_GENAI_MODULES_TELEMETRY_HEADER}/{aip_version.__version__} "
55+
# f"gl-python/{aip_version.__version__}"
56+
# )
5457

55-
headers["user-agent"] = tracking_label
56-
headers["x-goog-api-client"] = tracking_label
58+
# headers["user-agent"] = tracking_label
59+
# headers["x-goog-api-client"] = tracking_label
5760

58-
headers.pop("x-goog-vertex-sdk", None)
59-
else:
60-
genai_append_method(headers)
61+
# headers.pop("x-goog-vertex-sdk", None)
62+
# else:
63+
# genai_append_method(headers)
6164

6265

63-
_api_client.append_library_version_headers = _add_tracking_headers
66+
# _api_client.append_library_version_headers = _add_tracking_headers
6467

6568

6669
class AsyncClient:
@@ -229,7 +232,24 @@ def __init__(
229232
http_options = types.HttpOptions()
230233
if http_options.headers is None:
231234
http_options.headers = {}
232-
http_options.headers["x-goog-vertex-sdk"] = "true"
235+
236+
tracking_label = (
237+
f"{_GENAI_MODULES_TELEMETRY_HEADER}/{aip_version.__version__}"
238+
)
239+
240+
if "user-agent" in http_options.headers:
241+
http_options.headers["user-agent"] = (
242+
f"{tracking_label} {http_options.headers['user-agent']}"
243+
)
244+
else:
245+
http_options.headers["user-agent"] = tracking_label
246+
247+
if "x-goog-api-client" in http_options.headers:
248+
http_options.headers["x-goog-api-client"] = (
249+
f"{tracking_label} {http_options.headers['x-goog-api-client']}"
250+
)
251+
else:
252+
http_options.headers["x-goog-api-client"] = tracking_label
233253

234254
self._api_client = genai_client.Client._get_api_client(
235255
vertexai=True,

0 commit comments

Comments
 (0)