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 LLM helper type hints

Magnus Müller6abcde9fea8d
diff --git a/RUST_AGENT_INTEGRATION_FEATURES.md b/RUST_AGENT_INTEGRATION_FEATURES.md
index 9b3945c61..c270df7e2 100644
--- a/RUST_AGENT_INTEGRATION_FEATURES.md
+++ b/RUST_AGENT_INTEGRATION_FEATURES.md
@@ -581,10 +581,15 @@ Terminal core branch: `magnus/browser-use-rust-integration` at terminal main `ee
- `_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`.
+115. Rust Agent LLM-message helper type-hint parity
+ - The Rust-backed LLM helper methods now expose Browser Use's `BaseMessage` metadata for callers that introspect model-output and URL-shortening helpers.
+ - `get_model_output(...)`, `_get_model_output_with_retry(...)`, `_handle_post_llm_processing(...)`, and `_process_messsages_and_replace_long_urls_shorter_ones(...)` now resolve to Browser Use's message-list annotations instead of loose `Any` metadata.
+ - Proof: `test_rust_agent_llm_message_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` (88 tests)
+- `uv run pytest -q tests/ci/test_rust_agent.py` (89 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 8f0b17984..1cf486729 100644
--- a/browser_use/rust/service.py
+++ b/browser_use/rust/service.py
@@ -42,7 +42,7 @@ from browser_use.browser.views import BrowserStateHistory, BrowserStateSummary,
from browser_use.dom.views import DOMInteractedElement
from browser_use.filesystem.file_system import FileSystem
from browser_use.llm.base import BaseChatModel
-from browser_use.llm.messages import ContentPartImageParam, ContentPartTextParam
+from browser_use.llm.messages import BaseMessage, ContentPartImageParam, ContentPartTextParam
from browser_use.screenshots.service import ScreenshotService
from browser_use.telemetry.service import ProductTelemetry
from browser_use.telemetry.views import AgentTelemetryEvent
@@ -1651,7 +1651,7 @@ class Agent(Generic[Context, AgentStructuredOutput]):
return URL_PATTERN.sub(replace_url, text), replaced_urls
- def _process_messsages_and_replace_long_urls_shorter_ones(self, input_messages: list[Any]) -> dict[str, str]:
+ def _process_messsages_and_replace_long_urls_shorter_ones(self, input_messages: list[BaseMessage]) -> dict[str, str]:
"""Replace long URLs in Browser Use LLM messages in place."""
from browser_use.llm.messages import AssistantMessage, ContentPartTextParam, UserMessage
@@ -1860,7 +1860,7 @@ class Agent(Generic[Context, AgentStructuredOutput]):
await self._force_done_after_failure()
return browser_state_summary
- async def get_model_output(self, input_messages: list[Any]) -> AgentOutput:
+ async def get_model_output(self, input_messages: list[BaseMessage]) -> AgentOutput:
"""Get next Browser Use action output from the configured Python LLM."""
if self.llm is None or not hasattr(self.llm, 'ainvoke'):
raise ValueError('A Browser Use-compatible llm with ainvoke(...) is required for get_model_output().')
@@ -1879,7 +1879,7 @@ class Agent(Generic[Context, AgentStructuredOutput]):
self._log_next_action_summary(parsed)
return parsed
- async def _get_model_output_with_retry(self, input_messages: list[Any]) -> AgentOutput:
+ async def _get_model_output_with_retry(self, input_messages: list[BaseMessage]) -> AgentOutput:
"""Get model output, retrying once when the model returns no usable action."""
model_output = await self.get_model_output(input_messages)
@@ -1906,7 +1906,11 @@ class Agent(Generic[Context, AgentStructuredOutput]):
return model_output
- async def _handle_post_llm_processing(self, browser_state_summary: Any, input_messages: list[Any]) -> None:
+ async def _handle_post_llm_processing(
+ self,
+ browser_state_summary: BrowserStateSummary,
+ input_messages: list[BaseMessage],
+ ) -> None:
"""Handle Browser Use callbacks and conversation saving after an LLM response."""
if self.register_new_step_callback and self.state.last_model_output:
if inspect.iscoroutinefunction(self.register_new_step_callback):
diff --git a/tests/ci/test_rust_agent.py b/tests/ci/test_rust_agent.py
index a20634166..562f8b735 100644
--- a/tests/ci/test_rust_agent.py
+++ b/tests/ci/test_rust_agent.py
@@ -161,6 +161,22 @@ def test_rust_agent_browser_state_helper_type_hints_match_browser_use():
assert rust_hints == browser_use_hints
+def test_rust_agent_llm_message_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 (
+ '_get_model_output_with_retry',
+ '_handle_post_llm_processing',
+ '_process_messsages_and_replace_long_urls_shorter_ones',
+ 'get_model_output',
+ ):
+ 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