sdk-python icon indicating copy to clipboard operation
sdk-python copied to clipboard

[FEATURE] Support for async Tool

Open tan-z-tan opened this issue 1 year ago • 8 comments

Problem Statement

Hi team,

I’m looking to create tools for LLM calls and search systems, but these functions are implemented as async functions in my codebase.

Does StrandsAgents support defining tools as async functions? If not, I would like to request support for async tool definitions to better integrate with asynchronous workflows.

Proposed Solution

No response

Use Case

Many modern applications use asynchronous operations for network-bound tasks like API calls or database queries. For example: • Calling an external LLM API such as OpenAI or Claude, which requires awaiting the HTTP response • Performing a vector search against an async-compatible database or search engine (e.g., Redis, Qdrant, or OpenSearch via async clients) • Fetching contextual information from other services in parallel

Allowing tools to be defined as async def would enable developers to: • Seamlessly integrate StrandsAgents into async applications without blocking the event loop • Compose multiple async tools in parallel using asyncio.gather • Improve responsiveness and scalability in production environments

Alternatives Solutions

No response

Additional Context

No response

tan-z-tan avatar May 26 '25 07:05 tan-z-tan

+1

kandakji avatar May 26 '25 12:05 kandakji

Thank you for opening this feature request @tan-z-tan.

This is something that the team is actively thinking about. https://github.com/strands-agents/sdk-python/issues/83 is a little related as well so tagging that here.

arronbailiss avatar May 26 '25 17:05 arronbailiss

+1

ayoubak141 avatar May 27 '25 17:05 ayoubak141

Notes on Async support for Tools in Strands

A brief write up on the unsystematic hacking/patching to support async support for tool invocation in the Strands Agents Python SDK. It covers the incremental patches applied, the challenges encountered, the final outcome. Also, outlined is a comparison against the goals in EPIC #129 (“Implement native async iterator support”).

My original motiviation was to support async and parallel invocation of agents for processing RSS feeds. I had created a RSS Feed Parser using the @tool decorator. Without paying attention, I had started building the RSS Feed tool (using the @tool decorator) as an async script, and it failed. That piqued my curiousity to see if I can patch or hack to make it work. I had done something similar but much smaller hack when I had found that the A2A Client Python code was not using true async libraries, so that A2A client would block the event loop.

Hacking | Patching Journey & Key Milestones

Each of the steps in this journey happened incrementally. That is, the change was not systematic or done holistically. Changes were made one step or file at a time as bugs appeared during execution with generous help from Q!

  1. decorator.py
  • Modified src/strands/tools/decorator.py so that:
  • If a user function is an async def, the generated wrapper is also async def and awaits the underlying coroutine.
    • Detect using inspect.iscoroutinefunction(f) on the user’s function.
    • Emit an async def wrapper when the underlying function is asynchronous, invoking await f(...).
    • Leave sync functions untouched, so existing tools continue to work. (however the sync/normal version of the script does not work. I still have to debug and figure out why it fails.)
  1. event_loop.py
  • Converted core routines in src/strands/event_loop/event_loop.py to async def:
    • event_loop_cycle
    • _handle_tool_execution
    • recurse_event_loop
  • Replace all recursive “return foo(...)” calls with return await foo(...).
  • Wrap the tool handler via a small async helper so that it can await decorator-generated coroutines.
  1. executor.py
  • Transformed run_tools in src/strands/tools/executor.py into an async def function.
  • Within both sequential and (when enabled) parallel paths, inspect each handler return for coroutines and await them.
  • Sequential path loops over tool_uses, await handler(tool_use).
  • Parallel path is commented out so that the processing flow goes through the sequential logic. Before commenting out the async execution would fail. I decided not to patch or debug the parallel handler and make it async. No debug attempted (or an attempt to understand) on why control was going to parallel executor.
  1. agent.py
  • In src/strands/agent/agent.py, both the public __call__ and internal _run_loop/_execute_event_loop_cycle methods became async def.
  1. streaming.py
  • No changes. Only used for instrumenting with logs to understand flow and debug. Bugs were manifesting as if Bedrock Streaming was returning results with no content (tied to the parallel path execution.)
  1. Testing
  • Instrumented debug prints around streaming and tool-use phases to confirm that all coroutines were awaited and that messages contained the expected content.
  • Verified end-to-end with newrss.py, confirming that an async def RSS-parsing tool executed without errors and produced the correct output.
  • The sync/normal version of the script still fails, so if tools created with decorator do not have async & await - it fails.

Comparison to EPIC #129

EPIC Item Status Notes
1. Async iterator event loop ◐ The loop is now async/await, but still recursive rather than an async for interface.
2. Async tool invocation ✅ @tool supports async def, and executor awaits handler coroutines.
3. Async model streams ⬜ Providers remain synchronous generators; full async streams remain future work.
4. Cancellation tokens ⬜ No change.
5. Async MCP servers ⬜ No change.
6. Async‐aware AgentResult ◐ AgentResult can be awaited in async contexts; streaming helpers could be improved.
7. Bidirectional streaming ⬜
8. Sync wrappers for backward compatibility ◐ Attempted to keep backward compatibility preserved via sync‐wrapper or optional helper functions. However normal synchronous calls not working in testing.
9. Contextvar propagation in tests ⬜ No formal testing, no updates to testing.

AshuJoshi avatar Jun 13 '25 17:06 AshuJoshi

Async tools are now supported and documented at https://strandsagents.com/latest/documentation/docs/user-guide/concepts/tools/python-tools/#async-invocation

zastrowm avatar Jul 21 '25 15:07 zastrowm

Nice! @zastrowm would this enable to stream sub-agent's events for agents-as-tools pattern? I found that only supervisor's tokens are surfaced when calling agent.stream_async()

iker592 avatar Jul 25 '25 12:07 iker592

Nice! @zastrowm would this enable to stream sub-agent's events for agents-as-tools pattern?

Not yet. The refactor to enable async tools provided the ability to enable tool result streaming but we haven't yet implemented it - We think tool result streaming will be the first implementation of #242.

CC @pgrayy

zastrowm avatar Jul 25 '25 14:07 zastrowm

Asynchronous tools are now supported. For details on defining an asynchronous decorated tool, please see our docs (note, MCP tools are all called asynchronously and so no extra effort is required on that front).

pgrayy avatar Aug 14 '25 13:08 pgrayy