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 .venv/lib/python3.14/site-packages/dspy/primitives/python_interpreter.py
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 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 | |
Functions¶
__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
458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 | |
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: safe to call multiple times.
Source code in .venv/lib/python3.14/site-packages/dspy/primitives/python_interpreter.py
:::