Skip to content

[BUG] Browser tool fails when used with stream_async #453

@Hweinstock

Description

@Hweinstock

Checks

  • I have updated to the lastest minor and patch version of Strands
  • I have checked the documentation and this is not expected behavior
  • I have searched ./issues and there are no duplicates of my issue

Strands Version

1.35.0

Tools Package Version

0.4.0

Tools used

AgentCoreBrowser

Python Version

3.10

Operating System

Amazon Linux 2

Installation Method

pip

Steps to Reproduce

  1. Create an AgentCoreBrowser instance (this sets the event loop on the current thread)
  2. Pass browser_instance.browser as a tool to a strands Agent
  3. Call agent.stream_async() from within an already-running async event loop (e.g. uvicorn, asyncio.run(main()))
  4. Send a message that causes the model to invoke the browser tool

The tool call fails with RuntimeError: There is no current event loop in thread 'asyncio_0'.

This does not reproduce with the sync agent() call — only stream_async() / invoke_async().

Expected Behavior

tool executes successfully.

Actual Behavior

blocking error.

Additional Context

My understanding of why this is happening:

Browser.init creates an event loop and registers it on the current thread.

_execute_async is defined as a synchronous method.

Therefore calling _execute_async causes strands to dispatch it to another thread based on the logic in _stream.

However, that thread is different from the one where the browser was initiated, meaning its missing the event loop leading to the error.

Possible Solution

Add asyncio.set_event_loop(self._loop) in Browser._execute_async before nest_asyncio.apply(), so the
browser's loop is registered on whatever thread runs the tool.

Related Issues

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions