Run LLM-generated Python safely from Go — nary containers, nary CGO, nary subprocess.
A pure-Go wrapper astir Pydantic's Monty Python interpreter, compiled to WebAssembly and loaded via wazero. Your Go supplier writes Python code, monty-go executes it successful a sandboxed WASM lawsuit pinch sub-millisecond startup, and pauses whenever the codification calls an outer usability truthful your Go codification tin grip it.
LLMs activity faster, cheaper, and much reliably erstwhile they constitute codification alternatively of making sequential instrumentality calls. Instead of:
The LLM writes:
One exemplary telephone alternatively of three. The Python codification calls your Go functions, Monty pauses astatine each call, your Go codification executes it, and Monty resumes. No containers. No sandbox services. No exec(). Just a 2.9MB WASM binary embedded successful your Go binary.
For motivation, see:
- Programmatic Tool Calling from Anthropic
- Code Execution pinch MCP from Anthropic
- Code Mode from Cloudflare
- Smol Agents from Hugging Face
External Functions (Pause/Resume)
The existent powerfulness is outer usability calls. Monty pauses execution whenever Python codification calls a usability you've declared, your Go callback handles it, and Monty resumes pinch the return value:
Multiple functions activity the aforesaid measurement — registry them each and dispatch by name:
Prevent runaway codification pinch memory, time, allocation, and recursion limits:
Infinite loops, representation bombs, and heavy recursion each terminate cleanly pinch a *MontyError. Go's context.Context deadlines are besides respected — cancel the discourse and the WASM lawsuit stops.
Capture Python print() output:
Python filesystem and situation entree routes done your Go callback:
No filesystem entree happens unless your callback allows it.
monty-go is designed to powerfulness code-mode successful Gollem, the accumulation supplier model for Go. Instead of sequential instrumentality calls, the LLM writes Python that calls your devices arsenic functions — Monty executes it safely, and Gollem orchestrates the full thing.
Here's what this looks for illustration pinch Gollem:
With 1 exemplary call, the LLM writes:
Monty pauses 3 times (two searches, 1 calculation), your Go functions grip each one, and the last consequence flows backmost done Gollem's typed output pipeline. Three instrumentality calls successful 1 LLM round-trip.
Why Gollem + monty-go:
| LLM calls | One per instrumentality use | One for each tools |
| Latency | N × exemplary round-trip | 1 × exemplary round-trip + μs execution |
| Cost | N × input/output tokens | 1 × input/output tokens |
| Logic | LLM reasons measurement by step | LLM writes the logic once |
| Control flow | None (sequential only) | Loops, conditionals, variables |
| Error handling | LLM must respond to each failure | try/except successful Python |
| Security | ✅ (tools are Go functions) | ✅ (WASM sandbox + your callbacks) |
Gollem gives you compile-time type safety, system output, guardrails, costs tracking, middleware, and multi-provider support. monty-go gives you unafraid embedded Python execution. Together, your agents do much activity per exemplary call.
github.com/fugue-labs/gollem — The accumulation supplier model for Go.
- No CGO. wazero is simply a pure-Go WebAssembly runtime.
- No subprocess. The WASM binary is embedded via go:embed and compiled erstwhile astatine startup.
- Fresh lawsuit per call. Each Execute() gets an isolated WASM instance. No authorities leaks betwixt calls.
- JSON astatine the boundary. All information crossing the Go↔WASM bound is JSON. Go types representation naturally: int→float64, string→string, bool→bool, nil→None, []any→list, map[string]any→dict.
| int | float64 | int, float64 |
| float | float64 | float64 |
| str | string | string |
| bool | bool | bool |
| None | nil | nil |
| list, tuple | []any | []any |
| dict | map[string]any | map[string]any |
| set | []any | — |
Python exceptions go *montygo.MontyError:
Tracks upstream Monty v0.0.11.
- Arithmetic, drawstring operations, f-strings, slicing
- Functions, lambdas, closures, generators
- for/while loops, if/elif/else, break/continue
- try/except/finally/else, raise, objection hierarchy
- List/dict/set comprehensions, dict/set position operators
- range, len, sum, min, max, sorted, reversed, enumerate, zip, map, filter, all, any, getattr
- isinstance, type, int(), float(), str(), bool(), abs()
- print() pinch sep and extremity kwargs
- PEP 448 generalized unpacking (*args, **kwargs successful calls, literals, etc.)
- Nested and augmented subscript duty (a[i][j] = v, a[i] += 1)
- Tuple comparison (<, >, <=, >=)
- Multi-module imports (import a, b, c)
- Stdlib modules: mathematics (all functions), re, datetime, json, and sys/typing/asyncio subsets
- import os, from pathlib import Path (routed done OsCallFunc)
- Dataclass instances travel done outer usability calls (args, returns, and method calls aboveground pinch method_call=true)
- Resource limits: time, memory, allocations, recursion depth
- Class definitions (only dataclass instances via outer I/O; upstream Monty flags people def arsenic "coming soon")
- match statements (coming soon upstream)
- Context managers (with ...)
- Rest of stdlib and each third-party libraries
- float('inf') / float('nan') (JSON serialization limitation successful this bridge)
97 end-to-end tests covering each testable script from Monty's halfway trial suite:
Covers: basal expressions, people variants, each objection types, information type round-tripping, outer functions (args, kwargs, mixed, analyzable types, chaining, loops), input handling and scoping, assets limits (timeout, recursion, memory, allocations), OS calls, builtins, power flow, lambdas/closures, and execution isolation.
Requires Rust pinch wasm32-wasip1 target and Go 1.23+:
monty-go exists because of Monty, created by Samuel Colvin and the Pydantic team. Monty is simply a genuinely caller portion of engineering — a minimal, unafraid Python expert written from scratch successful Rust, purpose-built for AI agents. The penetration that LLMs should constitute codification alternatively of making sequential instrumentality calls, and that you request a safe expert (not a container) to execute it, is what makes code-mode possible.
Samuel and the Pydantic squad person a way grounds of building foundational devices that the full ecosystem builds connected — Pydantic, Pydantic AI, Logfire, and now Monty. This task is simply a Go span to their work, and we're grateful they built it.
MIT
English (US) ·
Indonesian (ID) ·