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.5.0.tar.gz (33.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.5.0-py3-none-any.whl (37.2 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: roche_sandbox-0.5.0.tar.gz
  • Upload date:
  • Size: 33.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.5.0.tar.gz
Algorithm Hash digest
SHA256 e986ddf5e34e855446d6e54f76cda409b0b68e71fca06d24f5d7891f9bd8ce06
MD5 170e982429b8c12f67957fa17f535d15
BLAKE2b-256 464f7c0e2bcb6ae61bf1841cc57c8f6bc93c37fd823b97928e123f528c0d619a

See more details on using hashes here.

File details

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

File metadata

  • Download URL: roche_sandbox-0.5.0-py3-none-any.whl
  • Upload date:
  • Size: 37.2 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.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c140dc3b0ffc2647572563e03320b7c465b2fc11eee01de333b05f2cc3c89321
MD5 09cdbe197a3e869c1b5a5a63fb6f738e
BLAKE2b-256 32d92158df99d942107310d755ad24ae6edc3909a63c35bc0aabe54f43b751c0

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