Skip to main content
Run shell commands inside sandboxes with full stdout/stderr capture, real-time streaming, and configurable timeouts.
Sandbox-specific operations use the sandbox proxy URL: https://<sandbox-id>.sandbox.tensorlake.ai

Basic Execution

from tensorlake.sandbox import SandboxClient

client = SandboxClient()

with client.create_and_connect() as sandbox:
    result = sandbox.run("python", ["-c", "print('Hello from sandbox!')"])
    print(result.stdout)     # Hello from sandbox!
    print(result.exit_code)  # 0

Shell Commands

Use shell features like pipes, redirects, and command chaining:
with client.create_and_connect() as sandbox:
    # Pipes
    result = sandbox.run("bash", ["-c", "ls -la /workspace | grep '.py' | wc -l"])
    print(result.stdout)

    # Redirects
    sandbox.run("bash", ["-c", "python script.py > output.txt 2> errors.txt"])

    # Command chaining
    sandbox.run("bash", ["-c", "cd /workspace && pip install -r requirements.txt && python main.py"])

Get Process Output

with client.create_and_connect() as sandbox:
    result = sandbox.run("python", ["-c", "print('hello')"])
    print(result.stdout)
    print(result.stderr)

Interactive Shell

with client.create_and_connect() as sandbox:
    # Create a PTY session for interactive terminal access
    session = sandbox.create_pty_session(
        command="/bin/bash",
        rows=24,
        cols=80
    )

    # Connect via WebSocket
    ws_url = sandbox.pty_ws_url(session["session_id"], session["token"])
    print(f"Terminal URL: {ws_url}")

Error Handling

with client.create_and_connect() as sandbox:
    result = sandbox.run("python", ["-c", "import nonexistent_module"])

    if result.exit_code != 0:
        print(f"Command failed with exit code {result.exit_code}")
        print(f"stderr: {result.stderr}")
    else:
        print(f"stdout: {result.stdout}")

Streaming Output

Stream stdout/stderr in real time for long-running commands using Server-Sent Events:
with client.create_and_connect() as sandbox:
    # Start a long-running process
    proc = sandbox.start_process("python", ["-c", """
import time
for i in range(5):
    print(f"Step {i+1}/5")
    time.sleep(1)
"""])

    # Stream output as it arrives
    for event in sandbox.follow_output(proc.pid):
        print(event.line, end="")

Learn More