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/
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
|
Source code in .venv/lib/python3.14/site-packages/dspy/primitives/python_interpreter.py
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 | |
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
604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 | |
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.