Skip to content

fix(live): map http base_url to ws and omit ssl kwarg for ws:// URIs - #3462

Open
Roxen-AS wants to merge 2 commits into
googleapis:mainfrom
Roxen-AS:fix/live-websocket-http-scheme
Open

Roxen-AS wants to merge 2 commits into
googleapis:mainfrom
Roxen-AS:fix/live-websocket-http-scheme

Conversation

@Roxen-AS

@Roxen-AS Roxen-AS commented Dec 3, 2026

Copy link
Copy Markdown

Summary

Fixes #3451.

When connecting to the Live API over a plaintext http:// proxy (or an explicit ws:// URL), two issues prevented WebSocket connections:

  1. BaseApiClient._websocket_base_url() unconditionally replaced the URL scheme with 'wss' even when http_options.base_url used http:// or ws:// (and could apply to_mtls_url even for plaintext ws).
  2. AsyncLive.connect() unconditionally unpacked self._api_client._websocket_ssl_ctx into ws_connect(...), which passes ssl=... to websockets.connect and raises ValueError: ssl argument is incompatible with a ws:// URI.

Changes

  • Map 'http' -> 'ws' and 'https' -> 'wss' (preserving 'ws'/'wss') in BaseApiClient._websocket_base_url(), and only apply to_mtls_url when scheme == 'wss'.
  • Strip 'ssl' from ws_connect kwargs in AsyncLive.connect() when the target URI is not wss:// (matching _interactions_websocket.py).
  • Added unit tests in google/genai/tests/live/test_live.py.

Testing

  • pytest google/genai/tests/live/test_live.py -k "websocket or ssl_kwarg" -q: 6 passed
  • pytest google/genai/tests/live/test_live_music.py google/genai/tests/client/test_client_initialization.py -q: 166 passed
  • mypy google/genai/_api_client.py google/genai/live.py: Success: no issues found in 2 source files

Signed-off-by: Roxen-AS <akash.arjun12@gmail.com>

@Sun-Wukhan Sun-Wukhan left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, this fixes a real dead end for running Live behind a local or plain HTTP proxy: on main _websocket_base_url rewrites every scheme to wss, and connect always passes the ssl kwarg from _websocket_ssl_ctx, which websockets will not accept for a ws:// connection. Mapping http to ws and https to wss, gating the mTLS rewrite on wss, and dropping ssl for non wss URIs all line up with how the URI is actually used.

Three smaller points:

  1. The new mapping test covers http to ws and a ws URL passing through unchanged, but not https to wss, which is the production path and the one that must keep the mTLS rewrite. Could you add the https case to test_websocket_base_url_maps_http_and_preserves_ws so both directions of the table are pinned?

  2. In _websocket_base_url, unknown schemes now pass through unchanged, where main forced them to wss. A base_url such as ftp://host would now reach websockets as an ftp:// URI and fail there with a less clear error, and the ssl kwarg would also be dropped by the live.py check. Is passing unknown schemes through deliberate? If not, falling back to wss for anything that is not http or ws would preserve the old behaviour for typos.

  3. The bytes handling for url_parts.scheme: urlparse only returns a bytes scheme when the URL itself is bytes, and HttpOptions.base_url is typed as a string, so that branch looks unreachable. If a bytes base_url ever did arrive, the rest of the flow would break anyway, for example uri.lower().startswith('wss://') in live.py would raise a TypeError on a bytes URI. Suggest dropping the bytes branch to keep the mapping table easy to read, unless there is a caller passing bytes that I missed.

The connect level test asserting that ssl is absent from the captured kwargs for both the http proxy case and the direct ws URL case is exactly the right level to pin this.

Signed-off-by: Roxen-AS <akash.arjun12@gmail.com>
@Roxen-AS

Roxen-AS commented Dec 5, 2026

Copy link
Copy Markdown
Author

Thanks @Sun-Wukhan for the review! Updated _websocket_base_url and tests:

  1. Added https:// -> wss:// (with and without _websocket_use_mtls_endpoint=True) plus http:// + mTLS and fallback scheme coverage to test_websocket_base_url_maps_http_and_preserves_ws.
  2. Restored the 'wss' fallback in _websocket_base_url for any scheme other than 'http' or 'ws', so typos/unknown schemes behave as before.
  3. Replaced urlparse(http_options.base_url) with urlparse(http_options.base_url or '') and removed the bytes scheme decoding branch and the # type: ignore annotations (mypy passes cleanly).

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size:XL Code changes > 100 lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Live (Vertex AI): WebSocket URL always uses wss, even when base_url is http

3 participants