dspy.PythonInterpreter¶
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)
¶
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
Deno must be installed: https://docs.deno.com/runtime/getting_started/installation/
Example
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
|
Source code in dspy/primitives/python_interpreter.py
Functions¶
__call__(code: str, variables: dict[str, Any] | None = None) -> Any
¶
execute(code: str, variables: dict[str, Any] | None = None) -> Any
¶
Source code in dspy/primitives/python_interpreter.py
shutdown() -> None
¶
Source code in 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: safe to call multiple times.
Source code in dspy/primitives/python_interpreter.py
:::