dspy.LocalInterpreter¶
LocalInterpreter runs Python in one persistent local CPython worker. It is useful when generated code needs
ordinary Python compatibility plus separation from the DSPy process’s memory, stdout, and lifecycle. State and
imports persist until shutdown(), host tools cross a JSON protocol, and execution_timeout can terminate a stuck
worker.
import dspy
rlm = dspy.RLM(
"question: str -> answer: int",
interpreter_factory=dspy.LocalInterpreter,
)
A subprocess is not a security sandbox
Generated code retains the host user’s filesystem, environment, credentials, subprocess, and network authority.
Use the default PythonInterpreter or a remote sandbox for untrusted code. The subprocess
boundary protects ordinary host memory and stdout from accidental mutation; it does not contain hostile code.
Inputs, host-tool arguments/results, and structured outputs must be JSON-compatible. The worker uses the current
Python executable.
execution_timeout includes time spent in host tools and terminates the worker promptly when the deadline expires.
Python cannot forcibly stop a running host callable, so that callable may finish in a detached daemon thread; its
result is discarded and the interpreter session remains terminal.
Parallel guest code is supported when all threads finish before the current execute() call returns, such as a
context-managed ThreadPoolExecutor. Leaving a guest thread running makes the session terminal because that thread
could otherwise mutate state, write output, or invoke tools during a later execution. Shutdown and terminal failures
also terminate ordinary descendant processes with the worker.
LocalInterpreter.execution_instructions is stable class metadata. RLM adds it to the action prompt without
starting a worker.
dspy.LocalInterpreter(tools: dict[str, Callable[..., Any]] | None = None, output_fields: list[dict[str, Any]] | None = None, *, execution_timeout: float | None = None, callbacks: list[BaseCallback] | None = None)
¶
Execute Python in a persistent local subprocess.
This separates generated code from DSPy’s memory, stdout, and lifecycle, but it is not a security sandbox. The worker retains the host user’s files, environment, credentials, subprocess, and network authority.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tools
|
dict[str, Callable[..., Any]] | None
|
Host functions exposed to executed code by name. Arguments and return values must be JSON-compatible. |
None
|
output_fields
|
list[dict[str, Any]] | None
|
Optional field definitions for typed |
None
|
execution_timeout
|
float | None
|
Maximum seconds for one execution, including host tool calls. A timeout terminates the worker and its session state. Python cannot forcibly stop a running host callable, so a timed-out callable may finish in a detached daemon thread; its result is discarded. |
None
|
callbacks
|
list[BaseCallback] | None
|
Optional instance-level callback handlers. |
None
|
Source code in dspy/primitives/local_interpreter.py
Methods:¶
__call__(code: str, variables: dict[str, Any] | None = None) -> Any
¶
execute(code: str, variables: dict[str, Any] | None = None) -> Any
¶
Execute code in the worker’s persistent namespace.
Source code in dspy/primitives/local_interpreter.py
225 226 227 228 229 230 231 232 233 234 235 236 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 | |
shutdown() -> None
¶
start() -> None
¶
Start the worker, or return immediately if it is already running.