Skip to content

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

Bridge BrowserProfile launch env

Magnus Müller8bbebb5a4940
diff --git a/RUST_AGENT_INTEGRATION_FEATURES.md b/RUST_AGENT_INTEGRATION_FEATURES.md
index bd8303859..1f6d855e0 100644
--- a/RUST_AGENT_INTEGRATION_FEATURES.md
+++ b/RUST_AGENT_INTEGRATION_FEATURES.md
@@ -135,10 +135,15 @@ Terminal core branch: `magnus/browser-use-rust-integration` at latest pulled mai
- The terminal core already tries `CHROME_PATH` first when launching managed Chromium, so custom Browser Use browser binaries are preserved without changing the Rust launcher.
- Proof: `test_rust_agent_translates_browser_profile_executable_path`.
+26. BrowserProfile managed launch environment bridge
+ - `BrowserProfile.env` scalar values are serialized into the terminal subprocess environment for managed Chromium runs.
+ - Values are stringified for process-env compatibility, unsupported nested values are ignored, and cloud/CDP runs do not receive managed browser launch env overrides.
+ - Proof: `test_rust_agent_translates_browser_profile_env`.
+
## 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` (35 tests)
+- `uv run pytest -q tests/ci/test_rust_agent.py` (36 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 7f99bb571..3783e726c 100644
--- a/browser_use/rust/service.py
+++ b/browser_use/rust/service.py
@@ -185,6 +185,28 @@ def _managed_browser_executable_path(browser_session: BrowserSession | None, bro
return None
+def _env_value_to_str(value: Any) -> str | None:
+ if isinstance(value, bool):
+ return 'true' if value else 'false'
+ if isinstance(value, (str, int, float, os.PathLike)):
+ return str(value)
+ return None
+
+
+def _managed_browser_env(browser_session: BrowserSession | None, browser_profile: BrowserProfile | None) -> dict[str, str]:
+ env: dict[str, str] = {}
+ session_profile = getattr(browser_session, 'browser_profile', None)
+ for profile in (session_profile, browser_profile, browser_session):
+ raw_env = getattr(profile, 'env', None)
+ if not isinstance(raw_env, dict):
+ continue
+ for key, value in raw_env.items():
+ env_value = _env_value_to_str(value)
+ if isinstance(key, str) and key and env_value is not None:
+ env[key] = env_value
+ return env
+
+
def _is_managed_browser_mode(mode: str) -> bool:
normalized = mode.strip().lower().replace('_', '-').replace(' ', '-')
return normalized in {'managed-headless', 'headless', 'headless-chromium', 'managed-headed', 'managed', 'headed'}
@@ -714,6 +736,7 @@ class Agent(Generic[AgentStructuredOutput]):
self.managed_browser_args = _managed_browser_launch_args(self.browser_session, self.browser_profile)
self.managed_browser_profile_dir = _managed_browser_profile_dir(self.browser_session, self.browser_profile)
self.managed_browser_executable_path = _managed_browser_executable_path(self.browser_session, self.browser_profile)
+ self.managed_browser_env = _managed_browser_env(self.browser_session, self.browser_profile)
self.sensitive_data_context = _sensitive_data_context(sensitive_data)
self.display_files_in_done_text = display_files_in_done_text
self.file_system_path = file_system_path
@@ -1159,6 +1182,8 @@ class Agent(Generic[AgentStructuredOutput]):
cdp_url = _extract_cdp_url(self.browser_session) or _extract_profile_cdp_url(self.browser_profile)
if cdp_url:
env['BU_CDP_URL'] = cdp_url
+ if self.managed_browser_env and _is_managed_browser_mode(browser_mode):
+ env.update(self.managed_browser_env)
if self.managed_browser_args and _is_managed_browser_mode(browser_mode):
env['BU_MANAGED_BROWSER_ARGS'] = json.dumps(self.managed_browser_args)
if self.managed_browser_profile_dir and _is_managed_browser_mode(browser_mode):
diff --git a/tests/ci/test_rust_agent.py b/tests/ci/test_rust_agent.py
index 8bdde5aad..7efd2985e 100644
--- a/tests/ci/test_rust_agent.py
+++ b/tests/ci/test_rust_agent.py
@@ -318,6 +318,46 @@ def test_rust_agent_translates_browser_profile_executable_path(monkeypatch, tmp_
assert cloud_env['CHROME_PATH'] == '/usr/bin/existing-chrome'
+def test_rust_agent_translates_browser_profile_env(monkeypatch):
+ from browser_use.rust import Agent
+
+ class BrowserProfile:
+ headless = True
+ env = {
+ 'BU_BROWSER_FLAG': 'enabled',
+ 'BU_BROWSER_BOOL': True,
+ 'BU_BROWSER_RETRIES': 3,
+ 'IGNORED_NESTED': {'nested': 'value'},
+ }
+
+ monkeypatch.setenv('BROWSER_USE_TERMINAL_BINARY', '/tmp/browser-use-terminal')
+ monkeypatch.setenv('BU_BROWSER_FLAG', 'outer')
+ monkeypatch.delenv('BROWSER_USE_RUST_BROWSER_MODE', raising=False)
+ monkeypatch.delenv('BROWSER_USE_BROWSER_MODE', raising=False)
+
+ agent = Agent(task='report title', browser_profile=BrowserProfile())
+ env = agent._run_env()
+
+ assert env['LLM_BROWSER_BROWSER_MODE'] == 'managed-headless'
+ assert agent.managed_browser_env == {
+ 'BU_BROWSER_FLAG': 'enabled',
+ 'BU_BROWSER_BOOL': 'true',
+ 'BU_BROWSER_RETRIES': '3',
+ }
+ assert env['BU_BROWSER_FLAG'] == 'enabled'
+ assert env['BU_BROWSER_BOOL'] == 'true'
+ assert env['BU_BROWSER_RETRIES'] == '3'
+ assert 'IGNORED_NESTED' not in env
+
+ monkeypatch.setenv('BROWSER_USE_BROWSER_MODE', 'cloud')
+ cloud_agent = Agent(task='report title', browser_profile=BrowserProfile())
+ cloud_env = cloud_agent._run_env()
+
+ assert cloud_env['LLM_BROWSER_BROWSER_MODE'] == 'cloud'
+ assert cloud_env['BU_BROWSER_FLAG'] == 'outer'
+ assert 'BU_BROWSER_BOOL' not in cloud_env
+
+
def test_rust_agent_adds_browser_profile_domain_constraints():
from browser_use.rust import Agent