From 2ad7cbe473d7f02f8eb408d18e54b7cdcf03bbb9 Mon Sep 17 00:00:00 2001 From: William Schmitt Date: Mon, 16 Nov 2026 12:38:52 -0800 Subject: [PATCH] examples: add a browser-use example that connects over CDP Issue #144: the docs' browser-use snippet failed with Playwright's `KeyError: 'error'` in _connection.py dispatch. The reporter's log shows "Connecting to remote browser via WSS", which is browser-use 0.1.x's BrowserConfig(wss_url=...) path (browser_use/browser/browser.py:124 in 0.1.40). It calls playwright.chromium.connect(), which speaks Playwright's own wire protocol. A Browserbase connect_url speaks CDP, so the first reply is a CDP error object with no nested "error" field and Playwright's dispatch() raises KeyError. Reproduced on a live dev session with playwright 1.51.0. cdp_url (connect_over_cdp) was the right option. The docs have since switched to Browser(browser_profile=BrowserProfile( cdp_url=...)), but browser-use 1.0 removed BrowserProfile, so that snippet now fails with ImportError. 1.0 takes Browser(cdp_url=...). browser-use 1.0 needs Python >= 3.11 and this repo's dev env and lint run on 3.9, so the example is a PEP 723 script with its own deps, and file-level mypy/pyright directives keep ./scripts/lint green without installing it. Browser.stop() never ends a cdp_url browser, so the example releases the session with REQUEST_RELEASE instead of leaving it billing until timeout. Co-Authored-By: Claude Opus 5.5 --- examples/browser_use_basic.py | 45 +++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100755 examples/browser_use_basic.py diff --git a/examples/browser_use_basic.py b/examples/browser_use_basic.py new file mode 100755 index 00000000..87890bab --- /dev/null +++ b/examples/browser_use_basic.py @@ -0,0 +1,45 @@ +#!/usr/bin/env -S uv run --script +# /// script +# requires-python = ">=3.11" +# dependencies = ["browserbase>=1.26.0", "browser-use>=1.0.6,<2", "python-dotenv"] +# /// +# browser-use needs Python 3.11+, so this runs in its own environment: `uv run examples/browser_use_basic.py`. +# Lint runs on 3.9 without browser-use installed: +# mypy: ignore-errors +# pyright: basic, reportMissingImports=false +import os +import asyncio + +from dotenv import load_dotenv +from browser_use import Agent, Browser, ChatAnthropic + +from browserbase import Browserbase + +load_dotenv(override=True) + +bb = Browserbase(api_key=os.environ["BROWSERBASE_API_KEY"]) + + +async def run(llm: object) -> None: + session = bb.sessions.create() + print(f"Session: https://www.browserbase.com/sessions/{session.id}") + + # cdp_url, not browser-use 0.x's wss_url: connect_url speaks CDP, not Playwright's protocol. + browser = Browser(cdp_url=session.connect_url) + try: + agent = Agent( + task="Open https://news.ycombinator.com and return the title of the top story.", + llm=llm, + browser=browser, + ) + history = await agent.run() + print(history.final_result()) + finally: + # stop() only disconnects from a cdp_url browser; release the session so it stops billing. + await browser.stop() + bb.sessions.update(session.id, status="REQUEST_RELEASE") + print("Done!") + + +if __name__ == "__main__": + asyncio.run(run(ChatAnthropic(model="claude-sonnet-5-5")))