Skip to content

armgit / E2B

Public Imported from GitHub
0

Open-source, secure environment with real-world tools for enterprise-grade agents.

fix(python-sdk): align sync and async implementations (#1403)

Mish Ushakovd86368a11ec5
diff --git a/.changeset/python-sync-async-alignment.md b/.changeset/python-sync-async-alignment.md
new file mode 100644
index 000000000..d41a35cdb
--- /dev/null
+++ b/.changeset/python-sync-async-alignment.md
@@ -0,0 +1,7 @@
+---
+"@e2b/python-sdk": minor
+---
+
+Align the sync and async Python SDK implementations: consistent parameter ordering (`_create`, `Commands._start`), matching docstrings, keyword arguments in `Filesystem.write`, and a consistent bare `Exception` for the internal "Body of the request is None" guard (matching the volume client).
+
+`Sandbox.pause()` / `AsyncSandbox.pause()` (and `beta_pause`) now return a `bool` — `True` if the sandbox got paused, `False` if it was already paused — matching the JS SDK. Previously the instance method returned `None` and the class-method form returned the sandbox ID.
diff --git a/packages/python-sdk/e2b/sandbox_async/commands/command.py b/packages/python-sdk/e2b/sandbox_async/commands/command.py
index 4476558b2..227e778f1 100644
--- a/packages/python-sdk/e2b/sandbox_async/commands/command.py
+++ b/packages/python-sdk/e2b/sandbox_async/commands/command.py
@@ -256,9 +256,9 @@ class Commands:
envs,
user,
cwd,
+ stdin,
timeout,
request_timeout,
- stdin,
on_stdout=on_stdout,
on_stderr=on_stderr,
)
@@ -271,9 +271,9 @@ class Commands:
envs: Optional[Dict[str, str]],
user: Optional[Username],
cwd: Optional[str],
+ stdin: bool,
timeout: Optional[float],
request_timeout: Optional[float],
- stdin: bool,
on_stdout: Optional[OutputHandler[Stdout]],
on_stderr: Optional[OutputHandler[Stderr]],
) -> AsyncCommandHandle:
@@ -342,13 +342,13 @@ class Commands:
process_pb2.ConnectRequest(
process=process_pb2.ProcessSelector(pid=pid),
),
+ headers={
+ KEEPALIVE_PING_HEADER: str(KEEPALIVE_PING_INTERVAL_SEC),
+ },
timeout=timeout,
request_timeout=self._connection_config.get_request_timeout(
request_timeout
),
- headers={
- KEEPALIVE_PING_HEADER: str(KEEPALIVE_PING_INTERVAL_SEC),
- },
)
try:
diff --git a/packages/python-sdk/e2b/sandbox_async/commands/pty.py b/packages/python-sdk/e2b/sandbox_async/commands/pty.py
index 3585b1324..5b7b69aef 100644
--- a/packages/python-sdk/e2b/sandbox_async/commands/pty.py
+++ b/packages/python-sdk/e2b/sandbox_async/commands/pty.py
@@ -220,7 +220,7 @@ class Pty:
pid: int,
size: PtySize,
request_timeout: Optional[float] = None,
- ):
+ ) -> None:
"""
Resize PTY.
Call this when the terminal window is resized and the number of columns and rows has changed.
diff --git a/packages/python-sdk/e2b/sandbox_async/filesystem/filesystem.py b/packages/python-sdk/e2b/sandbox_async/filesystem/filesystem.py
index 309f4f616..933d96508 100644
--- a/packages/python-sdk/e2b/sandbox_async/filesystem/filesystem.py
+++ b/packages/python-sdk/e2b/sandbox_async/filesystem/filesystem.py
@@ -216,10 +216,10 @@ class Filesystem:
"""
result = await self.write_files(
[WriteEntry(path=path, data=data)],
- user,
- request_timeout,
- gzip,
- use_octet_stream,
+ user=user,
+ request_timeout=request_timeout,
+ gzip=gzip,
+ use_octet_stream=use_octet_stream,
)
if len(result) != 1:
diff --git a/packages/python-sdk/e2b/sandbox_async/main.py b/packages/python-sdk/e2b/sandbox_async/main.py
index 1c8ce0786..d0bafd004 100644
--- a/packages/python-sdk/e2b/sandbox_async/main.py
+++ b/packages/python-sdk/e2b/sandbox_async/main.py
@@ -98,15 +98,15 @@ class AsyncSandbox(SandboxApi):
**opts: Unpack[SandboxOpts],
):
"""
+ :deprecated: This constructor is deprecated
+
Use `AsyncSandbox.create()` to create a new sandbox instead.
"""
super().__init__(**opts)
self._transport = get_transport(self.connection_config)
self._envd_api = httpx.AsyncClient(
- base_url=self.connection_config.get_sandbox_url(
- self.sandbox_id, self.sandbox_domain
- ),
+ base_url=self.envd_api_url,
transport=self._transport,
headers=self.connection_config.sandbox_headers,
)
@@ -604,11 +604,11 @@ class AsyncSandbox(SandboxApi):
async def pause(
self,
**opts: Unpack[ApiParams],
- ) -> None:
+ ) -> bool:
"""
Pause the sandbox.
- :return: Sandbox ID that can be used to resume the sandbox
+ :return: `True` if the sandbox got paused, `False` if the sandbox was already paused
"""
...
@@ -617,13 +617,13 @@ class AsyncSandbox(SandboxApi):
async def pause(
sandbox_id: str,
**opts: Unpack[ApiParams],
- ) -> None:
+ ) -> bool:
"""
Pause the sandbox specified by sandbox ID.
:param sandbox_id: Sandbox ID
- :return: Sandbox ID that can be used to resume the sandbox
+ :return: `True` if the sandbox got paused, `False` if the sandbox was already paused
"""
...
@@ -631,14 +631,14 @@ class AsyncSandbox(SandboxApi):
async def pause(
self,
**opts: Unpack[ApiParams],
- ) -> None:
+ ) -> bool:
"""
Pause the sandbox.
- :return: Sandbox ID that can be used to resume the sandbox
+ :return: `True` if the sandbox got paused, `False` if the sandbox was already paused
"""
- await SandboxApi._cls_pause(
+ return await SandboxApi._cls_pause(
sandbox_id=self.sandbox_id,
**self.connection_config.get_api_params(**opts),
)
@@ -647,24 +647,24 @@ class AsyncSandbox(SandboxApi):
async def beta_pause(
self,
**opts: Unpack[ApiParams],
- ) -> None: ...
+ ) -> bool: ...
@overload
@staticmethod
async def beta_pause(
sandbox_id: str,
**opts: Unpack[ApiParams],
- ) -> None: ...
+ ) -> bool: ...
@class_method_variant("_cls_pause")
async def beta_pause(
self,
**opts: Unpack[ApiParams],
- ) -> None:
+ ) -> bool:
"""
:deprecated: Use `pause()` instead.
"""
- await self.pause(**opts)
+ return await self.pause(**opts)
@overload
async def create_snapshot(
@@ -871,10 +871,10 @@ class AsyncSandbox(SandboxApi):
cls,
template: Optional[str],
timeout: Optional[int],
- allow_internet_access: bool,
metadata: Optional[Dict[str, str]],
envs: Optional[Dict[str, str]],
secure: bool,
+ allow_internet_access: bool,
mcp: Optional[McpServer] = None,
network: Optional[SandboxNetworkOpts] = None,
lifecycle: Optional[SandboxLifecycle] = None,
diff --git a/packages/python-sdk/e2b/sandbox_async/sandbox_api.py b/packages/python-sdk/e2b/sandbox_async/sandbox_api.py
index 7bee5cb07..180630c07 100644
--- a/packages/python-sdk/e2b/sandbox_async/sandbox_api.py
+++ b/packages/python-sdk/e2b/sandbox_async/sandbox_api.py
@@ -346,7 +346,7 @@ class SandboxApi(SandboxBase):
raise handle_api_exception(res)
if res.parsed is None:
- raise SandboxException("Body of the request is None")
+ raise Exception("Body of the request is None")
if isinstance(res.parsed, Error):
raise SandboxException(f"{res.parsed.message}: Request failed")
@@ -383,7 +383,7 @@ class SandboxApi(SandboxBase):
cls,
sandbox_id: str,
**opts: Unpack[ApiParams],
- ) -> str:
+ ) -> bool:
config = ConnectionConfig(**opts)
api_client = get_api_client(config)
@@ -396,7 +396,8 @@ class SandboxApi(SandboxBase):
raise SandboxNotFoundException(f"Sandbox {sandbox_id} not found")
if res.status_code == 409:
- return sandbox_id
+ # Sandbox is already paused
+ return False
if res.status_code >= 300:
raise handle_api_exception(res)
@@ -405,7 +406,7 @@ class SandboxApi(SandboxBase):
if isinstance(res.parsed, Error):
raise SandboxException(f"{res.parsed.message}: Request failed")
- return sandbox_id
+ return True
@classmethod
async def _cls_connect(
@@ -437,6 +438,6 @@ class SandboxApi(SandboxBase):
raise SandboxException(f"{res.parsed.message}: Request failed")
if res.parsed is None:
- raise SandboxException("Body of the request is None")
+ raise Exception("Body of the request is None")
return res.parsed
diff --git a/packages/python-sdk/e2b/sandbox_sync/commands/command.py b/packages/python-sdk/e2b/sandbox_sync/commands/command.py
index e23c1809e..feafd6c5d 100644
--- a/packages/python-sdk/e2b/sandbox_sync/commands/command.py
+++ b/packages/python-sdk/e2b/sandbox_sync/commands/command.py
@@ -79,7 +79,7 @@ class Commands:
request_timeout: Optional[float] = None,
) -> bool:
"""
- Kills a running command specified by its process ID.
+ Kill a running command specified by its process ID.
It uses `SIGKILL` signal to kill the command.
:param pid: Process ID of the command. You can get the list of processes using `sandbox.commands.list()`
diff --git a/packages/python-sdk/e2b/sandbox_sync/commands/pty.py b/packages/python-sdk/e2b/sandbox_sync/commands/pty.py
index fd936ef40..0b4a5fe92 100644
--- a/packages/python-sdk/e2b/sandbox_sync/commands/pty.py
+++ b/packages/python-sdk/e2b/sandbox_sync/commands/pty.py
@@ -217,7 +217,7 @@ class Pty:
:param pid: Process ID of the PTY
:param size: New size of the PTY
- :param request_timeout: Timeout for the request in **seconds**s
+ :param request_timeout: Timeout for the request in **seconds**
"""
self._rpc.update(
process_pb2.UpdateRequest(
diff --git a/packages/python-sdk/e2b/sandbox_sync/filesystem/filesystem.py b/packages/python-sdk/e2b/sandbox_sync/filesystem/filesystem.py
index b145200e4..ca345a2ab 100644
--- a/packages/python-sdk/e2b/sandbox_sync/filesystem/filesystem.py
+++ b/packages/python-sdk/e2b/sandbox_sync/filesystem/filesystem.py
@@ -234,6 +234,8 @@ class Filesystem:
use_octet_stream: bool = False,
) -> List[WriteInfo]:
"""
+ Writes multiple files.
+
Writes a list of files to the filesystem.
When writing to a file that doesn't exist, the file will get created.
When writing to a file that already exists, the file will get overwritten.
diff --git a/packages/python-sdk/e2b/sandbox_sync/main.py b/packages/python-sdk/e2b/sandbox_sync/main.py
index e21abe112..c8d3177c2 100644
--- a/packages/python-sdk/e2b/sandbox_sync/main.py
+++ b/packages/python-sdk/e2b/sandbox_sync/main.py
@@ -578,7 +578,7 @@ class Sandbox(SandboxApi):
**opts: Unpack[ApiParams],
) -> List[SandboxMetrics]:
"""
- Get the metrics of the sandbox specified by sandbox ID.
+ Get the metrics of the current sandbox.
:param start: Start time for the metrics, defaults to the start of the sandbox
:param end: End time for the metrics, defaults to the current time
@@ -606,9 +606,11 @@ class Sandbox(SandboxApi):
def pause(
self,
**opts: Unpack[ApiParams],
- ) -> None:
+ ) -> bool:
"""
Pause the sandbox.
+
+ :return: `True` if the sandbox got paused, `False` if the sandbox was already paused
"""
...
@@ -617,11 +619,13 @@ class Sandbox(SandboxApi):
def pause(
sandbox_id: str,
**opts: Unpack[ApiParams],
- ) -> None:
+ ) -> bool:
"""
Pause the sandbox specified by sandbox ID.
:param sandbox_id: Sandbox ID
+
+ :return: `True` if the sandbox got paused, `False` if the sandbox was already paused
"""
...
@@ -629,14 +633,14 @@ class Sandbox(SandboxApi):
def pause(
self,
**opts: Unpack[ApiParams],
- ) -> None:
+ ) -> bool:
"""
Pause the sandbox.
- :return: Sandbox ID that can be used to resume the sandbox
+ :return: `True` if the sandbox got paused, `False` if the sandbox was already paused
"""
- SandboxApi._cls_pause(
+ return SandboxApi._cls_pause(
sandbox_id=self.sandbox_id,
**self.connection_config.get_api_params(**opts),
)
@@ -645,24 +649,24 @@ class Sandbox(SandboxApi):
def beta_pause(
self,
**opts: Unpack[ApiParams],
- ) -> None: ...
+ ) -> bool: ...
@overload
@staticmethod
def beta_pause(
sandbox_id: str,
**opts: Unpack[ApiParams],
- ) -> None: ...
+ ) -> bool: ...
@class_method_variant("_cls_pause")
def beta_pause(
self,
**opts: Unpack[ApiParams],
- ) -> None:
+ ) -> bool:
"""
:deprecated: Use `pause()` instead.
"""
- self.pause(**opts)
+ return self.pause(**opts)
@overload
def create_snapshot(
@@ -856,10 +860,10 @@ class Sandbox(SandboxApi):
return cls(
sandbox_id=sandbox.sandbox_id,
sandbox_domain=sandbox.domain,
- connection_config=connection_config,
envd_version=Version(sandbox.envd_version),
envd_access_token=envd_access_token,
traffic_access_token=sandbox.traffic_access_token,
+ connection_config=connection_config,
)
@classmethod
diff --git a/packages/python-sdk/e2b/sandbox_sync/sandbox_api.py b/packages/python-sdk/e2b/sandbox_sync/sandbox_api.py
index b45e39cd6..ed9cd2d4c 100644
--- a/packages/python-sdk/e2b/sandbox_sync/sandbox_api.py
+++ b/packages/python-sdk/e2b/sandbox_sync/sandbox_api.py
@@ -103,7 +103,7 @@ class SandboxApi(SandboxBase):
raise handle_api_exception(res)
if res.parsed is None:
- raise SandboxException("Body of the request is None")
+ raise Exception("Body of the request is None")
if isinstance(res.parsed, Error):
raise SandboxException(f"{res.parsed.message}: Request failed")
@@ -340,7 +340,7 @@ class SandboxApi(SandboxBase):
raise SandboxException(f"{res.parsed.message}: Request failed")
if res.parsed is None:
- raise SandboxException("Body of the request is None")
+ raise Exception("Body of the request is None")
return res.parsed
@@ -367,7 +367,7 @@ class SandboxApi(SandboxBase):
raise handle_api_exception(res)
if res.parsed is None:
- raise SandboxException("Body of the request is None")
+ raise Exception("Body of the request is None")
if isinstance(res.parsed, Error):
raise SandboxException(f"{res.parsed.message}: Request failed")
@@ -404,7 +404,7 @@ class SandboxApi(SandboxBase):
cls,
sandbox_id: str,
**opts: Unpack[ApiParams],
- ) -> str:
+ ) -> bool:
config = ConnectionConfig(**opts)
api_client = get_api_client(config)
@@ -417,9 +417,14 @@ class SandboxApi(SandboxBase):
raise SandboxNotFoundException(f"Sandbox {sandbox_id} not found")
if res.status_code == 409:
- return sandbox_id
+ # Sandbox is already paused
+ return False
if res.status_code >= 300:
raise handle_api_exception(res)
- return sandbox_id
+ # Check if res.parse is Error
+ if isinstance(res.parsed, Error):
+ raise SandboxException(f"{res.parsed.message}: Request failed")
+
+ return True