dspy.PythonInterpreter¶
Deno Installation¶
PythonInterpreter uses Deno and Pyodide to run Python in a local WASM sandbox. The recommended installation
keeps Deno in the same Python environment as DSPy:
The deno extra installs the official Deno Python distribution (>=2.4.5,<3.0.0). DSPy prefers that managed
binary when it is installed, so Python dependency locking also locks the Deno runtime. The extra provides binaries
for macOS x86-64/arm64, glibc Linux x86-64/arm64, and Windows x86-64 and adds approximately 40–50 MiB to the
environment.
On other platforms, install a compatible Deno 2.x release (>=2.0.0,<3.0.0) using the
Deno installation instructions. DSPy falls back to
the deno executable on PATH. An explicit deno_command passed to PythonInterpreter takes precedence over
both options.
DSPy disables ambient deno.json, lockfile, package.json, and local node_modules discovery for its default
runner. A package.json in the current directory or an ancestor therefore cannot redirect the sandbox’s pinned
Pyodide dependency. No DENO_NO_PACKAGE_JSON environment variable is required.
dspy.PythonInterpreter(deno_command: list[str] | None = None, enable_read_paths: list[PathLike | str] | None = None, enable_write_paths: list[PathLike | str] | None = None, enable_env_vars: list[str] | None = None, enable_network_access: list[str] | None = None, sync_files: bool = True, tools: dict[str, Callable[..., str]] | None = None, output_fields: list[dict] | None = None, callbacks: list[BaseCallback] | None = None)
¶
Local interpreter for secure Python execution using Deno and Pyodide.
Implements the Interpreter protocol for secure code execution in a WASM-based sandbox. Code runs in an isolated Pyodide environment with no access to the host filesystem, network, or environment by default.
Prerequisites
Install the managed Deno runtime with pip install "dspy[deno]", or
install a compatible Deno 2.x release system-wide.
Examples:
# Basic execution
with PythonInterpreter() as interp:
result = interp("print(1 + 2)") # Returns "3"
# With host-side tools
def my_tool(question: str) -> str:
return "answer"
with PythonInterpreter(tools={"my_tool": my_tool}) as interp:
result = interp("print(my_tool(question='test'))")
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
deno_command
|
list[str] | None
|
command list to launch Deno. |
None
|
enable_read_paths
|
list[PathLike | str] | None
|
Files or directories to allow reading from in the sandbox. |
None
|
enable_write_paths
|
list[PathLike | str] | None
|
Files or directories to allow writing to in the sandbox. All write paths will also be able to be read from for mounting. |
None
|
enable_env_vars
|
list[str] | None
|
Environment variable names to allow in the sandbox. |
None
|
enable_network_access
|
list[str] | None
|
Domains or IPs to allow network access in the sandbox. |
None
|
sync_files
|
bool
|
If set, syncs changes within the sandbox back to original files after execution. |
True
|
tools
|
dict[str, Callable[..., str]] | None
|
Dictionary mapping tool names to callable functions. Each function should accept keyword arguments and return a string. Tools are callable directly from sandbox code by name. |
None
|
output_fields
|
list[dict] | None
|
List of output field definitions for typed SUBMIT signature. Each dict should have ‘name’ and optionally ‘type’ keys. |
None
|
callbacks
|
list[BaseCallback] | None
|
Optional instance-level callback handlers. |
None
|
Source code in .venv/lib/python3.14/site-packages/dspy/primitives/python_interpreter.py
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 | |
Methods:¶
__call__(code: str, variables: dict[str, Any] | None = None) -> Any
¶
execute(code: str, variables: dict[str, Any] | None = None) -> Any
¶
Source code in .venv/lib/python3.14/site-packages/dspy/primitives/python_interpreter.py
740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 | |
shutdown() -> None
¶
Source code in .venv/lib/python3.14/site-packages/dspy/primitives/python_interpreter.py
start() -> None
¶
Initialize the Deno/Pyodide sandbox.
This pre-warms the sandbox by starting the Deno subprocess. Can be called explicitly for pooling, or will be called lazily on first execute().
Idempotent while the session is active. A stopped or shut-down session cannot be restarted because its Python state cannot be reconstructed.