Skip to content

Commit 58358df

Browse files
sararobcopybara-github
authored andcommitted
chore: Update headers
PiperOrigin-RevId: 908716648
1 parent 68f053e commit 58358df

2 files changed

Lines changed: 25 additions & 20 deletions

File tree

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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -265,10 +265,6 @@ def client(use_vertex, replays_prefix, http_options, request):
265265
if http_options.headers is None:
266266
http_options.headers = {}
267267

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
271-
272268
replay_client = _replay_api_client.ReplayApiClient(
273269
mode=mode,
274270
replay_id=replay_id,

vertexai/_genai/client.py

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
import asyncio
1717
import importlib
18+
import sys
1819
from typing import Optional, Union, TYPE_CHECKING
1920
from types import TracebackType, ModuleType
2021

@@ -23,6 +24,8 @@
2324
from google.genai import _common
2425
from google.genai import client as genai_client
2526
from google.genai import types
27+
from google.genai import _api_client as genai_api_client
28+
from google.genai import version as genai_version
2629
from . import live
2730

2831
if TYPE_CHECKING:
@@ -41,6 +44,28 @@
4144
_GENAI_MODULES_TELEMETRY_HEADER = "vertex-genai-modules"
4245

4346

47+
def custom_append_library_version_headers(headers: dict[str, str]) -> None:
48+
"""Custom header injector for vertex-genai-modules."""
49+
genai_sdk_version = genai_version.__version__
50+
module_version = aip_version.__version__
51+
python_version = sys.version.split()[0]
52+
53+
custom_header = f"google-genai-sdk/{genai_sdk_version}+{_GENAI_MODULES_TELEMETRY_HEADER}/{module_version} gl-python/{python_version}"
54+
55+
if 'user-agent' in headers and custom_header not in headers['user-agent']:
56+
headers['user-agent'] = f"{custom_header} " + headers['user-agent']
57+
elif 'user-agent' not in headers:
58+
headers['user-agent'] = custom_header
59+
60+
if 'x-goog-api-client' in headers and custom_header not in headers['x-goog-api-client']:
61+
headers['x-goog-api-client'] = f"{custom_header} " + headers['x-goog-api-client']
62+
elif 'x-goog-api-client' not in headers:
63+
headers['x-goog-api-client'] = custom_header
64+
65+
# Apply the monkeypatch to override the base SDK behavior
66+
genai_api_client.append_library_version_headers = custom_append_library_version_headers
67+
68+
4469
class AsyncClient:
4570
"""Async Gen AI Client for the Vertex SDK."""
4671

@@ -208,22 +233,6 @@ def __init__(
208233
if http_options.headers is None:
209234
http_options.headers = {}
210235

211-
tracking_label = f"{_GENAI_MODULES_TELEMETRY_HEADER}/{aip_version.__version__}"
212-
213-
if "user-agent" in http_options.headers:
214-
http_options.headers["user-agent"] = (
215-
f"{http_options.headers['user-agent']} {tracking_label}"
216-
)
217-
else:
218-
http_options.headers["user-agent"] = tracking_label
219-
220-
if "x-goog-api-client" in http_options.headers:
221-
http_options.headers["x-goog-api-client"] = (
222-
f"{http_options.headers['x-goog-api-client']} {tracking_label}"
223-
)
224-
else:
225-
http_options.headers["x-goog-api-client"] = tracking_label
226-
227236
self._api_client = genai_client.Client._get_api_client(
228237
vertexai=True,
229238
api_key=api_key,

0 commit comments

Comments
 (0)