feat(tools): add transparent async tool support (fixes #334)#2038
Open
giulio-leone wants to merge 2 commits intohuggingface:mainfrom
Open
feat(tools): add transparent async tool support (fixes #334)#2038giulio-leone wants to merge 2 commits intohuggingface:mainfrom
giulio-leone wants to merge 2 commits intohuggingface:mainfrom
Conversation
When a Tool's forward() method is a coroutine, the result is now automatically awaited via asyncio.run() (or a thread pool if already inside a running event loop). This allows users to define async tools without any extra boilerplate. The @tool decorator also now correctly parses async function definitions. Refs: huggingface#334
Contributor
There was a problem hiding this comment.
Pull request overview
Adds first-class support for async tool implementations so tools with an async forward() (or @tool-decorated async functions) execute transparently and return concrete results instead of coroutine objects.
Changes:
- Added
_run_async()helper and awaitable handling inTool.__call__(). - Updated
@tooldecorator AST parsing to detectAsyncFunctionDef. - Added a dedicated test suite for async tool execution behavior.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
src/smolagents/tools.py |
Implements awaitable detection/execution in Tool.__call__() and extends @tool AST parsing to include async function definitions. |
tests/test_async_tools.py |
Adds new tests validating async tools work via subclassing and the @tool decorator in both sync and “inside event loop” call sites. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
…neration - Wrap non-coroutine awaitables before passing to asyncio.run() - Add docstring explaining thread-blocking design in _run_async() - Generate 'async def forward' for async functions in @tool decorator - Add serialization round-trip tests for async tools Refs: huggingface#2038
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.
Problem
smolagents does not support async tools — defining an async
forward()method returns a coroutine object instead of the actual result. This is a significant limitation for tools that need async I/O (database queries, API calls, file operations).Related: #145 (61 reactions), #334 (8 reactions)
Solution
Add transparent async detection and execution in the
Tool.__call__()method:self.forward(*args, **kwargs), check if the result is awaitable viainspect.isawaitable()asyncio.run()to execute itThe
@tooldecorator's AST parsing also now handlesAsyncFunctionDefnodes, so@tool-decorated async functions work correctly.This is fully backward compatible — sync tools work exactly as before. Async tools now 'just work' without any configuration.
Changes
src/smolagents/tools.py: Added_run_async()helper, async handling inTool.__call__(),AsyncFunctionDefsupport in@tooldecoratortests/test_async_tools.py: 10 new tests covering subclass tools, decorated tools, event loop contexts, and various return typesTesting
pythonbinary)Fixes #334
Related: #145