Skip to content

armgit / dspy

Public Imported from GitHub
0

DSPy: The framework for programming—not prompting—language models

Python

fix: await async tool functions in PythonInterpreter (#9754)

Michel Pavlukhin62f5c43ec8cb
diff --git a/dspy/primitives/python_interpreter.py b/dspy/primitives/python_interpreter.py
index a64960522..315405ed2 100644
--- a/dspy/primitives/python_interpreter.py
+++ b/dspy/primitives/python_interpreter.py
@@ -6,6 +6,7 @@ WASM environment using Deno and Pyodide. It implements the Interpreter
protocol defined in interpreter.py.
"""
+import asyncio
import functools
import inspect
import json
@@ -72,6 +73,17 @@ def _jsonrpc_error(code: int, message: str, id: int | str, data: dict | None = N
return json.dumps({"jsonrpc": "2.0", "error": err, "id": id})
+def _await_in_sync(coroutine: Any) -> Any:
+ """Run a coroutine to completion from a sync caller."""
+ try:
+ loop = asyncio.get_running_loop()
+ except RuntimeError:
+ loop = None
+ if loop is None:
+ return asyncio.run(coroutine)
+ return loop.run_until_complete(coroutine)
+
+
class PythonInterpreter:
"""Local interpreter for secure Python execution using Deno and Pyodide.
@@ -300,6 +312,8 @@ class PythonInterpreter:
if tool_name not in self.tools:
raise CodeInterpreterError(f"Unknown tool: {tool_name}")
result = self.tools[tool_name](**kwargs)
+ if asyncio.iscoroutine(result):
+ result = _await_in_sync(result)
is_json = isinstance(result, (list, dict))
response = _jsonrpc_result(
{"value": json.dumps(result) if is_json else (str(result) if result is not None else ""), "type": "json" if is_json else "string"},
diff --git a/tests/primitives/test_python_interpreter.py b/tests/primitives/test_python_interpreter.py
index e112db7c3..1e2ac9219 100644
--- a/tests/primitives/test_python_interpreter.py
+++ b/tests/primitives/test_python_interpreter.py
@@ -1,3 +1,4 @@
+import asyncio
import os
import random
@@ -376,6 +377,38 @@ def test_tool_error_surfaces_as_runtime_error():
assert "bad value: 42" in result
+def test_tool_async_def_function():
+ """async def tools should be awaited so the sandbox sees the resolved value."""
+
+ async def slow_search(query: str) -> str:
+ await asyncio.sleep(0)
+ return f"answer:{query}"
+
+ with PythonInterpreter(tools={"slow_search": slow_search}) as sandbox:
+ result = sandbox.execute("slow_search(query='hello')")
+ assert result == "answer:hello"
+
+
+def test_tool_async_def_raises_propagates():
+ """Exceptions raised inside an async tool should surface as RuntimeError in the sandbox."""
+
+ async def failing_async(x: int) -> str:
+ await asyncio.sleep(0)
+ raise ValueError(f"boom:{x}")
+
+ with PythonInterpreter(tools={"failing_async": failing_async}) as sandbox:
+ result = sandbox.execute(
+ "try:\n"
+ " failing_async(7)\n"
+ " output = 'no error'\n"
+ "except RuntimeError as e:\n"
+ " output = str(e)\n"
+ "output"
+ )
+ assert "ValueError" in result
+ assert "boom:7" in result
+
+
# =============================================================================
# Multi-Output SUBMIT Tests