Skip to content
A logo with the word 'backyard' and an drawing of a sandbox with a shovel in it

Backyard

Fast Python sandboxes for AI agents.

Backyard provides secure, stateful Python code execution via two backends:

  • MontySandbox — High-performance sandbox using pydantic-monty, a minimalistic Python interpreter written in Rust.
  • ContainerSandbox — Full-featured container-based sandbox using Podman or Docker, supporting all Python features including third-party package installation via uv.

The unified Sandbox entrypoint auto-routes code to Monty for compatible code and transparently converts to a container when unsupported features are detected (e.g., third-party imports, class definitions, and many standard library modules).

Features

  • Stateful execution — Variables persist across code blocks (like a Jupyter notebook)
  • Auto-routing — Starts with Monty for performance, transparently converts to a container when needed
  • Package management — Automatic dependency detection and installation via uv (container-based sandbox only)
  • Read-only mode — Dynamic permission switching for AI agent safety
  • File change tracking — Detect files created, modified, or deleted during execution
  • gVisor support — Automatic kernel-level sandboxing when runsc is available
  • Async APIrun_async() for non-blocking use in async agent harnesses

Note: The container-backed Sandbox requires Podman or Docker on your system. Monty-only usage (which supports limited stdlib modules plus external functions) comes built-in.

Quick example

from backyard import Sandbox

with Sandbox() as sb:
    # Automatic routing to Monty
    result = sb.run("x = 16; x ** 2")
    print(result.result)  # 256

    # State is preserved across calls
    result = sb.run("x + 1")
    print(result.result)  # 257

    # Auto-converts to container for full Python experience
    result = sb.run("import numpy; numpy.__version__")
    print(result.stdout)