Skip to content

Commit c0e9c54

Browse files
google-genai-botcopybara-github
authored andcommitted
feat: Support base_url, api_version and headers in LiveConnectConfig.http_options
PiperOrigin-RevId: 1005783011
1 parent 30f2ef4 commit c0e9c54

3 files changed

Lines changed: 259 additions & 20 deletions

File tree

‎google/genai/_api_client.py‎

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1590,13 +1590,22 @@ def _httpx_url(self, url: str, *, is_async: bool) -> str:
15901590
)
15911591
return to_mtls_url(url) if use_mtls_endpoint else url
15921592

1593-
def _websocket_base_url(self) -> str:
1593+
def _websocket_base_url(
1594+
self, http_options: Optional[HttpOptions] = None
1595+
) -> str:
1596+
"""Returns the base URL for websocket connections.
1597+
1598+
Args:
1599+
http_options: The http options of the connection. Defaults to the
1600+
client's.
1601+
"""
1602+
http_options = http_options or self._http_options
15941603
has_sufficient_auth = (self.project and self.location) or self.api_key
15951604
if self.custom_base_url and not has_sufficient_auth:
15961605
# API gateway proxy can use the auth in custom headers, not url.
15971606
# Enable custom url if auth is not sufficient.
1598-
return self.custom_base_url
1599-
url_parts = urlparse(self._http_options.base_url)
1607+
return http_options.base_url or self.custom_base_url
1608+
url_parts = urlparse(http_options.base_url)
16001609
url = url_parts._replace(scheme='wss').geturl() # type: ignore[arg-type]
16011610
if self._websocket_use_mtls_endpoint:
16021611
url = to_mtls_url(url) # type: ignore[arg-type]

‎google/genai/live.py‎

Lines changed: 54 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
from . import types
3939
from ._adapters import McpToGenAiToolAdapter
4040
from ._api_client import BaseApiClient
41+
from ._api_client import patch_http_options
4142
from ._common import get_value_by_path as getv
4243
from ._common import set_value_by_path as setv
4344
from ._mcp_utils import mcp_to_gemini_tool
@@ -83,6 +84,11 @@ def _auth_requests() -> Any:
8384
' response of a ToolCall.FunctionalCalls in Google AI.'
8485
)
8586

87+
# The `LiveConnectConfig.http_options` fields that `connect()` supports.
88+
_LIVE_SESSION_HTTP_OPTIONS_FIELDS = frozenset(
89+
{'base_url', 'api_version', 'headers'}
90+
)
91+
8692

