Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions google/genai/_api_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,13 @@ def populate_server_timeout_header(
headers['X-Server-Timeout'] = str(math.ceil(timeout_in_seconds))


def _strip_trailing_slash(url):
"""Removes a single trailing slash from a URL string, if present."""
if isinstance(url, str) and url.endswith('/'):
return url[:-1]
return url


def join_url_path(base_url: str, path: str) -> str:
parsed_base = urlparse(base_url)
base_path = (
Expand Down Expand Up @@ -1906,12 +1913,14 @@ def _websocket_base_url(
if self.custom_base_url and not has_sufficient_auth:
# API gateway proxy can use the auth in custom headers, not url.
# Enable custom url if auth is not sufficient.
return http_options.base_url or self.custom_base_url
return _strip_trailing_slash(
http_options.base_url or self.custom_base_url
)
url_parts = urlparse(http_options.base_url)
url = url_parts._replace(scheme='wss').geturl() # type: ignore[arg-type]
if self._websocket_use_mtls_endpoint:
url = to_mtls_url(url) # type: ignore[arg-type]
return url # type: ignore[return-value]
return _strip_trailing_slash(url) # type: ignore[return-value]

def _access_token(self) -> str:
"""Retrieves the access token for the credentials."""
Expand Down
10 changes: 5 additions & 5 deletions google/genai/tests/client/test_client_initialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -2294,7 +2294,7 @@ async def test_async_httpx_download_uses_mtls_endpoint_with_client_cert(
def test_websocket_uses_mtls_endpoint_with_client_cert(mock_client_cert):
client = _mtls_test_client()
assert client._api_client._websocket_base_url() == (
"wss://us-central1-aiplatform.mtls.googleapis.com/"
"wss://us-central1-aiplatform.mtls.googleapis.com"
)


Expand All @@ -2307,7 +2307,7 @@ def test_mtls_endpoint_not_used_without_client_cert(monkeypatch):
)
client = _mtls_test_client()
assert client._api_client._websocket_base_url() == (
"wss://us-central1-aiplatform.googleapis.com/"
"wss://us-central1-aiplatform.googleapis.com"
)
assert (
client._api_client._httpx_url(
Expand All @@ -2326,7 +2326,7 @@ def test_mtls_endpoint_not_used_with_custom_transport(mock_client_cert):
assert client._api_client._httpx_url(url, is_async=True) == url
assert not client._api_client._aiohttp_use_mtls_endpoint
assert client._api_client._websocket_base_url() == (
"wss://us-central1-aiplatform.googleapis.com/"
"wss://us-central1-aiplatform.googleapis.com"
)


Expand All @@ -2353,7 +2353,7 @@ def test_mtls_endpoint_env_override(
def test_mtls_endpoint_not_used_for_gemini_api(mock_client_cert):
client = Client(api_key="test-api-key")
assert client._api_client._websocket_base_url() == (
"wss://generativelanguage.googleapis.com/"
"wss://generativelanguage.googleapis.com"
)


Expand All @@ -2377,7 +2377,7 @@ def test_mtls_endpoint_not_used_when_client_cert_fails_to_load(
assert client._api_client._httpx_url(url, is_async=False) == url
assert client._api_client._httpx_url(url, is_async=True) == url
assert client._api_client._websocket_base_url() == (
"wss://us-central1-aiplatform.googleapis.com/"
"wss://us-central1-aiplatform.googleapis.com"
)


Expand Down
30 changes: 30 additions & 0 deletions google/genai/tests/live/test_live.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,16 @@ def test_websocket_base_url():
assert api_client._websocket_base_url() == 'wss://test.com'


def test_websocket_base_url_with_trailing_slash():
# The default base urls end in '/'; the websocket base url must not, or
# joining it with '/ws/...' produces a double slash in the path.
api_client = gl_client.BaseApiClient(
api_key='google_api_key',
http_options={'base_url': 'https://test.com/'},
)
assert api_client._websocket_base_url() == 'wss://test.com'


def test_websocket_base_url_no_auth_with_custom_base_url():
base_url = 'https://test-api-gateway-proxy.com'
api_client = gl_client.BaseApiClient(
Expand Down Expand Up @@ -2656,6 +2666,26 @@ async def test_connect_keeps_client_base_url():
)


@pytest.mark.asyncio
async def test_connect_with_trailing_slash_base_url():
client = gl_client.BaseApiClient(
vertexai=False,
api_key='TEST_API_KEY',
http_options={
'base_url': 'https://client-base-url.com/',
'api_version': 'v1beta',
},
)

capture = await _capture_connect(client, None)

# A base url ending in '/' must not produce a double slash in the path.
assert capture['uri'] == (
'wss://client-base-url.com/ws/google.ai.generativelanguage.v1beta'
'.GenerativeService.BidiGenerateContent'
)


@pytest.mark.asyncio
async def test_connect_with_session_http_options_and_credentials():
client = gl_client.BaseApiClient(
Expand Down
Loading