Secure, Fast, and Extensible Sandbox runtime for AI agents.
fix(sdk-python): ignore a trailing slash or /v1 in the connection domain
Max Freedom Pollard1a6d8f044b04
diff --git a/docs/sdks/python.md b/docs/sdks/python.mdindex 0d3fe831c..942f6138e 100644--- a/docs/sdks/python.md+++ b/docs/sdks/python.md@@ -519,7 +519,7 @@ The `ConnectionConfig` class manages API server connection settings.| Parameter | Description | Default | Environment Variable || ----------------- | ------------------------------------------ | ---------------------------- | ---------------------- || `api_key` | API Key for authentication | Optional; needed when server auth is enabled | `OPEN_SANDBOX_API_KEY` |-| `domain` | The endpoint domain of the sandbox service | `localhost:8080` | `OPEN_SANDBOX_DOMAIN` |+| `domain` | The endpoint domain of the sandbox service (`host[:port]` or a full URL); a trailing `/` or `/v1` is ignored | `localhost:8080` | `OPEN_SANDBOX_DOMAIN` || `protocol` | HTTP protocol (http/https) | `http` | - || `request_timeout` | Timeout for API requests | 30 seconds | - || `debug` | Enable debug logging for HTTP requests | `False` | - |diff --git a/sdks/sandbox/python/src/opensandbox/config/connection.py b/sdks/sandbox/python/src/opensandbox/config/connection.pyindex 317105a05..7600c6367 100644--- a/sdks/sandbox/python/src/opensandbox/config/connection.py+++ b/sdks/sandbox/python/src/opensandbox/config/connection.py@@ -253,7 +253,12 @@ class ConnectionConfig(BaseModel):def get_base_url(self) -> str:"""Get the full base URL for API requests."""- domain = self.get_domain()+ # Ignore a trailing "/" or "/v1" like the other SDKs, so the version+ # prefix is never doubled ("//v1", "/v1/v1").+ domain = self.get_domain().rstrip("/")+ version_suffix = f"/{self._API_VERSION}"+ if domain.endswith(version_suffix):+ domain = domain[: -len(version_suffix)]# Allow domain to override protocol if it explicitly starts with a schemeif domain.startswith("http://") or domain.startswith("https://"):return f"{domain}/{self._API_VERSION}"diff --git a/sdks/sandbox/python/src/opensandbox/config/connection_sync.py b/sdks/sandbox/python/src/opensandbox/config/connection_sync.pyindex 250b79791..fcd1d785a 100644--- a/sdks/sandbox/python/src/opensandbox/config/connection_sync.py+++ b/sdks/sandbox/python/src/opensandbox/config/connection_sync.py@@ -223,7 +223,11 @@ class ConnectionConfigSync(BaseModel):return self.domain or os.getenv(self._ENV_DOMAIN, self._DEFAULT_DOMAIN)def get_base_url(self) -> str:- domain = self.get_domain()+ # Same normalization as ConnectionConfig.get_base_url().+ domain = self.get_domain().rstrip("/")+ version_suffix = f"/{self._API_VERSION}"+ if domain.endswith(version_suffix):+ domain = domain[: -len(version_suffix)]if domain.startswith("http://") or domain.startswith("https://"):return f"{domain}/{self._API_VERSION}"return f"{self.protocol}://{domain}/{self._API_VERSION}"diff --git a/sdks/sandbox/python/tests/test_connection_config.py b/sdks/sandbox/python/tests/test_connection_config.pyindex 80bb7bad2..b00c69801 100644--- a/sdks/sandbox/python/tests/test_connection_config.py+++ b/sdks/sandbox/python/tests/test_connection_config.py@@ -44,6 +44,36 @@ def test_get_base_url_domain_can_include_scheme() -> None:assert cfg.get_base_url() == "https://example.com:9999/v1"[email protected]("config_cls", [ConnectionConfig, ConnectionConfigSync])+ ("domain", "expected"),+ [+ ("example.com:1234/", "https://example.com:1234/v1"),+ ("example.com:1234/v1", "https://example.com:1234/v1"),+ ("example.com:1234/v1/", "https://example.com:1234/v1"),+ ("https://example.com:9999/", "https://example.com:9999/v1"),+ ("https://example.com:9999/v1", "https://example.com:9999/v1"),+ ("https://example.com/proxy/", "https://example.com/proxy/v1"),+ ("https://example.com/proxy/v1/", "https://example.com/proxy/v1"),+ ],+)+def test_get_base_url_normalizes_trailing_slash_and_version_suffix(+ config_cls: type[ConnectionConfig] | type[ConnectionConfigSync],+ domain: str,+ expected: str,+) -> None:+ cfg = config_cls(domain=domain, protocol="https")+ assert cfg.get_base_url() == expected+++def test_get_base_url_normalizes_domain_from_env(+ monkeypatch: pytest.MonkeyPatch,+) -> None:+ monkeypatch.setenv("OPEN_SANDBOX_DOMAIN", "http://localhost:8080/")+ assert ConnectionConfig().get_base_url() == "http://localhost:8080/v1"+ assert ConnectionConfigSync().get_base_url() == "http://localhost:8080/v1"++@pytest.mark.asyncioasync def test_close_transport_if_owned_default_transport() -> None:cfg = ConnectionConfig().with_transport_if_missing()