Skip to content

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

Match Rust agent browser state helper type hints

Magnus Müllerd2e8ada10207
diff --git a/RUST_AGENT_INTEGRATION_FEATURES.md b/RUST_AGENT_INTEGRATION_FEATURES.md
index 71c720764..9b3945c61 100644
--- a/RUST_AGENT_INTEGRATION_FEATURES.md
+++ b/RUST_AGENT_INTEGRATION_FEATURES.md
@@ -576,10 +576,15 @@ Terminal core branch: `magnus/browser-use-rust-integration` at terminal main `ee
- `_convert_initial_actions(...)`, `_update_action_indices(...)`, and `multi_act(...)` now resolve to Browser Use's `ActionModel`, `DOMInteractedElement`, and `BrowserStateSummary` annotations instead of loose `Any` metadata.
- Proof: `test_rust_agent_action_model_helper_type_hints_match_browser_use`.
+114. Rust Agent browser-state helper type-hint parity
+ - The Rust-backed browser-state helper methods now expose Browser Use's `BrowserStateSummary` metadata for callers that introspect step preparation, logging, action selection, history creation, and finalization.
+ - `_prepare_context(...)`, `_get_next_action(...)`, `_log_step_context(...)`, `_make_history_item(...)`, and `_finalize(...)` now resolve to the same Browser Use browser-state annotations instead of loose `Any` metadata.
+ - Proof: `test_rust_agent_browser_state_helper_type_hints_match_browser_use`.
+
## Current Verification
- `python3 -m py_compile browser_use/agent/service.py browser_use/rust/service.py browser_use/rust/__init__.py browser_use/__init__.py tests/ci/test_rust_agent.py examples/rust_agent/basic.py examples/rust_agent/real_v8_smoke.py`
-- `uv run pytest -q tests/ci/test_rust_agent.py` (87 tests)
+- `uv run pytest -q tests/ci/test_rust_agent.py` (88 tests)
- `cargo build -q -p browser-use-cli`
- `CARGO_INCREMENTAL=0 cargo test -q -p browser-use-agent selected_remote_cdp_mode_allows_remote_cdp_connect -- --nocapture`
- `CARGO_INCREMENTAL=0 cargo test -q -p browser-use-agent bare_browser_connect_resolves_to_selected_managed_mode_with_launch_args -- --nocapture`
diff --git a/browser_use/rust/service.py b/browser_use/rust/service.py
index f34359e62..8f0b17984 100644
--- a/browser_use/rust/service.py
+++ b/browser_use/rust/service.py
@@ -1314,7 +1314,7 @@ class Agent(Generic[Context, AgentStructuredOutput]):
f'Starting a browser-use agent with version {self.version}, with provider={provider} and model={model}'
)
- def _log_step_context(self, browser_state_summary: Any) -> None:
+ def _log_step_context(self, browser_state_summary: BrowserStateSummary) -> None:
"""Log current step and page context."""
url = getattr(browser_state_summary, 'url', '') if browser_state_summary else ''
url_short = url[:50] + '...' if len(url) > 50 else url
@@ -1823,7 +1823,7 @@ class Agent(Generic[Context, AgentStructuredOutput]):
"""Save current Browser Use file system state back onto AgentState."""
self.state.file_system_state = self.file_system.get_state()
- async def _prepare_context(self, step_info: AgentStepInfo | None = None) -> Any:
+ async def _prepare_context(self, step_info: AgentStepInfo | None = None) -> BrowserStateSummary:
"""Prepare Browser Use step context from the configured browser session."""
if self.browser_session is None:
raise AssertionError('BrowserSession is not set up')
@@ -1933,7 +1933,7 @@ class Agent(Generic[Context, AgentStructuredOutput]):
self.settings.save_conversation_path_encoding,
)
- async def _get_next_action(self, browser_state_summary: Any) -> None:
+ async def _get_next_action(self, browser_state_summary: BrowserStateSummary) -> None:
"""Fetch the next model output and run Browser Use post-LLM hooks."""
input_messages = self._message_manager.get_messages()
try:
@@ -1959,7 +1959,7 @@ class Agent(Generic[Context, AgentStructuredOutput]):
async def _make_history_item(
self,
model_output: AgentOutput | None,
- browser_state_summary: Any,
+ browser_state_summary: BrowserStateSummary,
result: list[ActionResult],
metadata: StepMetadata | None = None,
state_message: str | None = None,
@@ -2024,7 +2024,7 @@ class Agent(Generic[Context, AgentStructuredOutput]):
)
self.state.last_result = [ActionResult(error=error_msg)]
- async def _finalize(self, browser_state_summary: Any | None) -> None:
+ async def _finalize(self, browser_state_summary: BrowserStateSummary | None) -> None:
"""Finalize one Browser Use-style step after Rust-backed helper execution."""
step_end_time = time.time()
if not self.state.last_result:
diff --git a/tests/ci/test_rust_agent.py b/tests/ci/test_rust_agent.py
index aca104a29..a20634166 100644
--- a/tests/ci/test_rust_agent.py
+++ b/tests/ci/test_rust_agent.py
@@ -150,6 +150,17 @@ def test_rust_agent_action_model_helper_type_hints_match_browser_use():
assert rust_hints == browser_use_hints
+def test_rust_agent_browser_state_helper_type_hints_match_browser_use():
+ from browser_use.agent.service import _PythonAgent as BrowserUseAgent
+ from browser_use.rust import Agent as RustAgent
+
+ for method_name in ('_finalize', '_get_next_action', '_log_step_context', '_make_history_item', '_prepare_context'):
+ browser_use_hints = get_type_hints(getattr(BrowserUseAgent, method_name))
+ rust_hints = get_type_hints(getattr(RustAgent, method_name))
+
+ assert rust_hints == browser_use_hints
+
+
def test_rust_events_reconstruct_browser_use_history():
from browser_use.rust.service import _history_from_events