8793
def _is_interaction_complete(
8894
server_content: Optional[types.LiveServerContent],
@@ -1011,34 +1017,34 @@ async def connect(
10111017
10121018
Args:
10131019
model: The model to use for the live session.
1014-
config: The configuration for the live session.
1020+
config: The configuration for the live session. The `base_url`,
1021+
`api_version` and `headers` fields in the
1022+
`LiveConnectConfig.http_options` field can override the `base_url`,
1023+
`api_version` and `headers` fields in the `Client` class.
10151024
**kwargs: additional keyword arguments.
10161025
10171026
Yields:
10181027
An AsyncSession object.
10191028
"""
1020-
# TODO(b/404946570): Support per request http options.
10211029
if isinstance(config, dict):
10221030
config = types.LiveConnectConfig(**config)
1023-
if config and config.http_options:
1024-
raise ValueError(
1025-
'google.genai.client.aio.live.connect() does not support'
1026-
' http_options at request-level in LiveConnectConfig yet. Please use'
1027-
' the client-level http_options configuration instead.'
1028-
)
1031+
session_http_options = config.http_options if config else None
1032+
http_options = _merge_live_http_options(
1033+
self._api_client._http_options, session_http_options
1034+
)
10291035

1030-
base_url = self._api_client._websocket_base_url()
1036+
base_url = self._api_client._websocket_base_url(http_options)
10311037
if isinstance(base_url, bytes):
10321038
base_url = base_url.decode('utf-8')
10331039
transformed_model = t.t_model(self._api_client, model) # type: ignore
10341040

10351041
parameter_model = await _t_live_connect_config(self._api_client, config)
10361042

10371043
if self._api_client.api_key and not self._api_client.vertexai:
1038-
version = self._api_client._http_options.api_version
1044+
version = http_options.api_version
10391045
api_key = self._api_client.api_key
10401046
method = 'BidiGenerateContent'
1041-
original_headers = self._api_client._http_options.headers
1047+
original_headers = http_options.headers
10421048
headers = original_headers.copy() if original_headers is not None else {}
10431049
if api_key.startswith('auth_tokens/'):
10441050
warnings.warn(
@@ -1080,9 +1086,9 @@ async def connect(
10801086
elif self._api_client.api_key and self._api_client.vertexai:
10811087
# Headers already contains api key for express mode.
10821088
api_key = self._api_client.api_key
1083-
version = self._api_client._http_options.api_version
1089+
version = http_options.api_version
10841090
uri = f'{base_url}/ws/google.cloud.aiplatform.{version}.LlmBidiService/BidiGenerateContent'
1085-
original_headers = self._api_client._http_options.headers
1091+
original_headers = http_options.headers
10861092
headers = original_headers.copy() if original_headers is not None else {}
10871093

10881094
request_dict = _common.convert_to_dict(
@@ -1100,18 +1106,18 @@ async def connect(
11001106

11011107
request = json.dumps(request_dict)
11021108
else:
1103-
version = self._api_client._http_options.api_version
1109+
version = http_options.api_version
11041110
has_sufficient_auth = (
11051111
self._api_client.project and self._api_client.location
11061112
)
11071113
if self._api_client.custom_base_url and not has_sufficient_auth:
11081114
# API gateway proxy can use the auth in custom headers, not url.
11091115
# Enable custom url if auth is not sufficient.
1110-
uri = self._api_client.custom_base_url
1116+
uri = http_options.base_url or self._api_client.custom_base_url
11111117
# Keep the model as is.
11121118
transformed_model = model
11131119
# Do not get credentials for custom url.
1114-
original_headers = self._api_client._http_options.headers
1120+
original_headers = http_options.headers
11151121
headers = (
11161122
original_headers.copy() if original_headers is not None else {}
11171123
)
@@ -1136,7 +1142,7 @@ async def connect(
11361142
creds.refresh(auth_req) # type: ignore[no-untyped-call]
11371143
bearer_token = creds.token
11381144

1139-
original_headers = self._api_client._http_options.headers
1145+
original_headers = http_options.headers
11401146
headers = (
11411147
original_headers.copy() if original_headers is not None else {}
11421148
)
@@ -1228,6 +1234,37 @@ async def connect(
12281234
)
12291235

12301236

1237+
def _merge_live_http_options(
1238+
client_http_options: types.HttpOptions,
1239+
session_http_options: Optional[types.HttpOptions],
1240+
) -> types.HttpOptions:
1241+
"""Returns the client's http options patched with the session-level ones.
1242+
1243+
Args:
1244+
client_http_options: The client-level http options.
1245+
session_http_options: The http options from `LiveConnectConfig`. Only the
1246+
fields in `_LIVE_SESSION_HTTP_OPTIONS_FIELDS` are supported.
1247+
1248+
Raises:
1249+
ValueError: If `session_http_options` sets an unsupported field.
1250+
"""
1251+
if session_http_options is None:
1252+
return client_http_options
1253+
unsupported_fields = [
1254+
field
1255+
for field in type(session_http_options).model_fields
1256+
if field not in _LIVE_SESSION_HTTP_OPTIONS_FIELDS
1257+
and getattr(session_http_options, field) is not None
1258+
]
1259+
if unsupported_fields:
1260+
raise ValueError(
1261+
'google.genai.client.aio.live.connect() only supports `base_url`,'
1262+
' `api_version` and `headers` in LiveConnectConfig.http_options, but'
1263+
f' got: {", ".join(unsupported_fields)}.'
1264+
)
1265+
return patch_http_options(client_http_options, session_http_options)
1266+
1267+
12311268
async def _t_live_connect_config(
12321269
api_client: BaseApiClient,
12331270
config: Optional[types.LiveConnectConfigOrDict],

‎google/genai/tests/live/test_live.py‎

Lines changed: 193 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,40 @@ def test_websocket_base_url_no_auth_with_custom_base_url():
254254
assert api_client._websocket_base_url() == base_url
255255

256256

257+
def test_websocket_base_url_with_http_options():
258+
api_client = gl_client.BaseApiClient(
259+
api_key='google_api_key',
260+
http_options={'base_url': 'https://test.com'},
261+
)
262+
assert (
263+
api_client._websocket_base_url(
264+
types.HttpOptions(base_url='https://session-test.com')
265+
)
266+
== 'wss://session-test.com'
267+
)
268+
269+
270+
def test_websocket_base_url_no_auth_with_http_options():
271+
api_client = gl_client.BaseApiClient(
272+
vertexai=True,
273+
http_options={
274+
'base_url': 'https://test-api-gateway-proxy.com',
275+
'headers': {'Authorization': 'Bearer test_token'},
276+
},
277+
)
278+
api_client.project = None
279+
api_client.location = None
280+
281+
# The base url of the given http options is passed as is, like the client's.
282+
session_base_url = 'https://session-api-gateway-proxy.com'
283+
assert (
284+
api_client._websocket_base_url(
285+
types.HttpOptions(base_url=session_base_url)
286+
)
287+
== session_base_url
288+
)
289+
290+
257291
@pytest.mark.parametrize('vertexai', [True, False])
258292
@pytest.mark.asyncio
259293
async def test_async_session_send_text(mock_websocket, vertexai):
@@ -2286,6 +2320,165 @@ async def _test_connect():
22862320
await _test_connect()
22872321

22882322

2323+
async def _capture_connect(api_client, config):
2324+
"""Connects through a mock websocket and returns what reached it."""
2325+
mock_ws = AsyncMock()
2326+
mock_ws.recv = AsyncMock(
2327+
return_value=b'{"setupComplete": {"sessionId": "test_session_id"}}'
2328+
)
2329+
capture = {}
2330+
2331+
@contextlib.asynccontextmanager
2332+
async def mock_connect(uri, additional_headers=None, **kwargs):
2333+
capture['uri'] = uri
2334+
capture['headers'] = additional_headers
2335+
yield mock_ws
2336+
2337+
with patch.object(live, 'ws_connect', new=mock_connect):
2338+
async with live.AsyncLive(api_client).connect(
2339+
model='test-model', config=config
2340+
):
2341+
pass
2342+
capture['request'] = mock_ws.send.call_args[0][0]
2343+
return capture
2344+
2345+
2346+
@pytest.mark.asyncio
2347+
async def test_connect_with_session_http_options():
2348+
client = gl_client.BaseApiClient(
2349+
vertexai=False,
2350+
api_key='TEST_API_KEY',
2351+
http_options={'headers': {'x-client-header': 'client-value'}},
2352+
)
2353+
config = types.LiveConnectConfig(
2354+
http_options=types.HttpOptions(
2355+
base_url='https://session-base-url.com',
2356+
api_version='v1alpha',
2357+
headers={
2358+
'x-client-header': 'session-value',
2359+
'x-session-header': 'session-value',
2360+
},
2361+
)
2362+
)
2363+
2364+
capture = await _capture_connect(client, config)
2365+
2366+
assert capture['uri'] == (
2367+
'wss://session-base-url.com/ws/google.ai.generativelanguage.v1alpha'
2368+
'.GenerativeService.BidiGenerateContent'
2369+
)
2370+
assert capture['headers']['x-goog-api-key'] == 'TEST_API_KEY'
2371+
assert capture['headers']['x-client-header'] == 'session-value'
2372+
assert capture['headers']['x-session-header'] == 'session-value'
2373+
# The session-level options apply to this session only.
2374+
assert client._http_options.headers['x-client-header'] == 'client-value'
2375+
assert 'x-session-header' not in client._http_options.headers
2376+
# They configure the connection and are not sent in the setup message.
2377+
assert 'x-session-header' not in capture['request']
2378+
2379+
2380+
@pytest.mark.asyncio
2381+
async def test_connect_keeps_client_base_url():
2382+
client = gl_client.BaseApiClient(
2383+
vertexai=False,
2384+
api_key='TEST_API_KEY',
2385+
http_options={'base_url': 'https://client-base-url.com'},
2386+
)
2387+
config = types.LiveConnectConfig(
2388+
http_options=types.HttpOptions(
2389+
headers={'x-session-header': 'session-value'}
2390+
)
2391+
)
2392+
2393+
capture = await _capture_connect(client, config)
2394+
2395+
# Without a session-level base url, the client's is used.
2396+
assert capture['uri'] == (
2397+
'wss://client-base-url.com/ws/google.ai.generativelanguage.v1beta'
2398+
'.GenerativeService.BidiGenerateContent'
2399+
)
2400+
2401+
2402+
@pytest.mark.asyncio
2403+
async def test_connect_with_session_http_options_and_credentials():
2404+
client = gl_client.BaseApiClient(
2405+
vertexai=True,
2406+
project='test-project',
2407+
location='us-central1',
2408+
credentials=Credentials(token='provided_fake_token'),
2409+
http_options={'base_url': 'https://client-base-url.com'},
2410+
)
2411+
config = types.LiveConnectConfig(
2412+
http_options=types.HttpOptions(
2413+
api_version='v1',
2414+
headers={'x-session-header': 'session-value'},
2415+
)
2416+
)
2417+
2418+
capture = await _capture_connect(client, config)
2419+
2420+
# The client's base url is kept, the api version is overridden.
2421+
assert capture['uri'] == (
2422+
'wss://client-base-url.com/ws/google.cloud.aiplatform.v1'
2423+
'.LlmBidiService/BidiGenerateContent'
2424+
)
2425+
assert capture['headers']['Authorization'] == 'Bearer provided_fake_token'
2426+
assert capture['headers']['x-session-header'] == 'session-value'
2427+
2428+
2429+
@pytest.mark.asyncio
2430+
async def test_connect_with_session_http_options_and_custom_base_url(
2431+
monkeypatch,
2432+
):
2433+
for env_var in (
2434+
'GOOGLE_CLOUD_PROJECT',
2435+
'GOOGLE_CLOUD_LOCATION',
2436+
'GOOGLE_API_KEY',
2437+
'GEMINI_API_KEY',
2438+
):
2439+
monkeypatch.delenv(env_var, raising=False)
2440+
client = gl_client.BaseApiClient(
2441+
vertexai=True,
2442+
http_options={
2443+
'base_url': 'https://custom-base-url.com',
2444+
'headers': {'Authorization': 'Bearer custom_test_token'},
2445+
},
2446+
)
2447+
config = types.LiveConnectConfig(
2448+
http_options=types.HttpOptions(
2449+
base_url='https://session-base-url.com',
2450+
headers={'Authorization': 'Bearer session_test_token'},
2451+
)
2452+
)
2453+
2454+
capture = await _capture_connect(client, config)
2455+
2456+
assert capture['uri'] == 'https://session-base-url.com'
2457+
assert capture['headers']['Authorization'] == 'Bearer session_test_token'
2458+
2459+
2460+
@pytest.mark.parametrize(
2461+
'field, value',
2462+
[
2463+
('timeout', 1000),
2464+
('extra_body', {'setup': {}}),
2465+
('retry_options', {'attempts': 1}),
2466+
('async_client_args', {'open_timeout': 5}),
2467+
],
2468+
)
2469+
@pytest.mark.asyncio
2470+
async def test_connect_with_unsupported_session_http_options(field, value):
2471+
config = {'http_options': {'headers': {'x-header': 'value'}, field: value}}
2472+
2473+
with patch.object(live, 'ws_connect') as mock_ws_connect:
2474+
with pytest.raises(ValueError, match=field):
2475+
async with live.AsyncLive(mock_api_client()).connect(
2476+
model='test-model', config=config
2477+
):
2478+
pass
2479+
mock_ws_connect.assert_not_called()
2480+
2481+
22892482
@pytest.mark.parametrize('vertexai', [False])
22902483
@pytest.mark.asyncio
22912484
async def test_bidi_setup_to_api_with_auth_tokens(mock_websocket, vertexai):

0 commit comments

Comments
 (0)