Skip to content

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

fix BrowserSession init on reuse case

Nick Sweeting232898623614
diff --git a/browser_use/browser/session.py b/browser_use/browser/session.py
index c7fce05ad..535a7782c 100644
--- a/browser_use/browser/session.py
+++ b/browser_use/browser/session.py
@@ -115,7 +115,9 @@ class BrowserSession(BaseModel):
revalidate_instances='always',
frozen=False,
arbitrary_types_allowed=True,
- populate_by_name=True,
+ validate_by_name=True,
+ validate_by_alias=True,
+ from_attributes=True,
)
# this class accepts arbitrary extra **kwargs in init because of the extra='allow' pydantic option
# they are saved on the model, then applied to self.browser_profile via .apply_session_overrides_to_profile()
diff --git a/examples/features/multiple_agents_same_browser.py b/examples/features/multiple_agents_same_browser.py
index f7f2bd70f..59c21df0f 100644
--- a/examples/features/multiple_agents_same_browser.py
+++ b/examples/features/multiple_agents_same_browser.py
@@ -10,56 +10,59 @@ load_dotenv()
from langchain_openai import ChatOpenAI
-from browser_use import Agent, Browser
+from browser_use import Agent, BrowserSession
# Video: https://preview.screen.studio/share/8Elaq9sm
async def main():
# Persist the browser state across agents
- browser = Browser()
- async with await browser.new_context() as context:
- model = ChatOpenAI(model='gpt-4o')
- current_agent = None
-
- async def get_input():
- return await asyncio.get_event_loop().run_in_executor(
- None, lambda: input('Enter task (p: pause current agent, r: resume, b: break): ')
- )
-
- while True:
- task = await get_input()
-
- if task.lower() == 'p':
- # Pause the current agent if one exists
- if current_agent:
- current_agent.pause()
- continue
- elif task.lower() == 'r':
- # Resume the current agent if one exists
- if current_agent:
- current_agent.resume()
- continue
- elif task.lower() == 'b':
- # Break the current agent's execution if one exists
- if current_agent:
- current_agent.stop()
- current_agent = None
- continue
-
- # If there's a current agent running, pause it before starting new one
- if current_agent:
- current_agent.pause()
+ browser_session = BrowserSession(
+ keep_alive=True,
+ user_data_dir=None,
+ )
+ await browser_session.start()
+ model = ChatOpenAI(model='gpt-4o')
+ current_agent = None
- # Create and run new agent with the task
- current_agent = Agent(
- task=task,
- llm=model,
- browser_context=context,
- )
+ async def get_input():
+ return await asyncio.get_event_loop().run_in_executor(
+ None, lambda: input('Enter task (p: pause current agent, r: resume, b: break): ')
+ )
- # Run the agent asynchronously without blocking
- asyncio.create_task(current_agent.run())
+ while True:
+ task = await get_input()
+
+ if task.lower() == 'p':
+ # Pause the current agent if one exists
+ if current_agent:
+ current_agent.pause()
+ continue
+ elif task.lower() == 'r':
+ # Resume the current agent if one exists
+ if current_agent:
+ current_agent.resume()
+ continue
+ elif task.lower() == 'b':
+ # Break the current agent's execution if one exists
+ if current_agent:
+ current_agent.stop()
+ current_agent = None
+ continue
+
+ # If there's a current agent running, pause it before starting new one
+ if current_agent:
+ current_agent.pause()
+
+ # Create and run new agent with the task
+ current_agent = Agent(
+ task=task,
+ llm=model,
+ browser_session=browser_session,
+ )
+
+ # Run the agent asynchronously without blocking
+ asyncio.create_task(current_agent.run())
asyncio.run(main())