|
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 version as genai_version |
| 28 | +from google.genai import _api_client as genai_api_client |
26 | 29 | from . import live |
27 | 30 |
|
28 | 31 | if TYPE_CHECKING: |
|
42 | 45 | _GENAI_MODULES_TELEMETRY_HEADER = "vertex-genai-modules" |
43 | 46 |
|
44 | 47 |
|
| 48 | +def _custom_append_library_version_headers(headers: dict[str, str]) -> None: |
| 49 | + """Overridde GenAI SDK header injection to use custom vertex-genai-modules header.""" |
| 50 | + genai_sdk_version = genai_version.__version__ |
| 51 | + module_version = aip_version.__version__ |
| 52 | + python_version = sys.version.split()[0] |
| 53 | + |
| 54 | + combined_label = f"google-genai-sdk/{genai_sdk_version}+{_GENAI_MODULES_TELEMETRY_HEADER}/{module_version}" |
| 55 | + full_header = f"{combined_label} gl-python/{python_version}" |
| 56 | + |
| 57 | + if "user-agent" not in headers or combined_label not in headers["user-agent"]: |
| 58 | + headers["user-agent"] = f"{full_header} " + headers.get("user-agent", "") |
| 59 | + headers["user-agent"] = headers["user-agent"].strip() |
| 60 | + |
| 61 | + if ( |
| 62 | + "x-goog-api-client" not in headers |
| 63 | + or combined_label not in headers["x-goog-api-client"] |
| 64 | + ): |
| 65 | + headers["x-goog-api-client"] = f"{full_header} " + headers.get( |
| 66 | + "x-goog-api-client", "" |
| 67 | + ) |
| 68 | + headers["x-goog-api-client"] = headers["x-goog-api-client"].strip() |
| 69 | + |
| 70 | + |
| 71 | +genai_api_client.append_library_version_headers = _custom_append_library_version_headers |
| 72 | + |
| 73 | + |
45 | 74 | class AsyncClient: |
46 | 75 | """Async Gen AI Client for the Vertex SDK.""" |
47 | 76 |
|
@@ -219,22 +248,6 @@ def __init__( |
219 | 248 | if http_options.headers is None: |
220 | 249 | http_options.headers = {} |
221 | 250 |
|
222 | | - tracking_label = f"{_GENAI_MODULES_TELEMETRY_HEADER}/{aip_version.__version__}" |
223 | | - |
224 | | - if "user-agent" in http_options.headers: |
225 | | - http_options.headers["user-agent"] = ( |
226 | | - f"{http_options.headers['user-agent']} {tracking_label}" |
227 | | - ) |
228 | | - else: |
229 | | - http_options.headers["user-agent"] = tracking_label |
230 | | - |
231 | | - if "x-goog-api-client" in http_options.headers: |
232 | | - http_options.headers["x-goog-api-client"] = ( |
233 | | - f"{http_options.headers['x-goog-api-client']} {tracking_label}" |
234 | | - ) |
235 | | - else: |
236 | | - http_options.headers["x-goog-api-client"] = tracking_label |
237 | | - |
238 | 251 | self._api_client = genai_client.Client._get_api_client( |
239 | 252 | vertexai=True, |
240 | 253 | api_key=api_key, |
|
0 commit comments