#132 added the .act() agent interaction method to the async API, but kept it limited to synchronous callbacks for tool implementations.
For the async API, it's clearly desirable to also accept coroutines as tool implementations, awaiting them in the current event loop instead of dispatching them to a subthread for execution. Running the coroutines in a different thread when using the async native API isn't desirable, as that would be introduce the kinds of thread synchronisation issues that async programming aims to avoid (if a callback needs to make blocking calls, it can be passed in as a regular synchronous callback, as those are moved out of the main event loop via asyncio.to_thread).
It would be possible to also support async callbacks in the synchronous API, with two potential implementations:
- the
.act() method executes async tools with asyncio.run, using the synchronous thread pool executor as normal.
- the
.act() method starts a single background thread with a shared event loop for async tool execution
The former approach is easy for SDK clients to implement in the tool implementations, which suggests that if async callback support is added to the synchronous API, it should use the latter approach.
However, for now, we're going to take the simplest option, which is to raise an exception if an asynchronous tool definition is passed to the synchronous API.
#132 added the
.act()agent interaction method to the async API, but kept it limited to synchronous callbacks for tool implementations.For the async API, it's clearly desirable to also accept coroutines as tool implementations, awaiting them in the current event loop instead of dispatching them to a subthread for execution. Running the coroutines in a different thread when using the async native API isn't desirable, as that would be introduce the kinds of thread synchronisation issues that async programming aims to avoid (if a callback needs to make blocking calls, it can be passed in as a regular synchronous callback, as those are moved out of the main event loop via
asyncio.to_thread).It would be possible to also support async callbacks in the synchronous API, with two potential implementations:
.act()method executes async tools withasyncio.run, using the synchronous thread pool executor as normal..act()method starts a single background thread with a shared event loop for async tool executionThe former approach is easy for SDK clients to implement in the tool implementations, which suggests that if async callback support is added to the synchronous API, it should use the latter approach.
However, for now, we're going to take the simplest option, which is to raise an exception if an asynchronous tool definition is passed to the synchronous API.