Python: run sync tools off the event loop#5773
Open
he-yufeng wants to merge 1 commit into
Open
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR updates the Python FunctionTool.invoke async execution path so that synchronous tools run in a worker thread (avoiding event-loop stalls), while async tools continue to run on the event loop. It also adds a regression test to ensure a blocking sync tool no longer prevents other coroutines from running (fixing #5741).
Changes:
- Add an internal async helper to route async tools to the event loop and sync tools to
asyncio.to_thread(). - Update
FunctionTool.invoketo use the helper in both the observability-enabled and disabled code paths. - Add an asyncio/threading regression test to confirm sync tool invocation does not block the event loop.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
python/packages/core/agent_framework/_tools.py |
Offloads synchronous tool execution to a worker thread during async invocation via a new helper. |
python/packages/core/tests/core/test_tools.py |
Adds a regression test validating that sync tools no longer block other event-loop tasks. |
Comment on lines
+540
to
+547
| async def _invoke_function(self, call_kwargs: Mapping[str, Any]) -> Any: | ||
| """Run sync tools off the event loop during async invocation.""" | ||
| func = self.func.func if isinstance(self.func, FunctionTool) else self.func | ||
| if inspect.iscoroutinefunction(func): | ||
| return await self.__call__(**call_kwargs) | ||
|
|
||
| res = await asyncio.to_thread(self.__call__, **call_kwargs) | ||
| return await res if inspect.isawaitable(res) else res |
eavanvalkenburg
approved these changes
May 18, 2026
Contributor
Python Test Coverage Report •
Python Unit Test Overview
|
||||||||||||||||||||||||||||||
2d983ec to
37330b7
Compare
Contributor
Author
|
Rebased this branch onto current Validation: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
FunctionTool.invokepathFixes #5741.
To verify
uv run pytest tests/core/test_tools.py -quv run ruff check agent_framework tests/core/test_tools.pyuv run ruff format --check agent_framework tests/core/test_tools.pyuv run pyrightuv run mypy --config-file pyproject.toml agent_frameworkpython -m py_compile agent_framework\_tools.py tests\core\test_tools.py