DSPy: The framework for programming—not prompting—language models
Python
fix(dspy): reject sync async-tool calls inside running loops (#10146)
Isaac Miller079ef05aa711
diff --git a/dspy/adapters/types/tool.py b/dspy/adapters/types/tool.pyindex 8dc81e398..aaa31da31 100644--- a/dspy/adapters/types/tool.py+++ b/dspy/adapters/types/tool.py@@ -176,7 +176,13 @@ class Tool(Type):if loop is None:return asyncio.run(coroutine)- return loop.run_until_complete(coroutine)++ coroutine.close()+ raise ValueError(+ "You are calling `__call__` on an async tool from within a running event loop, which cannot be "+ "converted to a sync call, even with `allow_tool_async_sync_conversion` enabled. Please use "+ "`await tool.acall(...)` instead."+ )@with_callbacksdef __call__(self, **kwargs):diff --git a/tests/adapters/test_tool.py b/tests/adapters/test_tool.pyindex 1e717308d..bfce1df79 100644--- a/tests/adapters/test_tool.py+++ b/tests/adapters/test_tool.py@@ -408,6 +408,14 @@ def test_async_tool_call_in_sync_mode():assert result == "hello 1"+async def test_async_tool_sync_call_within_running_loop():+ tool = Tool(async_dummy_function)+ with dspy.context(allow_tool_async_sync_conversion=True):+ with pytest.raises(ValueError, match=r".*await tool\.acall.*"):+ tool(x=1, y="hello")++TOOL_CALL_TEST_CASES = [([], {"tool_calls": []}),(