DSPy: The framework for programming—not prompting—language models
Python
allow tool async to sync conversion (#8332)
Chen Qian6c130fa0d8f3
diff --git a/dspy/adapters/types/tool.py b/dspy/adapters/types/tool.pyindex b17cabd61..71008d341 100644--- a/dspy/adapters/types/tool.py+++ b/dspy/adapters/types/tool.py@@ -6,6 +6,7 @@ from jsonschema import ValidationError, validatefrom pydantic import BaseModel, TypeAdapter, create_modelfrom dspy.adapters.types.base_type import BaseType+from dspy.dsp.utils.settings import settingsfrom dspy.utils.callback import with_callbacksif TYPE_CHECKING:@@ -160,12 +161,26 @@ class Tool(BaseType):},}+ def _run_async_in_sync(self, coroutine):+ try:+ loop = asyncio.get_running_loop()+ except RuntimeError:+ return asyncio.run(coroutine)++ return loop.run_until_complete(coroutine)+@with_callbacksdef __call__(self, **kwargs):parsed_kwargs = self._validate_and_parse_args(**kwargs)result = self.func(**parsed_kwargs)if asyncio.iscoroutine(result):- raise ValueError("You are calling `__call__` on an async tool, please use `acall` instead.")+ if settings.allow_tool_async_sync_conversion:+ return self._run_async_in_sync(result)+ else:+ raise ValueError(+ "You are calling `__call__` on an async tool, please use `acall` instead or set "+ "`allow_async=True` to run the async tool in sync mode."+ )return result@with_callbacksdiff --git a/dspy/dsp/utils/settings.py b/dspy/dsp/utils/settings.pyindex fd7ce52f3..fc0f7300d 100644--- a/dspy/dsp/utils/settings.py+++ b/dspy/dsp/utils/settings.py@@ -27,6 +27,8 @@ DEFAULT_CONFIG = dotdict(provide_traceback=False, # Whether to include traceback information in error logs.num_threads=8, # Number of threads to use for parallel processing.max_errors=10, # Maximum errors before halting operations.+ # If true, async tools can be called in sync mode by getting converted to sync.+ allow_tool_async_sync_conversion=False,)# Global base configuration and owner trackingdiff --git a/tests/adapters/test_tool.py b/tests/adapters/test_tool.pyindex de2f64d88..58938b878 100644--- a/tests/adapters/test_tool.py+++ b/tests/adapters/test_tool.py@@ -5,6 +5,7 @@ import pytestfrom pydantic import BaseModelfrom dspy.adapters.types.tool import Tool+import dspy# Test fixtures@@ -363,3 +364,14 @@ async def test_async_concurrent_calls():# Check that it ran concurrently (should take ~0.1s, not ~0.5s)# We use 0.3s as threshold to account for some overheadassert end_time - start_time < 0.3+++def test_async_tool_call_in_sync_mode():+ tool = Tool(async_dummy_function)+ with dspy.context(allow_tool_async_sync_conversion=False):+ with pytest.raises(ValueError):+ result = tool(x=1, y="hello")++ with dspy.context(allow_tool_async_sync_conversion=True):+ result = tool(x=1, y="hello")+ assert result == "hello 1"