Skip to content

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

perf: skip profile validation HTTP call on cloud connect

ShawnPana1deb430f8fa3
diff --git a/browser_use/skill_cli/browser.py b/browser_use/skill_cli/browser.py
index 938244932..b8c1a472b 100644
--- a/browser_use/skill_cli/browser.py
+++ b/browser_use/skill_cli/browser.py
@@ -135,7 +135,20 @@ class CLIBrowserSession(BrowserSession):
from browser_use.skill_cli.config import get_config_value
cloud_params.enable_recording = bool(get_config_value('cloud_connect_recording'))
- cloud_response = await self._cloud_browser_client.create_browser(cloud_params)
+
+ try:
+ cloud_response = await self._cloud_browser_client.create_browser(cloud_params)
+ except Exception as e:
+ # If profile is invalid, create a new one and retry once
+ if 'profile' in str(e).lower() or '422' in str(e):
+ logger.info('Cloud profile invalid, creating new one and retrying')
+ from browser_use.skill_cli.commands.cloud import _create_cloud_profile
+
+ new_profile_id = _create_cloud_profile()
+ cloud_params.profile_id = new_profile_id
+ cloud_response = await self._cloud_browser_client.create_browser(cloud_params)
+ else:
+ raise
self.browser_profile.cdp_url = cloud_response.cdpUrl
self.browser_profile.is_local = False
logger.info(f'Cloud browser provisioned, CDP: {cloud_response.cdpUrl}')
diff --git a/browser_use/skill_cli/commands/cloud.py b/browser_use/skill_cli/commands/cloud.py
index 50de79ffd..fb4d86348 100644
--- a/browser_use/skill_cli/commands/cloud.py
+++ b/browser_use/skill_cli/commands/cloud.py
@@ -93,25 +93,9 @@ def _get_api_key() -> str:
sys.exit(1)
-def _ensure_cloud_profile() -> str:
- """Return the default cloud connect profile ID, creating one if needed."""
- config = _read_config()
- profile_id = config.get('cloud_connect_profile_id')
+def _create_cloud_profile() -> str:
+ """Create a new cloud profile and save to config. Returns profile ID."""
api_key = _get_api_key()
-
- # Validate existing profile against current API key
- if profile_id:
- status, resp = _http_request('GET', f'{_base_url("v2")}/profiles/{profile_id}', None, api_key)
- if status == 200:
- return profile_id
- if status != 404:
- # Auth or server error — don't silently create a new profile
- print(f'Error validating cloud profile: HTTP {status}', file=sys.stderr)
- _print_json(resp, file=sys.stderr)
- sys.exit(1)
- # 404 — profile deleted, fall through to create a new one
-
- # Create new profile
body = json.dumps({'name': 'Browser Use CLI'}).encode()
status, resp = _http_request('POST', f'{_base_url("v2")}/profiles', body, api_key)
if status >= 400:
@@ -127,13 +111,21 @@ def _ensure_cloud_profile() -> str:
_print_json(resp, file=sys.stderr)
sys.exit(1)
- # Save to config
+ config = _read_config()
config['cloud_connect_profile_id'] = new_id
_write_config(config)
-
return new_id
+def _get_or_create_cloud_profile() -> str:
+ """Return cloud profile ID from config, creating one if missing. No validation HTTP call."""
+ config = _read_config()
+ profile_id = config.get('cloud_connect_profile_id')
+ if profile_id:
+ return profile_id
+ return _create_cloud_profile()
+
+
def _get_cloud_connect_proxy() -> str | None:
"""Return the cloud connect proxy country code from config."""
from browser_use.skill_cli.config import get_config_value
diff --git a/browser_use/skill_cli/main.py b/browser_use/skill_cli/main.py
index 950696d65..151224fe2 100755
--- a/browser_use/skill_cli/main.py
+++ b/browser_use/skill_cli/main.py
@@ -918,15 +918,15 @@ def _handle_cloud_connect(cloud_args: list[str], args: argparse.Namespace, sessi
# Validate API key exists before spawning daemon (shows our CLI error, not library's)
from browser_use.skill_cli.commands.cloud import (
- _ensure_cloud_profile,
_get_api_key,
_get_cloud_connect_proxy,
_get_cloud_connect_timeout,
+ _get_or_create_cloud_profile,
)
_get_api_key() # exits with helpful message if no key
- cloud_profile_id = _ensure_cloud_profile()
+ cloud_profile_id = _get_or_create_cloud_profile()
# Start daemon with cloud config
ensure_daemon(