E2B icon indicating copy to clipboard operation
E2B copied to clipboard

Unable to run commands in sandbox through Python SDK

Open jayv-tech opened this issue 10 months ago • 3 comments

Describe the bug
When executing a command using the E2B sandbox code interpreter (even a simple command like ls -l), the SDK returns a streaming response. However, any attempt to print or otherwise access the result (for example, with print(result)) triggers a RuntimeError with the message:

"Attempted to access 'response.content' on a streaming response. Call 'response.read()' first."

Even after trying to explicitly read the stream (using await result.aread() in an async context or result.read() in a synchronous context), the error persists because the package appears to attempt to access the response content before the stream has been fully buffered (atleast, that's what I guess is going on.)

To Reproduce
Steps to reproduce the behavior:

  1. Initialize an E2B sandbox.
  2. Run a simple command. For example: result = sandbox.commands.run('ls -l') print(result)
  3. A RuntimeError will raise with the message about accessing response.content on a streaming response.

Expected behavior
The command should be executed properly, and after it completes, accessing the result (or printing it) should not trigger an error.

Browser console output
N/A (This is a Python package issue, not a browser issue.)

Terminal commands & output
Terminal command executed: result = sandbox.commands.run('ls -l', on_stdout=lambda data: print(data)) print(result) Terminal output:

RuntimeError: Attempted to access 'response.content' on a streaming response. Call 'response.read()' first.

Screenshots

Image

Desktop (please complete the following information):

  • OS: WSL (Ubuntu 24.04), also tested with native Ubuntu 24.10 system.
  • Python Version: 3.12
  • E2B SDK Version: 1.0.5

Smartphone (please complete the following information):
N/A

Additional context
I also tried the example command from the documentation:

result = sandbox.commands.run('echo hello; sleep 1; echo world', on_stdout=lambda data: print(data), on_stderr=lambda data: print(data))
print(result)

which produces the same error. Even when attempting to call await result.aread() before accessing the result, the error is raised internally within the package. This behavior prevents any downstream access to the full command output, making it impossible to execute any command in the sandbox.

jayv-tech avatar Feb 15 '25 07:02 jayv-tech

Hey! Thanks for reporting. Unfortunately, I was unable to reproduce, can you provide a full example?

mishushakov avatar Feb 16 '25 13:02 mishushakov

I just tried to use the GPT 4o model to generate an SVG file and list and watch the directory to download that later. Even running the simple commands after initiating the sandbox and manually connecting to it, results in that specified error.

Now, I have tried and used the run_code method to bypass this: result = sandbox.run_code(f'ls -l {remote_dir}', 'bash')

Only this works. Also, any attempt to read and save an image file will result in an error unless we specify 'bytes' param along with it.

Something like this:

remote_file = "/home/user/test.svg"

local_filename = "test.svg"

content = sandbox.files.read(remote_file, 'bytes')

with open(local_filename, "wb") as f:
    f.write(content)
print(f"Downloaded new SVG.")

jayv-tech avatar Feb 17 '25 05:02 jayv-tech

The issue is in

with open(local_filename, "wb") as f:

By using wb mode, the file expects bytes, the default is to return string

You can either do

with open(local_filename, "w") as f:

but it may not work well for data types or as you did, download bytes

jakubno avatar May 14 '25 12:05 jakubno