Skip to main content

Sandbox0 Python SDK

Project description

Sandbox0 Python SDK

The official Python SDK for Sandbox0, providing typed models and ergonomic high-level APIs for managing secure code execution sandboxes.

Installation

pip install sandbox0

Requirements

  • Python 3.9 or later

Configuration

Environment Variable Required Default Description
SANDBOX0_TOKEN Yes - API authentication token
SANDBOX0_BASE_URL No https://api.sandbox0.ai API base URL

Quick Start

import os
from sandbox0 import Client, CmdOptions

client = Client(token=os.environ["SANDBOX0_TOKEN"])

# Using context manager for automatic cleanup
with client.sandboxes.open("default") as sandbox:
    # Execute Python code (REPL - stateful)
    result = sandbox.run("python", "print('Hello, Sandbox0!')")
    print(result.output_raw, end="")

CMD Streaming

stream = sandbox.cmd_stream(
    "sh -c 'echo hello && echo warn >&2'",
    CmdOptions(command=["sh", "-c", "echo hello && echo warn >&2"]),
)

for output in stream.iter_outputs():
    print(output.data, end="")

done = stream.wait()
print(f"exit={done.exit_code} state={done.state}")

OpenAI Agents SDK Sandbox

Install the optional adapter dependency:

pip install "sandbox0[openai-agents]"

Use Sandbox0SandboxClient anywhere the OpenAI Agents SDK expects a sandbox client:

import os

from agents import Runner
from agents.run_config import RunConfig, SandboxRunConfig
from agents.sandbox import SandboxAgent

from sandbox0_openai_agents import Sandbox0SandboxClient, Sandbox0SandboxClientOptions

client = Sandbox0SandboxClient(
    token=os.environ["SANDBOX0_TOKEN"],
    base_url=os.environ.get("SANDBOX0_BASE_URL"),
)

sandbox_agent = SandboxAgent(
    name="demo",
    instructions="Use the sandbox for filesystem and command execution tasks.",
)

result = Runner.run_sync(
    sandbox_agent,
    "Create hello.txt in the sandbox, then print it.",
    run_config=RunConfig(
        sandbox=SandboxRunConfig(
            client=client,
            options=Sandbox0SandboxClientOptions(template="default"),
        ),
    ),
)
print(result.final_output)

The adapter keeps the OpenAI SDK workspace at /workspace on a Sandbox0 SandboxVolume. delete() releases the sandbox runtime and deletes the workspace volume by default. Set delete_volume_on_delete=False when serialized session state must resume the same workspace volume after cleanup. Use Sandbox0SandboxClientOptions(volume_snapshot_id="...") for Sandbox0-native volume snapshots; generic OpenAI SDK snapshot specs are not used by this adapter.

LangChain Deep Agents Sandbox

Install the optional Deep Agents adapter dependency:

pip install "sandbox0[deepagents]"

The package registers a Deep Agents Code sandbox provider named sandbox0, so dcode can claim a Sandbox0 default template sandbox directly:

export SANDBOX0_TOKEN=...
dcode --sandbox sandbox0

For custom Deep Agents usage, wrap an existing Sandbox0 sandbox backend:

import os
from sandbox0 import Client
from sandbox0_deepagents import Sandbox0DeepAgentsSandbox

client = Client(token=os.environ["SANDBOX0_TOKEN"])
sandbox = client.sandboxes.claim("default")
backend = Sandbox0DeepAgentsSandbox(sandbox=sandbox)

result = backend.execute("python3 - <<'PY'\nprint('hello')\nPY")
print(result.output)

Documentation

Bootstrap Mounts At Claim Time

from sandbox0.apispec.models.claim_mount_request import ClaimMountRequest
from sandbox0.apispec.models.create_sandbox_volume_request import CreateSandboxVolumeRequest

volume = client.volumes.create(CreateSandboxVolumeRequest())

sandbox = client.sandboxes.claim(
    "default",
    mounts=[
        ClaimMountRequest(
            sandboxvolume_id=volume.id,
            mount_point="/workspace/data",
        )
    ],
)

for mount in sandbox.bootstrap_mounts:
    print(mount.sandboxvolume_id, mount.state)

Create A Template From A Sandbox

Capture the current root filesystem of an existing sandbox into a new template:

from sandbox0 import CreateTemplateFromSandboxOptions
from sandbox0.apispec.models.template_from_sandbox_create_request import (
    TemplateFromSandboxCreateRequest,
)
from sandbox0.apispec.models.template_from_sandbox_spec_overrides import (
    TemplateFromSandboxSpecOverrides,
)

template = client.create_template_from_sandbox(
    TemplateFromSandboxCreateRequest(
        template_id="python-ready",
        sandbox_id=sandbox.id,
        spec_overrides=TemplateFromSandboxSpecOverrides(
            display_name="Python Ready",
            tags=["python"],
        ),
    ),
    CreateTemplateFromSandboxOptions(
        idempotency_key="python-ready-v1",
        wait=True,
        timeout_sec=600,
    ),
)

print(template.template_id, template.status.creation.state)

Without wait=True, creation returns as soon as Sandbox0 accepts the request. The rootfs capture point is status.creation.captured_at, not request acceptance, so keep the source sandbox available and avoid rootfs writes while the stage is capturing. Call client.wait_template_ready("python-ready") to wait later. A client-side timeout or interruption stops local waiting but does not cancel image creation on the server.

Links

License

Apache-2.0

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

sandbox0-0.9.2.tar.gz (205.8 kB view details)

Uploaded Source

Built Distribution

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

sandbox0-0.9.2-py3-none-any.whl (580.9 kB view details)

Uploaded Python 3

File details

Details for the file sandbox0-0.9.2.tar.gz.

File metadata

  • Download URL: sandbox0-0.9.2.tar.gz
  • Upload date:
  • Size: 205.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.15

File hashes

Hashes for sandbox0-0.9.2.tar.gz
Algorithm Hash digest
SHA256 9f6128bbe6647717a52093f3779d6e056390c28fb892a8a836c64e871564d447
MD5 521f057fbe12d99a67601479db897943
BLAKE2b-256 43c84182f5b2d610279f98619d0892641efb234f72a3e305d5ba945f7e712328

See more details on using hashes here.

File details

Details for the file sandbox0-0.9.2-py3-none-any.whl.

File metadata

  • Download URL: sandbox0-0.9.2-py3-none-any.whl
  • Upload date:
  • Size: 580.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.15

File hashes

Hashes for sandbox0-0.9.2-py3-none-any.whl
Algorithm Hash digest
SHA256 d01b65bd4854bcffe09f0512d6d637b0bb73c39973f85940f8e777859dfcb13b
MD5 e05a81354b184a67b9b84bcfd0267a93
BLAKE2b-256 ab7a26d1b306e44971d855b64358af3ae312a2ebc74afef9d8ce078e4c9ac953

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