Skip to content

Secure, Fast, and Extensible Sandbox runtime for AI agents.

fix(sdk-code-interpreter): keep the structured error when codes.run fails

Maohao039632f549b5
diff --git a/sdks/code-interpreter/python/src/code_interpreter/adapters/code_adapter.py b/sdks/code-interpreter/python/src/code_interpreter/adapters/code_adapter.py
index 64e0d2c86..07a8ac49c 100644
--- a/sdks/code-interpreter/python/src/code_interpreter/adapters/code_adapter.py
+++ b/sdks/code-interpreter/python/src/code_interpreter/adapters/code_adapter.py
@@ -33,12 +33,12 @@ from opensandbox.adapters.converter.execution_event_dispatcher import (
ExecutionEventDispatcher,
)
from opensandbox.adapters.converter.response_handler import (
- extract_request_id,
+ build_api_exception_from_httpx,
handle_api_error,
require_parsed,
)
from opensandbox.config import ConnectionConfig
-from opensandbox.exceptions import InvalidArgumentException, SandboxApiException
+from opensandbox.exceptions import InvalidArgumentException
from opensandbox.models.execd import Execution, ExecutionHandlers
from opensandbox.models.sandboxes import SandboxEndpoint
from opensandbox.transport import unwrap_retry_transport
@@ -324,15 +324,10 @@ class CodesAdapter(Codes):
async with client.stream("POST", url, json=api_request) as response:
if response.status_code != 200:
await response.aread()
- error_body = response.text
logger.error(
- f"Failed to run code. Status: {response.status_code}, Body: {error_body}"
- )
- raise SandboxApiException(
- message=f"Failed to run code. Status code: {response.status_code}, Body: {error_body}",
- status_code=response.status_code,
- request_id=extract_request_id(response.headers),
+ f"Failed to run code. Status: {response.status_code}, Body: {response.text}"
)
+ raise build_api_exception_from_httpx(response, "Run code")
dispatcher = ExecutionEventDispatcher(execution, handlers)
diff --git a/sdks/code-interpreter/python/src/code_interpreter/sync/adapters/code_adapter.py b/sdks/code-interpreter/python/src/code_interpreter/sync/adapters/code_adapter.py
index 217b7d5d5..424c6b216 100644
--- a/sdks/code-interpreter/python/src/code_interpreter/sync/adapters/code_adapter.py
+++ b/sdks/code-interpreter/python/src/code_interpreter/sync/adapters/code_adapter.py
@@ -27,12 +27,12 @@ from opensandbox.adapters.converter.exception_converter import (
ExceptionConverter,
)
from opensandbox.adapters.converter.response_handler import (
- extract_request_id,
+ build_api_exception_from_httpx,
handle_api_error,
require_parsed,
)
from opensandbox.config.connection_sync import ConnectionConfigSync
-from opensandbox.exceptions import InvalidArgumentException, SandboxApiException
+from opensandbox.exceptions import InvalidArgumentException
from opensandbox.models.execd import Execution
from opensandbox.models.execd_sync import ExecutionHandlersSync
from opensandbox.models.sandboxes import SandboxEndpoint
@@ -315,12 +315,7 @@ class CodesAdapterSync(CodesSync):
with self._sse_client.stream("POST", url, json=api_request) as response:
if response.status_code != 200:
response.read()
- error_body = response.text
- raise SandboxApiException(
- message=f"Failed to run code. Status code: {response.status_code}, Body: {error_body}",
- status_code=response.status_code,
- request_id=extract_request_id(response.headers),
- )
+ raise build_api_exception_from_httpx(response, "Run code")
for line in response.iter_lines():
if not line or not line.strip():
diff --git a/sdks/code-interpreter/python/tests/test_code_service_adapter_streaming.py b/sdks/code-interpreter/python/tests/test_code_service_adapter_streaming.py
index 59686806a..a9d5a5e36 100644
--- a/sdks/code-interpreter/python/tests/test_code_service_adapter_streaming.py
+++ b/sdks/code-interpreter/python/tests/test_code_service_adapter_streaming.py
@@ -16,11 +16,16 @@
from __future__ import annotations
import json
+from datetime import timedelta
import httpx
import pytest
from opensandbox.config import ConnectionConfig
-from opensandbox.exceptions import InvalidArgumentException, SandboxApiException
+from opensandbox.exceptions import (
+ InvalidArgumentException,
+ SandboxApiException,
+ SandboxRateLimitException,
+)
from opensandbox.models.sandboxes import SandboxEndpoint
from code_interpreter.adapters.code_adapter import CodesAdapter
@@ -167,3 +172,24 @@ async def test_run_code_non_200_raises_api_exception() -> None:
assert ei.value.request_id == "req-code-123"
# The server's error body is spliced into the message so logs carry the reason.
assert "bad" in str(ei.value)
+
+
+async def test_run_code_non_200_keeps_structured_error_and_rate_limit() -> None:
+ def handler(request: httpx.Request) -> httpx.Response:
+ return httpx.Response(
+ 429,
+ headers={"Retry-After": "3"},
+ json={"code": "RATE_LIMITED", "message": "slow down"},
+ request=request,
+ )
+
+ cfg = ConnectionConfig(protocol="http", transport=httpx.MockTransport(handler))
+ adapter = CodesAdapter(SandboxEndpoint(endpoint="localhost:44772"), cfg)
+
+ with pytest.raises(SandboxRateLimitException) as ei:
+ await adapter.run("print(1)")
+ assert ei.value.error.code == "RATE_LIMITED"
+ assert ei.value.error.message == "slow down"
+ assert ei.value.retry_after == timedelta(seconds=3)
+ assert ei.value.response_body is not None
diff --git a/sdks/code-interpreter/python/tests/test_code_service_adapter_sync_headers.py b/sdks/code-interpreter/python/tests/test_code_service_adapter_sync_headers.py
index cb17c791d..5b23915ec 100644
--- a/sdks/code-interpreter/python/tests/test_code_service_adapter_sync_headers.py
+++ b/sdks/code-interpreter/python/tests/test_code_service_adapter_sync_headers.py
@@ -15,7 +15,7 @@
#
import pytest
from opensandbox.config.connection_sync import ConnectionConfigSync
-from opensandbox.exceptions import SandboxApiException
+from opensandbox.exceptions import SandboxApiException, SandboxRateLimitException
from opensandbox.models.sandboxes import SandboxEndpoint
from code_interpreter.sync.adapters.code_adapter import CodesAdapterSync
@@ -69,3 +69,24 @@ def test_sync_adapter_endpoint_headers_override_connection_headers() -> None:
assert adapter._httpx_client.headers["X-Shared"] == "endpoint"
assert adapter._sse_client.headers["X-Shared"] == "endpoint"
+
+
+def test_sync_adapter_non_200_keeps_structured_error_and_rate_limit() -> None:
+ import httpx
+
+ def handler(request: httpx.Request) -> httpx.Response:
+ return httpx.Response(
+ 429,
+ headers={"Retry-After": "3"},
+ json={"code": "RATE_LIMITED", "message": "slow down"},
+ request=request,
+ )
+
+ cfg = ConnectionConfigSync(protocol="http", transport=httpx.MockTransport(handler))
+ adapter = CodesAdapterSync(SandboxEndpoint(endpoint="localhost:44772"), cfg)
+
+ with pytest.raises(SandboxRateLimitException) as ei:
+ adapter.run("print(1)")
+ assert ei.value.error.code == "RATE_LIMITED"
+ assert ei.value.error.message == "slow down"
+ assert ei.value.response_body is not None