monty README

The project's README from github.com/pydantic/monty. As one markdown file: README.md.

Monty

A secure Python sandbox, written in Rust, for code written by AI.

CI Codspeed Coverage PyPI NPM crates.io license Join Slack

Monty avoids the latency, complexity and cost of a container based sandbox for running LLM generated code. It comes in two forms: OSS Monty, the MIT licensed Python 3.14 sandbox you install as a package, and Full Monty, the commercial server that runs the same sandbox behind a WebSocket as a service.

A new OSS Monty sandbox takes under 1ms from a running pool (Full Monty: about 2ms) compared to around 1500ms for a sandbox service.

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:

monty (pydantic/monty)

Searching Database…

8.7K ·GitHub
这是什么?Monty 是一个用 Rust 编写的精简、安全的 Python 解释器,旨在让 AI 模型运行生成的 Python 代码,而不必承担容器沙箱的延迟和开销。
这是我安装的库,还是一项托管服务?它有两种形态:OSS Monty 是你可以直接安装的 MIT 许可 Python 包;Full Monty 则是商业服务器,把同样的沙箱放在 WebSocket 后面作为服务运行。
怎么开始使用它?把 OSS Monty 作为 Python 包安装,然后按照文档中 Python、JavaScript 或 Rust 的入门指南操作;商业版 Full Monty 则以同一套 worker 的容器镜像形式部署。
我需要 Python 3.14 吗,还是也能从其他语言调用它?OSS Monty 面向 Python 3.14,但也有针对 Go(gomonty)和 Dart/Flutter(dartmonty)的社区绑定,另外还提供 JavaScript 和 Rust 的原生入门指南。
沙箱里的代码不能做什么?沙箱内部没有文件系统、没有环境变量、也没有网络——代码只能通过你显式传入的函数和挂载来访问宿主机,而且只能运行受支持的 Python 子集。
Monty 是完全开源的,还是需要付费方案?OSS Monty 本身采用 MIT 许可,作为包可以免费安装;如果你想要托管的 WebSocket 服务,Full Monty 是另外单独的商业托管版本。
Monty 比基于容器的沙箱快多少?从运行中的资源池启动一个全新的 OSS Monty 沙箱用时不到 1 毫秒(Full Monty 约 2 毫秒),而典型的容器沙箱服务大约需要 1500 毫秒。
Monty 与 Pydantic AI 是什么关系?Monty 驱动着 Pydantic AI 的“Code Mode”功能,是更庞大的 Pydantic Stack 的一部分,与 Pydantic Logfire 和 Logfire AI Gateway 并列。
我能自己运行 Full Monty 吗?Full Monty 是 Pydantic 的商业产品:把同样的沙箱 worker 打包成容器镜像并通过 WebSocket API 对外提供,并不是让你从源码自行托管的东西。
与 Docker、Pyodide 或 WASI 沙箱相比,Monty 表现如何?文档中有与这些替代方案的直接对比,认为 Monty 避免了它们的容器启动延迟、复杂性和成本,同时仍能隔离 AI 生成的代码。
What are similar projects?daytona, SPACE, smolvm, CubeSandbox, moejs, box, OpenSandbox, quicksand
Creator
pydantic
Size
18.1 MB
Stars
8.7K
Age
3y
Commits · 90 Days