Skip to main content

Sandflare Python SDK — Firecracker microVM sandboxes for AI agents

Project description

Sandflare Python SDK

The official Python SDK for creating and managing Sandflare sandboxes.

Install

pip install sandflare

For local development:

pip install -e sdk/python/

Quick start

from sandflare import Sandbox

sandbox = Sandbox.create("agent")
sandbox.run_code("value = 10")
result = sandbox.run_code("value += 5\nprint(value)")
print(result.stdout)

Environment variables

  • SANDFLARE_API_KEY: preferred API key
  • SANDFLARE_API_URL: optional API base URL override
  • PANDAAGENT_API_KEY: legacy API key alias
  • PANDAAGENT_BASE_URL: legacy base URL alias

Sandbox methods

exec / run_python / run_node

result = sb.exec("ls /home/agent")
result = sb.run_python("print('hello')")
result = sb.run_node("console.log('hello')")

stream / exec_stream

stream() reads all SSE output then yields StreamEvent objects (backward-compatible):

for event in sb.stream("python3 train.py"):
    if event.event in ("stdout", "stderr"):
        print(event.line, end="", flush=True)
    elif event.event == "done":
        print(f"\nexited {event.exit_code}")

exec_stream() is a true SSE generator that yields events as they arrive:

for event in sb.exec_stream("python3 train.py", timeout=120):
    if event.type in ("stdout", "stderr"):
        print(event.data, flush=True)
    elif event.type == "done":
        print(f"\nexited {event.exit_code}")

metrics()

m = sb.metrics()
print(f"CPU: {m.cpu_used_pct:.1f}%  RAM: {m.mem_used}/{m.mem_total} bytes")

kill_process(pid)

sb.kill_process(1234)

git_clone(repo, ...)

result = sb.git_clone(
    "https://github.com/org/repo",
    path="/home/agent/repo",
    branch="main",
    depth=1,
)
print(result.output)

File I/O

sb.write_file("/home/agent/data.csv", csv_text)
content = sb.read_file("/home/agent/output.txt")
sb.upload("local.png", "/home/agent/img.png")
raw_bytes = sb.download("/home/agent/img.png")
entries = sb.ls("/home/agent")

Template class

Build custom sandbox templates from Dockerfiles:

from sandflare import Template

# Submit a build (returns immediately)
job = Template.build(
    name="my-template",
    dockerfile="FROM ubuntu:22.04\nRUN apt-get update && apt-get install -y python3",
    description="Custom Python env",
)
print(job.id, job.status)  # e.g. "tmpl-abc123", "building"

# Poll until ready (up to 30 minutes)
job = Template.wait_for_build(job.id, timeout_seconds=600)
print(job.status)  # "ready"

# Use template when creating a sandbox
sb = Sandbox.create("agent", template_id=job.id)

# List all templates
jobs = Template.list()

# Check build status manually
job = Template.get_build_status("tmpl-abc123")

# Delete a template
Template.delete("tmpl-abc123")

New dataclasses

Class Fields
ProcessInfo pid, command, cpu_percent, memory_percent
SandboxMetrics sandbox_id, status, cpu_count, cpu_used_pct, mem_total, mem_used, disk_total, disk_used, ts
GitCloneResult path, repo, branch, output
TemplateBuildJob id, name, status, description, error, team_id, created_at, updated_at, logs

Build locally

cd sdk/python
python3 -m pip wheel . --no-deps -w dist/

Publish to PyPI

cd sdk/python
rm -rf dist/
python3 -m pip wheel . --no-deps -w dist/
python3 -m twine upload dist/*

Use a PyPI API token via TWINE_USERNAME=__token__ and TWINE_PASSWORD=<pypi-token>.

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

sandflare-2.1.7.tar.gz (20.5 kB view details)

Uploaded Source

Built Distribution

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

sandflare-2.1.7-py3-none-any.whl (19.4 kB view details)

Uploaded Python 3

File details

Details for the file sandflare-2.1.7.tar.gz.

File metadata

  • Download URL: sandflare-2.1.7.tar.gz
  • Upload date:
  • Size: 20.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for sandflare-2.1.7.tar.gz
Algorithm Hash digest
SHA256 c857d08015bb287175dc85d48a0d0831b43eedefa0257070b28beb02fce2f98c
MD5 f44f2cdcbd252073c6a6ed47dac4463c
BLAKE2b-256 2709c0c0f043a9893c561edb2f2d2bc549f92536f7fb80a38b2f761a99e18248

See more details on using hashes here.

File details

Details for the file sandflare-2.1.7-py3-none-any.whl.

File metadata

  • Download URL: sandflare-2.1.7-py3-none-any.whl
  • Upload date:
  • Size: 19.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for sandflare-2.1.7-py3-none-any.whl
Algorithm Hash digest
SHA256 34a9e67a54aaa9e4bbf1f39f82b6f478e45274ee9f6f292494ff510da259d2f3
MD5 01fe1f19adb85c5eb3f4ce0b44919bc2
BLAKE2b-256 70f00401ccc4f05164acda75aecd2c92dd82b7bbe3d00148dc13719c242bcb5b

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