Skip to main content

Fast Python sandboxes for AI agents.

Project description

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

--> Documentation

Backyard

Fast Python sandbox for AI agent harnesses.

backyard supports safe and transparent execution of Python code using either:

  • Pydantic-Monty: an ultra-fast, minimal Python interpreter written in Rust
  • A container (Docker or Podman) with full-featured Python support

Getting started

Installation

Using uv (recommended):

uv add backyard

Or, with pip:

pip install backyard

Python API

from backyard import Sandbox

sandbox = Sandbox()

code = """
print("hello from the sandbox! 🏖🏰")
"""

result = sandbox.run(code)

print(result.stdout)

TUI

backyard comes with a notebook-like Text-based User Inferface (TUI) for trying out the sandboxes in your terminal. It requires the tui extra:

uv add "backyard[tui]"
uv run backyard

# Or, if you've activated the .venv already, you can run:
# backyard

Here's an example of the built-in TUI for accessing the sandboxes in a notebook-like environment.

It uses the Monty sandbox by default and transparently switches to a container-backed sandbox when executing code that requires third-party Python libraries or network access.

TUI demo Demo recorded with asciinema.

Security

Both sandbox backends provide strong security measures for running agent-generated code: read-only file mounting, resource constraints (memory, CPU usage, timeouts), and network restrictions.

For an added layer of security for container-based sandboxes, backyard supports the gVisor container runtime.

gVisor (runsc) sandboxing

For maximum isolation when running untrusted code, backyard automatically detects and uses gVisor (runsc) as the container runtime if it is registered with your container engine. gVisor provides an additional kernel-level security boundary between the sandboxed code and the host system.

If gVisor is not detected, backyard falls back to the default runc runtime and logs a warning.

Installing gVisor

Click to see instructions
  1. Install the runsc binary:

    # Ubuntu / Debian
    sudo apt-get install runsc
    
    # Or download the latest release
    wget https://storage.googleapis.com/gvisor/releases/release/latest/runsc
    sudo mv runsc /usr/local/bin/runsc
    sudo chmod +x /usr/local/bin/runsc
    
  2. Register runsc with your container engine by editing the daemon configuration:

    Docker — add to /etc/docker/daemon.json:

    {
        "runtimes": {
            "runsc": {
                "path": "/usr/local/bin/runsc"
            }
        }
    }
    

    Podman — add to ~/.config/containers/containers.conf or /etc/containers/containers.conf:

    [engine.runtimes]
    runsc = ["/usr/local/bin/runsc"]
    
  3. Restart the container engine:

    # Docker
    sudo systemctl restart docker
    
    # Podman (no daemon, but verify the config is picked up)
    podman info --format '{{json .Host.OCIRuntimes}}'
    

Once configured, backyard will automatically detect and use gVisor for all container sandboxes, providing an additional kernel-level security boundary.

FAQ

Why did you create this?

To power the code execution tool for an AI coding agent harness I'm developing.

Originally, I built the dual-engine sandbox for fine-tuning large language models using Reinforcement Learning from Verifiable Rewards (RLVR) with Group Relative Policy Optimization (GRPO). The sandbox provided an environment to safely execute parallel batches of model-generated code, computing rewards for training the model.

I modified and expanded the sandbox capabilities for use in the breeze coding agent harness I am building.

To adapt the sandbox to the needs of AI agents, I replaced the stateless, parallel execution approach with a stateful system that enables agents to execute code interactively, like a Python notebook or advanced REPL.

Where does the name backyard come from?

The term sandbox refers to a controlled environment where untrusted code can be run safely.

In the physical world, a sandbox is a place where children explore and imagine as they create and shape the world around them. 🏖🏰

Where are sandboxes placed? In the backyard.

License

This project is distributed under the MIT license.

Acknowledgements

A big thank you to the following teams for their work that makes this project possible:

  • The Pydantic team for creating Monty, the Rust-based Python interpreter that powers the ultra-fast default sandbox in backyard.
  • The Podman and Docker teams for building container runtimes that power the full-featured sandbox.
  • The Astral team for developing amazing tooling: the uv package manager (used in the full-featured sandbox), the ruff linter, and the ty type checker.
  • Will McGugan and contributors to Textual, the TUI framework that powers backyard's notebook-like TUI.
  • The Zensical team for the static site generator that powers backyard's documentation site.
  • Google's Gemini team for Gemini 3.6 Flash and the Nano Banana image-generation model, which I used to design the logo.
  • Scofield Eric for creating vectorize-image, which I used to convert the logo into SVG format
  • The marimo team for maintaining the list of import-to-PyPI package names used in backyard for automatic dependency installation

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

backyard-0.3.0.tar.gz (66.8 kB view details)

Uploaded Source

Built Distribution

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

backyard-0.3.0-py3-none-any.whl (80.1 kB view details)

Uploaded Python 3

File details

Details for the file backyard-0.3.0.tar.gz.

File metadata

  • Download URL: backyard-0.3.0.tar.gz
  • Upload date:
  • Size: 66.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.13

File hashes

Hashes for backyard-0.3.0.tar.gz
Algorithm Hash digest
SHA256 749f62ddce40a022675c786ab0e69209d151323431e720304eac45bd89d019ad
MD5 9dc0ae1961bed4e4f5af17050f747ae0
BLAKE2b-256 21f885382a4f04028010998ffbf293b26eb651a4a4c247751ad6306d8df41315

See more details on using hashes here.

Provenance

The following attestation bundles were made for backyard-0.3.0.tar.gz:

Publisher: publish.yml on rparkr/backyard

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file backyard-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: backyard-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 80.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.13

File hashes

Hashes for backyard-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 abcf671c467052a8160ff0205973c7b690df6a55f41cb64cdc7b5300d570112a
MD5 0cacbc352594ef38bad99a614385913f
BLAKE2b-256 9f17fa330396aba27e6e3c3da03da00ba48c9c5386ca016392e69ec159dbce98

See more details on using hashes here.

Provenance

The following attestation bundles were made for backyard-0.3.0-py3-none-any.whl:

Publisher: publish.yml on rparkr/backyard

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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