Open-source Python library for AI browser agents, plus Browser Use Cloud: stealth browsers and a hosted agent API.
Python ai-agentsai-toolsbrowser-agentbrowser-automationbrowser-usecdpcloud-browsercomputer-useheadless-browserllmmcpplaywrightpythonremote-browserstealth-browserweb-automationweb-scraping
add device_id to all events
Nick Sweeting163faf355b13
diff --git a/browser_use/sync/auth.py b/browser_use/sync/auth.pyindex 1f2890e26..3853c9f6c 100644--- a/browser_use/sync/auth.py+++ b/browser_use/sync/auth.py@@ -5,6 +5,7 @@ OAuth2 Device Authorization Grant flow client for browser-use.import asyncioimport jsonimport os+import shutilimport timefrom datetime import datetime@@ -295,11 +296,12 @@ class DeviceAuthClient:verification_uri = device_auth['verification_uri'].replace(self.base_url, frontend_url)verification_uri_complete = device_auth['verification_uri_complete'].replace(self.base_url, frontend_url)+ terminal_width, _terminal_height = shutil.get_terminal_size((80, 20))if show_instructions:- logger.info('\n\n' + '─' * 70)+ logger.info('─' * (terminal_width - 1))logger.info('🌐 View the details of this run in Browser Use Cloud:')logger.info(f' 👉 {verification_uri_complete}')- logger.info('─' * 70 + '\n')+ logger.info('─' * (terminal_width - 1) + '\n')# Poll for tokentoken_data = await self.poll_for_token(diff --git a/browser_use/sync/service.py b/browser_use/sync/service.pyindex 8f6fd22e4..e9072f6e8 100644--- a/browser_use/sync/service.py+++ b/browser_use/sync/service.py@@ -73,9 +73,14 @@ class CloudSync:# Send event (batch format with direct BaseEvent serialization)async with httpx.AsyncClient() as client:+ # Serialize event and add device_id to all events+ event_data = event.model_dump(mode='json')+ if self.auth_client and self.auth_client.device_id:+ event_data['device_id'] = self.auth_client.device_id+response = await client.post(f'{self.base_url.rstrip("/")}/api/v1/events',- json={'events': [event.model_dump(mode='json')]},+ json={'events': [event_data]},headers=headers,timeout=10.0,)@@ -162,11 +167,14 @@ class CloudSync:if line.strip():events.append(json.loads(line))- # Update user_id+ # Update user_id and device_iduser_id = self.auth_client.user_id+ device_id = self.auth_client.device_idfor event in events:if 'user_id' in event:event['user_id'] = user_id+ # Add device_id to all events+ event['device_id'] = device_id# Write backupdated_content = '\n'.join(json.dumps(event) for event in events) + '\n'diff --git a/docs/customize/real-browser.mdx b/docs/customize/real-browser.mdxindex 5e7deaa80..48b4f661e 100644--- a/docs/customize/real-browser.mdx+++ b/docs/customize/real-browser.mdx@@ -284,28 +284,24 @@ await reused_session.close()```pythonfrom browser_use import Agent, BrowserSessionfrom browser_use.llm import ChatOpenAI+from playwright.async_api import async_playwright-shared_browser = BrowserSession(- storage_state='/tmp/cookies.json',- user_data_dir=None,- keep_alive=True,- headless=True,-)-await shared_browser.start() # when keep_alive=True, you must start the session yourself--agent1 = Agent(- task="The first task...",- llm=ChatOpenAI(model="gpt-4o-mini"),- browser_session=shared_browser, # pass the session in-)-agent2 = Agent(- task="The second task...",- llm=ChatOpenAI(model="gpt-4o-mini"),- browser_session=shared_browser, # re-use the same session-)-await asyncio.gather(agent1.run(), agent2.run()) # run in parallel--await shared_browser.close()+async with async_playwright() as playwright:+ browser_context = await playwright.chromium.launch_persistent_context()+ page1 = await browser_context.new_page()+ page2 = await browser_context.new_page()++ agent1 = Agent(+ task="The first task...",+ llm=ChatOpenAI(model="gpt-4o-mini"),+ page=page1,+ )+ agent2 = Agent(+ task="The second task...",+ llm=ChatOpenAI(model="gpt-4o-mini"),+ page=page2,+ )+ await asyncio.gather(agent1.run(), agent2.run()) # run in parallel```### Parallel Agents, Same Browser, Same Tab