dspy
dspy copied to clipboard
Add optional timeout to PythonInterpreter to fix infinite/long hangs
Summary:
Adds a timeout parameter to dspy.primitives.python_interpreter.PythonInterpreter to cap how long sandboxed code is allowed to run. When a timeout is provided, stdout is read via a background thread; if no output arrives before the deadline, the Deno subprocess is killed and an InterpreterError is raised. This guards against code hanging indefinitely.
Changes:
- Added
timeout: float | Nonein the constructor and store it on the instance. - Wrap the stdout read in
_read_output_line, which either blocks normally or enforces the timeout by joining a reader thread and, on timing out, terminating the sandbox process. - Ensure timed-out executions clean up the underlying Deno process so future runs can restart cleanly.
@isaacbmiller Mind taking a look?