Skip to content

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

Initialize Rust agent tools models

Magnus Müller4b4c31e8a495
diff --git a/RUST_AGENT_INTEGRATION_FEATURES.md b/RUST_AGENT_INTEGRATION_FEATURES.md
index 34f2a01e8..a2aad51c6 100644
--- a/RUST_AGENT_INTEGRATION_FEATURES.md
+++ b/RUST_AGENT_INTEGRATION_FEATURES.md
@@ -406,10 +406,15 @@ Terminal core branch: `magnus/browser-use-rust-integration` at terminal main `f0
- The wrapper also exposes `save_file_system_state()` so callers can persist the Browser Use `FileSystem` state back onto the agent state.
- Proof: `test_rust_agent_tracks_downloaded_files_and_saves_file_system_state`.
+80. Rust Agent tools/action-model parity
+ - The Rust-backed `Agent` now initializes Browser Use default `Tools` when callers do not provide tools/controller, including `use_vision=False` screenshot exclusion and structured-output `done` action registration.
+ - The wrapper now exposes Browser Use-style `ActionModel`, `DoneActionModel`, and `AgentOutput` classes from the configured tools registry so custom callers can inspect and build action models against the same interface.
+ - Proof: `test_rust_agent_initializes_tools_and_action_models`.
+
## Current Verification
- `python3 -m py_compile 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` (54 tests)
+- `uv run pytest -q tests/ci/test_rust_agent.py` (55 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 5cce7d316..b79e51755 100644
--- a/browser_use/rust/service.py
+++ b/browser_use/rust/service.py
@@ -17,12 +17,22 @@ from pydantic import BaseModel, ValidationError
from typing_extensions import TypeVar
from uuid_extensions import uuid7str
-from browser_use.agent.views import ActionResult, AgentHistory, AgentHistoryList, AgentSettings, AgentState, AgentStepInfo, StepMetadata
+from browser_use.agent.views import (
+ ActionResult,
+ AgentHistory,
+ AgentHistoryList,
+ AgentOutput,
+ AgentSettings,
+ AgentState,
+ AgentStepInfo,
+ StepMetadata,
+)
from browser_use.browser import BrowserProfile, BrowserSession
from browser_use.browser.profile import CHROME_DETERMINISTIC_RENDERING_ARGS, CHROME_DISABLE_SECURITY_ARGS, CHROME_DOCKER_ARGS
from browser_use.browser.views import BrowserStateHistory, TabInfo
from browser_use.filesystem.file_system import FileSystem
from browser_use.tokens.views import ModelUsageStats, UsageSummary
+from browser_use.tools.service import Tools
AgentStructuredOutput = TypeVar('AgentStructuredOutput', bound=BaseModel)
@@ -907,6 +917,22 @@ def _init_file_system(
return file_system, str(agent_directory)
+def _resolve_tools(
+ tools: Any | None,
+ controller: Any | None,
+ output_model_schema: type[BaseModel] | None,
+ use_vision: bool | Literal['auto'],
+ display_files_in_done_text: bool,
+) -> Any:
+ resolved_tools = controller or tools
+ if resolved_tools is None:
+ exclude_actions = ['screenshot'] if use_vision is False else []
+ resolved_tools = Tools(exclude_actions=exclude_actions, display_files_in_done_text=display_files_in_done_text)
+ if output_model_schema is not None and hasattr(resolved_tools, 'use_structured_output_action'):
+ resolved_tools.use_structured_output_action(output_model_schema)
+ return resolved_tools
+
+
def _action_payload(action: Any) -> dict[str, Any]:
"""Serialize a Browser Use action model into JSON-like data for Rust task context."""
if isinstance(action, dict):
@@ -1020,7 +1046,13 @@ class Agent(Generic[AgentStructuredOutput]):
browser_session,
browser,
)
- self.tools = controller or tools
+ self.tools = _resolve_tools(
+ tools,
+ controller,
+ output_model_schema,
+ use_vision,
+ display_files_in_done_text,
+ )
self.sensitive_data = sensitive_data
self.register_new_step_callback = register_new_step_callback
self.register_done_callback = register_done_callback
@@ -1055,6 +1087,7 @@ class Agent(Generic[AgentStructuredOutput]):
step_timeout=step_timeout,
final_response_after_failure=final_response_after_failure,
)
+ self._setup_action_models()
self.available_file_paths = available_file_paths or []
self.allowed_domains = _extract_profile_domains(self.browser_session, self.browser_profile, 'allowed_domains')
self.prohibited_domains = _extract_profile_domains(self.browser_session, self.browser_profile, 'prohibited_domains')
@@ -1223,6 +1256,24 @@ class Agent(Generic[AgentStructuredOutput]):
def usage(self) -> UsageSummary | None:
return self.history.usage
+ def _setup_action_models(self, page_url: str | None = None) -> None:
+ """Expose Browser Use-style action model classes from the configured tools."""
+ registry = getattr(self.tools, 'registry', None)
+ create_action_model = getattr(registry, 'create_action_model', None)
+ if not callable(create_action_model):
+ self.ActionModel = None
+ self.DoneActionModel = None
+ self.AgentOutput = AgentOutput
+ return
+ self.ActionModel = create_action_model(page_url=page_url)
+ if self.settings.flash_mode:
+ self.AgentOutput = AgentOutput.type_with_custom_actions_flash_mode(self.ActionModel)
+ elif self.settings.use_thinking:
+ self.AgentOutput = AgentOutput.type_with_custom_actions(self.ActionModel)
+ else:
+ self.AgentOutput = AgentOutput.type_with_custom_actions_no_thinking(self.ActionModel)
+ self.DoneActionModel = create_action_model(include_actions=['done'], page_url=page_url)
+
async def _check_and_update_downloads(self, context: str = '') -> None:
"""Mirror Browser Use's downloaded-file tracking for supplied sessions."""
if not self.has_downloads_path or self.browser_session is None:
diff --git a/tests/ci/test_rust_agent.py b/tests/ci/test_rust_agent.py
index 6917ef80d..acb85d94d 100644
--- a/tests/ci/test_rust_agent.py
+++ b/tests/ci/test_rust_agent.py
@@ -768,6 +768,31 @@ def test_rust_agent_exposes_browser_use_settings():
assert agent.include_recent_events is True
+def test_rust_agent_initializes_tools_and_action_models():
+ from browser_use.rust import Agent
+ from browser_use.tools.service import Tools
+
+ class Answer(BaseModel):
+ answer: str
+
+ agent = Agent(
+ task='Return structured answer.',
+ output_model_schema=Answer,
+ use_vision=False,
+ )
+
+ action_names = set(agent.tools.registry.registry.actions)
+ done_schema = agent.DoneActionModel.model_json_schema()
+
+ assert isinstance(agent.tools, Tools)
+ assert 'done' in action_names
+ assert 'screenshot' not in action_names
+ assert agent.ActionModel is not None
+ assert agent.DoneActionModel is not None
+ assert agent.AgentOutput is not None
+ assert 'done' in str(done_schema)
+
+
def test_rust_agent_initializes_browser_use_session_and_file_system(tmp_path):
from browser_use.browser import BrowserProfile, BrowserSession
from browser_use.rust import Agent