|
15 | 15 |
|
16 | 16 | import asyncio |
17 | 17 | import importlib |
| 18 | +import sys |
18 | 19 | from typing import Optional, Union, TYPE_CHECKING |
19 | 20 | from types import TracebackType, ModuleType |
20 | 21 |
|
|
23 | 24 | from google.genai import _common |
24 | 25 | from google.genai import client as genai_client |
25 | 26 | 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 |
26 | 29 | from . import live |
27 | 30 |
|
28 | 31 | if TYPE_CHECKING: |
|
41 | 44 | _GENAI_MODULES_TELEMETRY_HEADER = "vertex-genai-modules" |
42 | 45 |
|
43 | 46 |
|
| 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 | + |
44 | 69 | class AsyncClient: |
45 | 70 | """Async Gen AI Client for the Vertex SDK.""" |
46 | 71 |
|
@@ -208,22 +233,6 @@ def __init__( |
208 | 233 | if http_options.headers is None: |
209 | 234 | http_options.headers = {} |
210 | 235 |
|
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 | | - |
227 | 236 | self._api_client = genai_client.Client._get_api_client( |
228 | 237 | vertexai=True, |
229 | 238 | api_key=api_key, |
|
0 commit comments