Skip to content

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

throw a hard error requiring BrowserSession to be initialized before passing it in if keep_alive=True

Nick Sweeting0cd31c8b2a99
diff --git a/browser_use/agent/service.py b/browser_use/agent/service.py
index ee8987853..3aa3866db 100644
--- a/browser_use/agent/service.py
+++ b/browser_use/agent/service.py
@@ -313,6 +313,18 @@ class Agent(Generic[Context]):
browser_profile = browser_profile or DEFAULT_BROWSER_PROFILE
if browser_session:
+ # Check if user is trying to reuse an uninitialized session
+ if browser_session.browser_profile.keep_alive and not browser_session.initialized:
+ self.logger.error(
+ '❌ Passed a BrowserSession with keep_alive=True that is not initialized. '
+ 'Call await browser_session.start() before passing it to Agent() to reuse the same browser. '
+ 'Otherwise, each agent will launch its own browser instance.'
+ )
+ raise ValueError(
+ 'BrowserSession with keep_alive=True must be initialized before passing to Agent. '
+ 'Call: await browser_session.start()'
+ )
+
# always copy sessions that are passed in to avoid conflicting with other agents sharing the same session
self.browser_session = browser_session.model_copy(
update={