pydantic/monty

A minimal, secure Python interpreter written in Rust for use by AI

8,207

stars

740

commits

Rust

primary language

Sep 12, 2026

updated

pydantic.dev/docs/monty/
python
rust
sandbox
sandboxing

README

Monty

A sandboxed Python interpreter, written in Rust, for code written by AI.

CI Codspeed Coverage PyPI NPM crates.io license Join Slack

[!NOTE] Hack Monty Round 3 is live - the last round before Monty V1. See pydantic.dev/monty for details.

Monty runs Python written by a model with no container, VM or sandboxing service in the loop. Creating a sandbox and running ten commands in it takes 5 ms (Docker: 900 ms, a sandboxing service: 1900 ms), a paused interpreter serialises to bytes you can resume later, and memory, time and recursion limits are enforced by the VM itself. Filesystem, environment variables and network do not exist inside the sandbox: it reaches the host only through the functions and mounts you pass in.

Documentation: pydantic.dev/docs/monty

Install

uv add pydantic-monty        # Python
npm install @pydantic/monty  # JavaScript / TypeScript
cargo add monty              # Rust

The commercial Full Monty runs the same workers as a container image.

Example

The code string is what a model writes when asked how long a bar of chocolate could power a lightbulb:

from pydantic_monty import Monty

code = """
kcal = nutrition('chocolate bar')['kcal']
hours = kcal * 4184 / (bulb_watts * 3600)
print(f'a chocolate bar powers a {bulb_watts} W bulb for {hours:.1f} hours')
"""

with Monty() as pool:
    with pool.checkout() as session:
        session.feed_run(
            code,
            inputs={'bulb_watts': 10},
            external_lookup={'nutrition': lambda food: {'kcal': 230}},
        )
        #> a chocolate bar powers a 10 W bulb for 26.7 hours

nutrition ran on the host and the sandbox saw only its return value.

Documentation

Monty runs Code Mode in Pydantic AI. Community bindings: gomonty (Go) and dart_monty (Dart / Flutter).

Part of the Pydantic Stack

The Pydantic Stack is everything you need to ship production-grade AI agents:

Contributors

(top 30 of 52)

samuelcolvin

445 commits

davidhewitt

154 commits

rewitt94

52 commits

petyosi

8 commits

pydantic/monty

A minimal, secure Python interpreter written in Rust for use by AI

8,207

stars

740

commits

Rust

primary language

Sep 12, 2026

updated

pydantic.dev/docs/monty/
python
rust
sandbox
sandboxing

README

Monty

A sandboxed Python interpreter, written in Rust, for code written by AI.

CI Codspeed Coverage PyPI NPM crates.io license Join Slack

[!NOTE] Hack Monty Round 3 is live - the last round before Monty V1. See pydantic.dev/monty for details.

Monty runs Python written by a model with no container, VM or sandboxing service in the loop. Creating a sandbox and running ten commands in it takes 5 ms (Docker: 900 ms, a sandboxing service: 1900 ms), a paused interpreter serialises to bytes you can resume later, and memory, time and recursion limits are enforced by the VM itself. Filesystem, environment variables and network do not exist inside the sandbox: it reaches the host only through the functions and mounts you pass in.

Documentation: pydantic.dev/docs/monty

Install

uv add pydantic-monty        # Python
npm install @pydantic/monty  # JavaScript / TypeScript
cargo add monty              # Rust

The commercial Full Monty runs the same workers as a container image.

Example

The code string is what a model writes when asked how long a bar of chocolate could power a lightbulb:

from pydantic_monty import Monty

code = """
kcal = nutrition('chocolate bar')['kcal']
hours = kcal * 4184 / (bulb_watts * 3600)
print(f'a chocolate bar powers a {bulb_watts} W bulb for {hours:.1f} hours')
"""

with Monty() as pool:
    with pool.checkout() as session:
        session.feed_run(
            code,
            inputs={'bulb_watts': 10},
            external_lookup={'nutrition': lambda food: {'kcal': 230}},
        )
        #> a chocolate bar powers a 10 W bulb for 26.7 hours

nutrition ran on the host and the sandbox saw only its return value.

Documentation

Monty runs Code Mode in Pydantic AI. Community bindings: gomonty (Go) and dart_monty (Dart / Flutter).

Part of the Pydantic Stack

The Pydantic Stack is everything you need to ship production-grade AI agents:

Contributors

(top 30 of 52)

samuelcolvin

445 commits

davidhewitt

154 commits

rewitt94

52 commits

petyosi

8 commits

Languages

Rust

71.2%

Python

23.4%

TypeScript

5.1%