Skip to content

Open-source Python library for AI browser agents, plus Browser Use Cloud: stealth browsers and a hosted agent API.

address review feedback: preserve real cause via raise_if_any, fix pyright

Faseehc4e314072efd
diff --git a/browser_use/tools/service.py b/browser_use/tools/service.py
index 915690ff1..9898c7ccf 100644
--- a/browser_use/tools/service.py
+++ b/browser_use/tools/service.py
@@ -1013,7 +1013,9 @@ class Tools(Generic[Context]):
event = browser_session.event_bus.dispatch(SwitchTabEvent(target_id=target_id))
await event
- new_target_id = await event.event_result(raise_if_any=False, raise_if_none=False) # Don't raise on errors
+ # raise_if_any=True so a handler failure surfaces its real cause here instead
+ # of silently becoming a "produced no result" below.
+ new_target_id = await event.event_result(raise_if_any=True, raise_if_none=False)
except Exception as e:
logger.warning(f'Tab switch failed: {e}')
# Preserve the concrete cause (e.g. a stale tab_id) instead of a generic
@@ -1022,8 +1024,8 @@ class Tools(Generic[Context]):
raise BrowserError(memory, short_term_memory=memory, long_term_memory=memory)
# on_SwitchTabEvent returns the newly focused TargetID on every success path, so a
- # missing result means the handler failed rather than having quietly succeeded
- # (raise_if_any=False above only suppresses the exception, it doesn't mean success).
+ # missing result (with no exception raised) means the handler still failed rather
+ # than having quietly succeeded.
if not new_target_id:
memory = f'Failed to switch to tab #{params.tab_id}: tab switch produced no result'
logger.warning(memory)
diff --git a/tests/ci/browser/test_tabs.py b/tests/ci/browser/test_tabs.py
index e08633f6b..a38124248 100644
--- a/tests/ci/browser/test_tabs.py
+++ b/tests/ci/browser/test_tabs.py
@@ -19,11 +19,13 @@ Usage:
import asyncio
import time
+from typing import Any
import pytest
from pytest_httpserver import HTTPServer
from browser_use.agent.service import Agent
+from browser_use.agent.views import ActionModel
from browser_use.browser import BrowserSession
from browser_use.browser.profile import BrowserProfile
from browser_use.tools.service import Tools
@@ -672,6 +674,18 @@ class TestMultiTabOperations:
pytest.fail('Test timed out after 2 minutes - agent hung during multiple tab operations')
+class _TabActionModel(ActionModel):
+ """ActionModel with explicit slots for the tab actions driven directly via tools.act().
+
+ registry.create_action_model() builds its fields at runtime, so a statically declared
+ subclass is what keeps pyright able to check these call sites. act() dispatches on the
+ key returned by model_dump(exclude_unset=True), so the real registered actions still run.
+ """
+
+ switch: dict[str, Any] | None = None
+ navigate: dict[str, Any] | None = None
+
+
class TestSwitchTabFailureReporting:
"""A failed `switch` must surface as ActionResult.error rather than a fake success.
@@ -683,14 +697,13 @@ class TestSwitchTabFailureReporting:
async def test_switch_to_nonexistent_tab_reports_error(self, browser_session):
tools = Tools()
- ActionModel = tools.registry.create_action_model()
tabs_before = await browser_session.get_tabs()
live_ids = {tab.target_id[-4:] for tab in tabs_before}
bogus_tab_id = 'zzzz'
assert bogus_tab_id not in live_ids
- result = await tools.act(ActionModel(switch={'tab_id': bogus_tab_id}), browser_session=browser_session)
+ result = await tools.act(_TabActionModel(switch={'tab_id': bogus_tab_id}), browser_session=browser_session)
assert result.error is not None, 'a failed tab switch must set ActionResult.error'
assert bogus_tab_id in result.error
@@ -702,17 +715,16 @@ class TestSwitchTabFailureReporting:
async def test_switch_to_open_tab_still_succeeds(self, browser_session, base_url):
tools = Tools()
- ActionModel = tools.registry.create_action_model()
original_tab_id = (await browser_session.get_tabs())[0].target_id[-4:]
open_result = await tools.act(
- ActionModel(navigate={'url': f'{base_url}/page1', 'new_tab': True}),
+ _TabActionModel(navigate={'url': f'{base_url}/page1', 'new_tab': True}),
browser_session=browser_session,
)
assert open_result.error is None, f'opening a new tab should not error: {open_result.error}'
- result = await tools.act(ActionModel(switch={'tab_id': original_tab_id}), browser_session=browser_session)
+ result = await tools.act(_TabActionModel(switch={'tab_id': original_tab_id}), browser_session=browser_session)
assert result.error is None, f'switching to a live tab must not error: {result.error}'
assert result.long_term_memory is not None