Skip to main content

Universal sandbox orchestrator for AI agents — Python SDK

Project description

roche-sandbox

Python SDK for Roche -- universal sandbox orchestrator for AI agents.

Requirements

  • Python >= 3.10
  • Roche CLI on PATH (or Roche daemon running)

Install

pip install roche-sandbox          # SDK only (requires Roche CLI installed separately)
pip install roche-sandbox[cli]     # SDK + auto-download prebuilt CLI binary

If you installed without [cli], you can download the CLI later:

roche-install-cli                  # downloads from GitHub Releases
cargo install roche-cli            # or build from source

Quick Start

from roche_sandbox import Roche

roche = Roche()
sandbox = roche.create(image="python:3.12-slim")
output = sandbox.exec(["python3", "-c", "print('Hello from Roche!')"])
print(output.stdout)  # Hello from Roche!
sandbox.destroy()

Context Manager (auto-cleanup)

with roche.create(image="python:3.12-slim") as sandbox:
    output = sandbox.exec(["echo", "hello"])

Async API

import asyncio
from roche_sandbox import AsyncRoche

async def main():
    roche = AsyncRoche()
    sandbox = await roche.create(image="python:3.12-slim")
    output = await sandbox.exec(["echo", "hello"])
    await sandbox.destroy()

asyncio.run(main())

Configuration

sandbox = roche.create(
    image="python:3.12-slim",
    memory="512m",
    cpus=1.0,
    timeout_secs=600,
    network=False,    # default: AI-safe
    writable=False,   # default: AI-safe
    env={"API_KEY": "secret"},
)

Transport

The SDK auto-detects whether the Roche gRPC daemon is running and connects to it. If the daemon is unavailable, it falls back to invoking the Roche CLI as a subprocess.

You can force CLI mode explicitly:

roche = Roche(mode="direct")

API Styles

The SDK provides two API styles:

  • Async-first: AsyncRoche and AsyncSandbox -- native async/await support.
  • Sync wrapper: Roche and Sandbox -- blocking equivalents for scripts and notebooks.

Public Exports

from roche_sandbox import (
    Roche, AsyncRoche,
    Sandbox, AsyncSandbox,
    roche_sandbox,                 # decorator
    SandboxConfig, ExecOutput, SandboxInfo,
    Mount, SandboxStatus,
    RocheError, SandboxNotFound, SandboxPaused,
    ProviderUnavailable, TimeoutError, UnsupportedOperation,
)

@roche_sandbox Decorator

The decorator automatically creates and injects a sandbox into your function — no manual lifecycle management needed. Works with both sync and async functions.

from roche_sandbox import roche_sandbox

@roche_sandbox(image="python:3.12-slim")
def run_code(code: str, sandbox) -> str:
    result = sandbox.exec(["python3", "-c", code])
    return result.stdout

output = run_code("print('hello')")  # sandbox is auto-managed

Async

@roche_sandbox(image="python:3.12-slim")
async def run_code(code: str, sandbox) -> str:
    result = await sandbox.exec(["python3", "-c", code])
    return result.stdout

Agent Framework Integration

The decorator strips the sandbox parameter from the function signature, so agent frameworks (OpenAI, LangChain, CrewAI, etc.) only see user-facing parameters:

from agents import function_tool

@function_tool
@roche_sandbox(image="python:3.12-slim")
def run_code(code: str, sandbox) -> str:
    """Execute Python code in a sandbox."""
    return sandbox.exec(["python3", "-c", code]).stdout

Parameters

Parameter Type Default Description
image str "python:3.12-slim" Container image
provider str "docker" Sandbox provider
network bool False Enable network access
writable bool False Enable writable filesystem
timeout_secs int 300 Sandbox timeout
memory str | None None Memory limit (e.g. "512m")
cpus float | None None CPU limit
sandbox_param str "sandbox" Name of the injected parameter

Agent Framework Examples

See examples/python/ for integration examples with:

  • OpenAI Agents SDK@function_tool integration
  • LangChain / LangGraph — custom BaseTool + stateful retry workflow
  • CrewAI@tool decorator + multi-agent crew
  • Anthropic APItool_use + multi-turn agentic loop
  • AutoGen — custom CodeExecutor + group chat
  • Camel-AIBaseToolkit + role-playing session

All examples run without API keys (simulated mode) and optionally switch to real LLM calls.

License

Apache-2.0 -- see LICENSE.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

roche_sandbox-0.4.0.tar.gz (31.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

roche_sandbox-0.4.0-py3-none-any.whl (34.5 kB view details)

Uploaded Python 3

File details

Details for the file roche_sandbox-0.4.0.tar.gz.

File metadata

  • Download URL: roche_sandbox-0.4.0.tar.gz
  • Upload date:
  • Size: 31.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.13

File hashes

Hashes for roche_sandbox-0.4.0.tar.gz
Algorithm Hash digest
SHA256 c2b05616db765affb23f6b9f5bd9aaf64f404269cb4bca101cbabc19b6867d07
MD5 0981872fd90b108a4fdb8971ee25e341
BLAKE2b-256 9a3a04ec6dc565b4d4da1cff1eb573aa5f8c68c9f30e7733eceb0d104f8e35b7

See more details on using hashes here.

File details

Details for the file roche_sandbox-0.4.0-py3-none-any.whl.

File metadata

  • Download URL: roche_sandbox-0.4.0-py3-none-any.whl
  • Upload date:
  • Size: 34.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.13

File hashes

Hashes for roche_sandbox-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 987258adc8eca02c68413487e9cf442f4de1550e8cd645bb3bfba28e2d179e7f
MD5 949743765af549b7ed75d21e6f02c351
BLAKE2b-256 740a41248348e8f4e03432603152e8b50c75d2367a98a72942fa89d1b3fac9be

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page