DSPy: The framework for programming—not prompting—language models
Python
Make PythonInterpreter request handling robust (#10208)
Isaac Miller80b118e52cb1
diff --git a/dspy/primitives/python_interpreter.py b/dspy/primitives/python_interpreter.pyindex fe5d1c7ed..9c9e671de 100644--- a/dspy/primitives/python_interpreter.py+++ b/dspy/primitives/python_interpreter.py@@ -16,6 +16,7 @@ import loggingimport mathimport osimport re+import secretsimport shutilimport subprocessimport threading@@ -336,9 +337,9 @@ class PythonInterpreter:self.deno_process = Noneself._mounted_files = False- self._request_id = 0self._last_diagnostic: str | None = Noneself._owner_thread: int | None = None+ self._handling_tool_call = Falseself._pending_large_vars = {}self._session_ended = False@@ -503,7 +504,11 @@ class PythonInterpreter:kwargs = params.get("kwargs", {})try:- result = self.invoke_tool(tool_name, kwargs)+ self._handling_tool_call = True+ try:+ result = self.invoke_tool(tool_name, kwargs)+ finally:+ self._handling_tool_call = Falseresult = _make_jsonable(result)if result is None or isinstance(result, str):response = _jsonrpc_result({"value": str(result) if result is not None else "", "type": "string"}, request_id)@@ -594,10 +599,9 @@ class PythonInterpreter:logger.debug("Skipping malformed JSON during %s: %s", context, response_line[:100])return None- def _next_request_id(self) -> int:- self._request_id += 1+ def _next_request_id(self) -> str:self._last_diagnostic = None- return self._request_id+ return secrets.token_hex(16)def _handle_out_of_band_message(self, msg: dict, context: str) -> bool:"""Consume a message that is not a response to any request (a notification@@ -760,6 +764,8 @@ class PythonInterpreter:code: str,variables: dict[str, Any] | None = None,) -> Any:+ if self._handling_tool_call:+ raise CodeInterpreterError("PythonInterpreter cannot execute recursively from one of its tools.")self._check_session_active()self._check_thread_ownership()variables = variables or {}diff --git a/dspy/primitives/runner.js b/dspy/primitives/runner.jsindex 3ab6e842a..ae6bfce4a 100644--- a/dspy/primitives/runner.js+++ b/dspy/primitives/runner.js@@ -3,6 +3,7 @@import pyodideModule from "npm:[email protected]/pyodide.js";import { readLines } from "https://deno.land/[email protected]/io/mod.ts";+const JSON = Object.freeze(globalThis.JSON), console = Object.freeze(globalThis.console);// =============================================================================// Python Code Templates// =============================================================================@@ -19,7 +20,7 @@ sys.stdout, sys.stderr = buf_stdout, buf_stderrdef last_exception_args():return json.dumps(sys.last_exc.args) if sys.last_exc else None-class FinalOutput(BaseException):+class _DSPyFinalOutput(BaseException):# Control-flow exception to signal completion (like StopIteration)pass@@ -27,24 +28,24 @@ class FinalOutput(BaseException):# Only define if not already registered with typed signatures.if 'SUBMIT' not in dir():def SUBMIT(output):- raise FinalOutput({"output": output})+ raise _DSPyFinalOutput({"output": output})`;// Generate a tool wrapper function with typed signature.// Parameters is an array of {name, type?, default?} objects.// Convert a JavaScript/JSON value to Python literal syntaxconst toPythonLiteral = (value) => {- if (value === null) return 'None';- if (value === true) return 'True';- if (value === false) return 'False';- return JSON.stringify(value); // Works for strings, numbers, arrays, objects+ const serialized = JSON.stringify(value);+ return `__import__("json").loads(${JSON.stringify(serialized)})`;};+const toPythonType = (type) => type === "NoneType" ? "type(None)" : type;+const makeToolWrapper = (toolName, parameters = []) => {// Build signature parts: "query: str, limit: int = 10"const sigParts = parameters.map(p => {let part = p.name;- if (p.type) part += `: ${p.type}`;+ if (p.type) part += `: ${toPythonType(p.type)}`;if (p.default !== undefined) part += ` = ${toPythonLiteral(p.default)}`;return part;});@@ -71,20 +72,20 @@ const makeSubmitWrapper = (outputs) => {// Fallback to single-arg SUBMIT if no outputs definedreturn `def SUBMIT(output):- raise FinalOutput({"output": output})+ raise _DSPyFinalOutput({"output": output})`;}const sigParts = outputs.map(o => {let part = o.name;- if (o.type) part += `: ${o.type}`;+ if (o.type) part += `: ${toPythonType(o.type)}`;return part;});const dictParts = outputs.map(o => `"${o.name}": ${o.name}`);return `def SUBMIT(${sigParts.join(', ')}):- raise FinalOutput({${dictParts.join(', ')}})+ raise _DSPyFinalOutput({${dictParts.join(', ')}})`;};@@ -114,7 +115,7 @@ const JSONRPC_APP_ERRORS = {};const jsonrpcRequest = (method, params, id) =>- JSON.stringify({ jsonrpc: "2.0", method, params, id });+ JSON.stringify({ __proto__: null, jsonrpc: "2.0", method, params: { __proto__: null, ...params }, id });const jsonrpcNotification = (method, params = null) => {const msg = { jsonrpc: "2.0", method };@@ -351,8 +352,8 @@ while (true) {// error.message is mostly blank.const errorMessage = (error.message || "").trim();- // Handle FinalOutput as a success result, not an error- if (errorType === "FinalOutput") {+ // Handle SUBMIT's control-flow exception as a success result, not an error+ if (errorType === "_DSPyFinalOutput") {const last_exception_args = pyodide.globals.get("last_exception_args");const errorArgs = JSON.parse(last_exception_args()) || [];const answer = errorArgs[0] || null;diff --git a/tests/primitives/test_python_interpreter.py b/tests/primitives/test_python_interpreter.pyindex 87f9414eb..2cd28ec95 100644--- a/tests/primitives/test_python_interpreter.py+++ b/tests/primitives/test_python_interpreter.py@@ -720,6 +720,26 @@ def test_tool_with_typed_signature(configure_pooled_interpreter):assert result == "searched 'test' with limit 5"+def test_tool_none_type_and_nested_json_default():+ def tool(value: type(None) = None, items: list = [None]): # noqa: B006+ return [value, items]++ with PythonInterpreter(tools={"tool": tool}) as interpreter:+ assert interpreter.execute("tool()") == [None, [None]]+++ ("field", "code", "expected"),+ [+ ({"name": "nothing", "type": "NoneType"}, "SUBMIT(nothing=None)", {"nothing": None}),+ ({"name": "FinalOutput", "type": "str"}, "SUBMIT(FinalOutput='ok')", {"FinalOutput": "ok"}),+ ],+)+def test_submit_valid_runtime_edge_cases(field, code, expected):+ with PythonInterpreter(output_fields=[field]) as interpreter:+ assert interpreter.execute(code) == FinalOutput(expected)++def test_tool_positional_args(configure_pooled_interpreter):"""Test that tools work with positional arguments."""@@ -798,6 +818,51 @@ def test_protocol_failure_ends_session(monkeypatch):interpreter.execute("2 + 2")+def test_request_ids_are_128_bit_random_values():+ interpreter = PythonInterpreter(deno_command=["deno"])+ request_ids = {interpreter._next_request_id() for _ in range(100)}++ assert len(request_ids) == 100+ assert all(len(request_id) == 32 and int(request_id, 16) >= 0 for request_id in request_ids)+++def test_guest_prototype_hook_cannot_redirect_tool_wrapper():+ calls = []++ def benign():+ calls.append("benign")+ return "safe"++ def danger():+ calls.append("danger")+ return "unsafe"++ with PythonInterpreter(tools={"benign": benign, "danger": danger}) as interpreter:+ result = interpreter.execute(+ "import js\n"+ "js.eval('Object.prototype.toJSON = function() { "+ 'if (Object.hasOwn(this, "name")) return {name: "danger", kwargs: {}}; '+ "return this; }')\n"+ "benign()"+ )++ assert result == "safe"+ assert calls == ["benign"]+++def test_tool_cannot_reenter_same_interpreter():+ holder = {}++ def nested_execute():+ return holder["interpreter"].execute("'nested'")++ with PythonInterpreter(tools={"nested_execute": nested_execute}) as interpreter:+ holder["interpreter"] = interpreter+ with pytest.raises(CodeExecutionError, match="cannot execute recursively"):+ interpreter.execute("nested_execute()")+ assert interpreter.execute("40 + 2") == 42++def test_failed_health_check_ends_session(monkeypatch):interpreter = PythonInterpreter()monkeypatch.setattr(@@ -1524,9 +1589,11 @@ def test_id_less_protocol_errors_are_terminal():interpreter._handle_out_of_band_message(parse_error, "during test")-def test_unsolicited_error_line_is_not_consumed_as_the_response():+def test_unsolicited_error_line_is_not_consumed_as_the_response(monkeypatch):"""#10165: an id-less async error arriving ahead of the real response (thewire trace an unhandled rejection produces) must not be mistaken for it."""+ request_id = "a" * 32+ monkeypatch.setattr(python_interpreter.secrets, "token_hex", lambda _: request_id)class FakeDeno:def __init__(self, lines):@@ -1540,7 +1607,7 @@ def test_unsolicited_error_line_is_not_consumed_as_the_response():interpreter = PythonInterpreter()interpreter.deno_process = FakeDeno([json.dumps({"jsonrpc": "2.0", "error": {"code": -32007, "message": "Unhandled async error: PythonError"}, "id": None}),- json.dumps({"jsonrpc": "2.0", "result": {"output": "ok\n"}, "id": 1}),+ json.dumps({"jsonrpc": "2.0", "result": {"output": "ok\n"}, "id": request_id}),])assert interpreter.execute("print('ok')") == "ok\n"