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
Fix SDK compatibility and remote browser lifecycle checks
Magnus Mueller2200f50c4a7a
diff --git a/browser_use/integrations/openai/browser_use.py b/browser_use/integrations/openai/browser_use.pyindex 6e9c26db8..846e4bd40 100644--- a/browser_use/integrations/openai/browser_use.py+++ b/browser_use/integrations/openai/browser_use.py@@ -18,7 +18,6 @@ from openai.lib import browser as apiclass Reference(BaseModel):tab: str- document: intbackend: int@@ -55,7 +54,14 @@ class BrowserUse:"""Close only the browser that this driver created."""if not self._closed:if self._owns_browser:- await self.browser.kill()+ try:+ cloud = self.browser._cloud_browser_client+ if cloud.current_session_id:+ stopped = await cloud.stop_browser()+ if stopped.status != 'stopped':+ raise api.BrowserError('Cloud did not confirm browser cleanup.')+ finally:+ await self.browser.kill()self._refs.clear()self._closed = True@@ -85,19 +91,20 @@ class BrowserUse:self._active = targetreturn Page(self.browser, target, session.session_id)- async def _document(self, page: Page) -> int:- result = await self.browser.cdp_client.send.DOM.getDocument({'depth': 0}, session_id=await page.session_id)- return result['root']['backendNodeId']-async def _element(self, page: Page, target: str):ref = self._refs.get(target)- if ref is None or ref.tab != page._target_id or ref.document != await self._document(page):+ if ref is None or ref.tab != page._target_id:raise api.BrowserError('Stale element reference; read the page again.')- element = await page.get_element(ref.backend)- connected = await element.evaluate('() => this.isConnected')- if connected.casefold() != 'true':- raise api.BrowserError('Detached element; read the page again.')- return element+ try:+ await self.browser.cdp_client.send.DOM.getDocument({'depth': 0}, session_id=await page.session_id)+ element = await page.get_element(ref.backend)+ connected = await element.evaluate('() => this.isConnected')+ if connected.casefold() != 'true':+ raise ValueError('Detached element')+ return element+ except Exception as exc:+ self._refs.pop(target, None)+ raise api.BrowserError('Stale element reference; read the page again.') from excasync def navigate(self, args: api.NavigateInput) -> None:"""Invalidate element references before navigating the selected tab."""@@ -108,7 +115,7 @@ class BrowserUse:async def read_page(self, args: api.ReadPageInput) -> str:"""Read the accessibility tree and issue tab-bound element references."""page = await self._page(args['tab_id'])- document = await self._document(page)+ await self.browser.cdp_client.send.DOM.getDocument({'depth': 0}, session_id=await page.session_id)self._refs = {name: ref for name, ref in self._refs.items() if ref.tab != page._target_id}result = await self.browser.cdp_client.send.Accessibility.getFullAXTree(session_id=await page.session_id)interactive = {@@ -140,7 +147,7 @@ class BrowserUse:if backend and role in interactive:self._next_ref += 1ref = f'ref_{self._next_ref}'- self._refs[ref] = Reference(tab=page._target_id, document=document, backend=backend)+ self._refs[ref] = Reference(tab=page._target_id, backend=backend)prefix = f'[{ref}] 'lines.append(f'{prefix}{role} {json.dumps(name, ensure_ascii=False)}')return '\n'.join(lines)@@ -246,6 +253,11 @@ class BrowserUse:"""Close only the selected tab."""page = await self._page(args['tab_id'])await self.browser.close_page(page)+ for _ in range(500):+ if not any(tab['id'] == page._target_id for tab in (await self.state())['tabs']):+ return+ await asyncio.sleep(0.02)+ raise api.BrowserError('Tab closure is pending; read the tab list before retrying.')async def _bounds(self, page: Page, points: list[tuple[int, int]]) -> None:metrics = await self.browser.cdp_client.send.Page.getLayoutMetrics(session_id=await page.session_id)diff --git a/browser_use/llm/cerebras/chat.py b/browser_use/llm/cerebras/chat.pyindex e87a00b1c..d6b764b5b 100644--- a/browser_use/llm/cerebras/chat.py+++ b/browser_use/llm/cerebras/chat.py@@ -12,6 +12,7 @@ from openai import (APITimeoutError,AsyncOpenAI,RateLimitError,+ Timeout,)from openai.types.chat import ChatCompletionfrom pydantic import BaseModel@@ -58,8 +59,8 @@ class ChatCerebras(BaseChatModel):return AsyncOpenAI(api_key=api_key,- base_url=self.base_url,- timeout=self.timeout,+ base_url=str(self.base_url) if self.base_url is not None else None,+ timeout=Timeout(**self.timeout.as_dict()) if isinstance(self.timeout, httpx.Timeout) else self.timeout,**(self.client_params or {}),)diff --git a/browser_use/llm/deepseek/chat.py b/browser_use/llm/deepseek/chat.pyindex f67dca0aa..5452903e1 100644--- a/browser_use/llm/deepseek/chat.py+++ b/browser_use/llm/deepseek/chat.py@@ -13,6 +13,7 @@ from openai import (APITimeoutError,AsyncOpenAI,RateLimitError,+ Timeout,)from pydantic import BaseModel@@ -61,8 +62,8 @@ class ChatDeepSeek(BaseChatModel):return AsyncOpenAI(api_key=api_key,- base_url=self.base_url,- timeout=self.timeout,+ base_url=str(self.base_url) if self.base_url is not None else None,+ timeout=Timeout(**self.timeout.as_dict()) if isinstance(self.timeout, httpx.Timeout) else self.timeout,**(self.client_params or {}),)diff --git a/browser_use/llm/openai/chat.py b/browser_use/llm/openai/chat.pyindex 51226e5db..cf7db86ef 100644--- a/browser_use/llm/openai/chat.py+++ b/browser_use/llm/openai/chat.py@@ -3,7 +3,7 @@ from dataclasses import dataclass, fieldfrom typing import Any, Literal, TypeVar, overloadimport httpx-from openai import APIConnectionError, APIStatusError, AsyncOpenAI, RateLimitError+from openai import APIConnectionError, APIStatusError, AsyncOpenAI, DefaultAsyncHttpxClient, RateLimitError, Timeoutfrom openai.types.chat import ChatCompletionContentPartTextParamfrom openai.types.chat.chat_completion import ChatCompletionfrom openai.types.shared.chat_model import ChatModel@@ -59,7 +59,7 @@ class ChatOpenAI(BaseChatModel):max_retries: int = 5 # Increase default retries for automation reliabilitydefault_headers: Mapping[str, str] | None = Nonedefault_query: Mapping[str, object] | None = None- http_client: httpx.AsyncClient | None = None+ http_client: httpx.AsyncClient | DefaultAsyncHttpxClient | None = None_strict_response_validation: bool = Falsemax_completion_tokens: int | None = 4096reasoning_models: list[ChatModel | str] | None = field(@@ -88,9 +88,9 @@ class ChatOpenAI(BaseChatModel):'api_key': self.api_key,'organization': self.organization,'project': self.project,- 'base_url': self.base_url,- 'websocket_base_url': self.websocket_base_url,- 'timeout': self.timeout,+ 'base_url': str(self.base_url) if self.base_url is not None else None,+ 'websocket_base_url': str(self.websocket_base_url) if self.websocket_base_url is not None else None,+ 'timeout': Timeout(**self.timeout.as_dict()) if isinstance(self.timeout, httpx.Timeout) else self.timeout,'max_retries': self.max_retries,'default_headers': self.default_headers,'default_query': self.default_query,diff --git a/examples/integrations/openai/quickstart.py b/examples/integrations/openai/quickstart.pyindex 54fb3140e..7ae1130e8 100644--- a/examples/integrations/openai/quickstart.py+++ b/examples/integrations/openai/quickstart.py@@ -41,7 +41,7 @@ async def main():max_steps=6,)text = await driver.get_page_text({'tab_id': None})- if 'Example Domain' not in text:+ if 'documentation examples' not in text:raise RuntimeError('The browser did not show the expected page.')print(response.output_text)finally:diff --git a/pyproject.toml b/pyproject.tomlindex cf482d95d..a588764c8 100644--- a/pyproject.toml+++ b/pyproject.toml@@ -78,7 +78,7 @@ examples = ["imgcat==0.6.0",# "stagehand-py>=0.3.6",# "browserbase>=0.4.0",- "langchain-openai==1.1.14",+ "langchain-openai==1.7.2",]eval = ["lmnr[all]==0.7.42",diff --git a/tests/ci/integrations/test_openai_browser_use.py b/tests/ci/integrations/test_openai_browser_use.pyindex 28fdd33dd..1deb3fddc 100644--- a/tests/ci/integrations/test_openai_browser_use.py+++ b/tests/ci/integrations/test_openai_browser_use.py@@ -9,7 +9,6 @@ import pytestfrom openai.lib.browser import AsyncBrowserToolset, BrowserErrorfrom openai.types.responses import ResponseFunctionToolCallfrom PIL import Image-from playwright.async_api import async_playwrightfrom browser_use import BrowserSessionfrom browser_use.integrations.openai import BrowserUse@@ -17,20 +16,14 @@ from browser_use.integrations.openai import BrowserUse@pytest.fixtureasync def driver():- async with async_playwright() as playwright:- browser = BrowserSession(- headless=True,- executable_path=playwright.chromium.executable_path,- enable_default_extensions=False,- user_data_dir=None,- )- await browser.start()- try:- async with BrowserUse(browser) as value:- yield value- assert browser.is_cdp_connected- finally:- await browser.kill()+ browser = BrowserSession(headless=True, enable_default_extensions=False, user_data_dir=None)+ await browser.start()+ try:+ async with BrowserUse(browser) as value:+ yield value+ assert browser.is_cdp_connected+ finally:+ await browser.kill()async def fixture_page(driver, html):@@ -80,7 +73,7 @@ async def test_driver_rejects_stale_or_cross_tab_reference(driver, change):await driver.new_tab({'url': 'about:blank'})else:await driver.read_page({'tab_id': None, 'filter': 'interactive'})- with pytest.raises((BrowserError, RuntimeError)):+ with pytest.raises(BrowserError):await driver.click({'tab_id': None, 'target': target})@@ -136,3 +129,37 @@ async def test_driver_wait_and_pointer_bounds(driver):await driver.click_at({'tab_id': None, 'x': 999999, 'y': 0, 'button': 'left', 'click_count': 1, 'keys': []})with pytest.raises(BrowserError):await driver.zoom({'tab_id': None, 'region': [0, 0, 999999, 100]})+++async def test_sdk_dispatch_changes_form_and_selects_option(driver):+ page = await fixture_page(+ driver,+ """<label>Name<input id="name"></label><label>Choice<select id="choice"><option value="a">Alpha</option><option value="b">Beta</option></select></label><button onclick="document.title=document.querySelector('#name').value">Save</button>""",+ )++ async def approve(context):+ return True++ tools = AsyncBrowserToolset(driver, members=['type', 'select_option', 'click'], confirm=approve)+ try:+ text = await driver.read_page({'tab_id': None, 'filter': 'interactive'})+ for member, arguments in [+ ('type', {'target': reference(text, 'Name'), 'text': 'SDK result'}),+ ('select_option', {'target': reference(text, 'Choice'), 'values': ['b']}),+ ('click', {'target': reference(text, 'Save')}),+ ]:+ await tools.execute(+ ResponseFunctionToolCall(+ id='fc_' + member,+ call_id='call_' + member,+ type='function_call',+ status='completed',+ name='browser_' + member,+ arguments=json.dumps({'tab_id': None, **arguments}),+ )+ )+ assert await page.evaluate('() => document.title') == 'SDK result'+ assert await page.evaluate("() => document.querySelector('#choice').value") == 'b'+ finally:+ tools.close()diff --git a/tests/ci/models/test_llm_openai.py b/tests/ci/models/test_llm_openai.pyindex d29e49645..1ae80de21 100644--- a/tests/ci/models/test_llm_openai.py+++ b/tests/ci/models/test_llm_openai.py@@ -64,3 +64,25 @@ async def test_openai_empty_reasoning_model_patterns_preserve_sampling_parameterassert captured['temperature'] == 0.7assert captured['frequency_penalty'] == 0.4assert 'reasoning_effort' not in captured++[email protected]('provider', ['openai', 'deepseek', 'cerebras'])+async def test_clients_preserve_httpx_url_and_timeout_settings(provider):+ import httpx++ from browser_use.llm.cerebras.chat import ChatCerebras+ from browser_use.llm.deepseek.chat import ChatDeepSeek++ classes = {'openai': ChatOpenAI, 'deepseek': ChatDeepSeek, 'cerebras': ChatCerebras}+ model = classes[provider](+ model='test-model',+ api_key='test-key',+ base_url=httpx.URL('https://example.com/v1'),+ timeout=httpx.Timeout(connect=1, read=2, write=3, pool=4),+ )+ client = model.get_client() if provider == 'openai' else model._client()+ try:+ assert str(client.base_url) == 'https://example.com/v1/'+ assert client.timeout.as_dict() == {'connect': 1, 'read': 2, 'write': 3, 'pool': 4}+ finally:+ await client.close()