Skip to content

Accepting asynchronous tool definitions in the .act() APIs #133

@ncoghlan

Description

@ncoghlan

#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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions