Commit d42f29e
authored
Avoid unwanted exception chaining when running async tool in sync context (#9092)
Running async tool under sync context (require `settings.allow_tool_async_sync_conversion`) is handled by `Tool._run_async_in_sync`. If there's no running event loop, we used to run the coroutine directly when handling the `RuntimeError` raised by `asyncio.get_running_loop()`. This resulted in exception chaining, where the error raised by the tool will be chained with the `RuntimeError` raised by `asyncio`. Since the result will be passed to the LM as tool call result, such behavior is regarded as unnecessary leak of implementation detail, and will diverge the result of running with `async` and in sync. Move the real call outside of the `except` block will resolve the problem.
## Before
```
Execution error in retrieve:
Traceback (most recent call last):
File ".venv\Lib\site-packages\dspy\adapters\types\tool.py", line 167, in _run_async_in_sync
loop = asyncio.get_running_loop()
RuntimeError: no running event loop
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File ".venv\Lib\site-packages\dspy\predict\react.py", line 111, in forward
trajectory[f"observation_{idx}"] = self.tools[pred.next_tool_name](**pred.next_tool_args)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^
File ".venv\Lib\site-packages\dspy\utils\callback.py", line 326, in sync_wrapper
return fn(instance, *args, **kwargs)
File ".venv\Lib\site-packages\dspy\adapters\types\tool.py", line 176, in __call__
result = self.func(**parsed_kwargs)
File "src\retrieval\tool.py", line 169, in retrieve
result = run_request(kwargs)
~~~~~~~~~~~^^^^^^^^
ValueError: Invalid request
```
## After
```
Execution error in retrieve:
Traceback (most recent call last):
File ".venv\Lib\site-packages\dspy\predict\react.py", line 111, in forward
trajectory[f"observation_{idx}"] = self.tools[pred.next_tool_name](**pred.next_tool_args)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^
File ".venv\Lib\site-packages\dspy\utils\callback.py", line 326, in sync_wrapper
return fn(instance, *args, **kwargs)
File ".venv\Lib\site-packages\dspy\adapters\types\tool.py", line 176, in __call__
result = self.func(**parsed_kwargs)
File "src\retrieval\tool.py", line 169, in retrieve
result = run_request(kwargs)
~~~~~~~~~~~^^^^^^^^
ValueError: Invalid request
```1 parent 3183260 commit d42f29e
1 file changed
+4
-1
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
166 | 166 | | |
167 | 167 | | |
168 | 168 | | |
169 | | - | |
| 169 | + | |
| 170 | + | |
170 | 171 | | |
| 172 | + | |
| 173 | + | |
171 | 174 | | |
172 | 175 | | |
173 | 176 | | |
| |||
0 commit comments