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 generic subscription parity

Magnus Müllerbfb7d83479b4
diff --git a/RUST_AGENT_INTEGRATION_FEATURES.md b/RUST_AGENT_INTEGRATION_FEATURES.md
index 3b892c7b5..e4b45da73 100644
--- a/RUST_AGENT_INTEGRATION_FEATURES.md
+++ b/RUST_AGENT_INTEGRATION_FEATURES.md
@@ -551,10 +551,15 @@ Terminal core branch: `magnus/browser-use-rust-integration` at terminal main `ee
- Ordinary vision-capable model families still preserve caller-provided `use_vision=True`.
- Proof: `test_rust_agent_disables_vision_for_unsupported_model_families`.
+109. Rust Agent generic subscription parity
+ - The Rust-backed `Agent` now mirrors Browser Use's two-parameter generic runtime surface for `Agent[Context, StructuredOutput]` annotations.
+ - The redirected `browser_use.agent.service.Agent` import also supports the same two-argument generic subscription, while one-argument subscriptions raise the same too-few-arguments error shape as Browser Use.
+ - Proof: `test_rust_agent_generic_subscription_matches_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` (82 tests)
+- `uv run pytest -q tests/ci/test_rust_agent.py` (83 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 5dfd5768a..4dbfab597 100644
--- a/browser_use/rust/service.py
+++ b/browser_use/rust/service.py
@@ -48,6 +48,7 @@ from browser_use.tools.service import Tools
from browser_use.utils import URL_PATTERN, check_latest_browser_use_version, get_browser_use_version
+Context = TypeVar('Context')
AgentStructuredOutput = TypeVar('AgentStructuredOutput', bound=BaseModel)
AgentHookFunc = Callable[[Any], Awaitable[None] | None]
AgentDoneCallback = Callable[[AgentHistoryList], Awaitable[None] | None]
@@ -1056,7 +1057,7 @@ def _normalize_initial_action(action_name: str, params: Any) -> tuple[str, Any]:
return action_name, params
-class Agent(Generic[AgentStructuredOutput]):
+class Agent(Generic[Context, AgentStructuredOutput]):
"""Browser Use-style Agent backed by the Rust browser-use-terminal core."""
def __init__(
diff --git a/tests/ci/test_rust_agent.py b/tests/ci/test_rust_agent.py
index 245c3b609..7f2e786ee 100644
--- a/tests/ci/test_rust_agent.py
+++ b/tests/ci/test_rust_agent.py
@@ -5,6 +5,7 @@ import importlib.util
import json
import sys
from pathlib import Path
+from typing import get_args, get_origin
import pytest
from pydantic import BaseModel
@@ -41,6 +42,27 @@ def test_agent_service_export_uses_rust_wrapper():
assert ServiceAgent is RustAgent
+def test_rust_agent_generic_subscription_matches_browser_use():
+ from browser_use.agent.service import Agent as ServiceAgent
+ from browser_use.agent.service import _PythonAgent as BrowserUseAgent
+ from browser_use.rust import Agent as RustAgent
+
+ class Answer(BaseModel):
+ answer: str
+
+ browser_use_alias = BrowserUseAgent[dict, Answer]
+ rust_alias = RustAgent[dict, Answer]
+ service_alias = ServiceAgent[dict, Answer]
+
+ assert get_args(rust_alias) == get_args(browser_use_alias) == (dict, Answer)
+ assert get_args(service_alias) == (dict, Answer)
+ assert get_origin(rust_alias) is RustAgent
+ assert get_origin(service_alias) is RustAgent
+
+ with pytest.raises(TypeError, match='Too few arguments'):
+ RustAgent[Answer]
+
+
def test_rust_agent_constructor_signature_matches_browser_use_order(tmp_path):
from browser_use.agent.service import _PythonAgent as BrowserUseAgent
from browser_use.rust import Agent as RustAgent