Skip to content

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

Simplify ChatOpenAI import to 1 line

Magnus Müller90757686fc07
diff --git a/.cursor/rules/browser-use-rules.mdc b/.cursor/rules/browser-use-rules.mdc
index e24f3c6cd..9041d265d 100644
--- a/.cursor/rules/browser-use-rules.mdc
+++ b/.cursor/rules/browser-use-rules.mdc
@@ -74,8 +74,7 @@ async def search_web(query: str, page: Page):
To define a task and run a browser-use agent:
```python
-from browser_use import Agent
-from browser_use.llm import ChatOpenAI
+from browser_use import Agent, ChatOpenAI
task = "Find the CEO of OpenAI and return their name"
model = ChatOpenAI(model="gpt-4.1-mini")
diff --git a/README.md b/README.md
index 4389407bc..1fd879b21 100644
--- a/README.md
+++ b/README.md
@@ -40,8 +40,7 @@ Spin up your agent:
import asyncio
from dotenv import load_dotenv
load_dotenv()
-from browser_use import Agent
-from browser_use.llm import ChatOpenAI
+from browser_use import Agent, ChatOpenAI
async def main():
agent = Agent(
diff --git a/docs/customize/agent-settings.mdx b/docs/customize/agent-settings.mdx
index 754e01b4f..6baa6b8d4 100644
--- a/docs/customize/agent-settings.mdx
+++ b/docs/customize/agent-settings.mdx
@@ -11,8 +11,7 @@ The `Agent` class is the core component of Browser Use that handles browser auto
## Basic Settings
```python
-from browser_use import Agent
-from browser_use.llm import ChatOpenAI
+from browser_use import Agent, ChatOpenAI
agent = Agent(
task="Search for latest news about AI",
diff --git a/docs/customize/hooks.mdx b/docs/customize/hooks.mdx
index 92c0b007b..b3091a050 100644
--- a/docs/customize/hooks.mdx
+++ b/docs/customize/hooks.mdx
@@ -26,8 +26,7 @@ Each hook should be an `async` callable function that accepts the `agent` instan
### Basic Example
```python
-from browser_use import Agent
-from browser_use.llm import ChatOpenAI
+from browser_use import Agent, ChatOpenAI
async def my_step_hook(agent: Agent):
diff --git a/examples/features/click_fallback_options.py b/examples/features/click_fallback_options.py
deleted file mode 100644
index 0f65f68bf..000000000
--- a/examples/features/click_fallback_options.py
+++ /dev/null
@@ -1,209 +0,0 @@
-import asyncio
-import os
-import sys
-
-sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))
-
-from dotenv import load_dotenv
-
-load_dotenv()
-
-from aiohttp import web # make sure to install aiohttp: pip install aiohttp
-
-from browser_use import Agent, Controller
-from browser_use.llm import ChatOpenAI
-
-# Define a simple HTML page
-HTML_CONTENT = """
-<!DOCTYPE html>
-<html lang="en">
-<head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Custom Select Div</title>
- <style>
- .custom-select {
- position: relative;
- width: 200px;
- font-family: Arial, sans-serif;
- margin-bottom: 20px;
- }
-
- .select-display {
- padding: 10px;
- border: 1px solid #ccc;
- background-color: #fff;
- cursor: pointer;
- }
-
- .select-options {
- position: absolute;
- top: 100%;
- left: 0;
- right: 0;
- border: 1px solid #ccc;
- border-top: none;
- background-color: #fff;
- display: none;
- max-height: 150px;
- overflow-y: auto;
- z-index: 100;
- }
-
- .select-option {
- padding: 10px;
- cursor: pointer;
- }
-
- .select-option:hover {
- background-color: #f0f0f0;
- }
- </style>
-</head>
-<body>
- <div class="custom-select">
- <div class="select-display">Select a fruit</div>
- <div class="select-options">
- <div class="select-option" data-value="option1">Apples</div>
- <div class="select-option" data-value="option2">Oranges</div>
- <div class="select-option" data-value="option3">Pineapples</div>
- </div>
- </div>
-
- <div class="custom-select">
- <div class="select-display">Select a fruit</div>
- <div class="select-options">
- <div class="select-option" data-value="option1">Apples</div>
- <div class="select-option" data-value="option2">Oranges</div>
- <div class="select-option" data-value="option3">Pineapples</div>
- </div>
- </div>
-
- <div class="custom-select">
- <div class="select-display">Select a fruit</div>
- <div class="select-options">
- <div class="select-option" data-value="option1">Apples</div>
- <div class="select-option" data-value="option2">Oranges</div>
- <div class="select-option" data-value="option3">Pineapples</div>
- </div>
- </div>
-
- <div class="custom-select">
- <div class="select-display">Select a fruit</div>
- <div class="select-options">
- <div class="select-option" data-value="option1">Apples</div>
- <div class="select-option" data-value="option2">Oranges</div>
- <div class="select-option" data-value="option3">Pineapples</div>
- </div>
- </div>
-
- <label for="cars">Choose a car:</label>
- <select name="cars" id="cars">
- <option value="volvo">Volvo</option>
- <option value="bmw">BMW</option>
- <option value="mercedes">Mercedes</option>
- <option value="audi">Audi</option>
- </select>
-
- <button onclick="alert('I told you!')">Don't click me</button>
-
- <script>
- document.querySelectorAll('.custom-select').forEach(customSelect => {
- const selectDisplay = customSelect.querySelector('.select-display');
- const selectOptions = customSelect.querySelector('.select-options');
- const options = customSelect.querySelectorAll('.select-option');
-
- selectDisplay.addEventListener('click', (e) => {
- // Close all other dropdowns
- document.querySelectorAll('.select-options').forEach(opt => {
- if (opt !== selectOptions) opt.style.display = 'none';
- });
-
- // Toggle current dropdown
- const isVisible = selectOptions.style.display === 'block';
- selectOptions.style.display = isVisible ? 'none' : 'block';
-
- e.stopPropagation();
- });
-
- options.forEach(option => {
- option.addEventListener('click', () => {
- selectDisplay.textContent = option.textContent;
- selectDisplay.dataset.value = option.getAttribute('data-value');
- selectOptions.style.display = 'none';
- });
- });
- });
-
- // Close all dropdowns if clicking outside
- document.addEventListener('click', () => {
- document.querySelectorAll('.select-options').forEach(opt => {
- opt.style.display = 'none';
- });
- });
- </script>
-</body>
-</html>
-
-"""
-
-
-# aiohttp request handler to serve the HTML content
-async def handle_root(request):
- return web.Response(text=HTML_CONTENT, content_type='text/html')
-
-
-# Function to run the HTTP server
-async def run_http_server():
- app = web.Application()
- app.router.add_get('/', handle_root)
- runner = web.AppRunner(app)
- await runner.setup()
- site = web.TCPSite(runner, 'localhost', 8000)
- await site.start()
- print('HTTP server running on http://localhost:8000')
- # Keep the server running indefinitely.
- await asyncio.Event().wait()
-
-
-# Your agent tasks and other logic
-controller = Controller()
-
-
-async def main():
- # Start the HTTP server in the background.
- server_task = asyncio.create_task(run_http_server())
-
- # Example tasks for the agent.
- xpath_task = 'Open http://localhost:8000/, click element with the xpath "/html/body/div/div[1]" and then click on Oranges'
- css_selector_task = 'Open http://localhost:8000/, click element with the selector div.select-display and then click on apples'
- text_task = 'Open http://localhost:8000/, click the third element with the text "Select a fruit" and then click on Apples, then click the second element with the text "Select a fruit" and then click on Oranges'
- select_task = 'Open http://localhost:8000/, choose the car BMW'
- button_task = 'Open http://localhost:8000/, click on the button'
-
- llm = ChatOpenAI(model='gpt-4.1')
- # llm = ChatGoogleGenerativeAI(
- # model="gemini-2.0-flash-lite",
- # )
-
- # Run different agent tasks.
- for task in [xpath_task, css_selector_task, text_task, select_task, button_task]:
- agent = Agent(
- task=task,
- llm=llm,
- controller=controller,
- )
- await agent.run()
-
- # Wait for user input before shutting down.
- input('Press Enter to close...')
- # Cancel the server task once finished.
- server_task.cancel()
- try:
- await server_task
- except asyncio.CancelledError:
- print('HTTP server stopped.')
-
-
-if __name__ == '__main__':
- asyncio.run(main())
diff --git a/examples/features/custom_system_prompt.py b/examples/features/custom_system_prompt.py
index f28d4690f..88f1474f3 100644
--- a/examples/features/custom_system_prompt.py
+++ b/examples/features/custom_system_prompt.py
@@ -17,8 +17,7 @@ except Exception as e:
print(f'Error initializing Laminar: {e}')
-from browser_use import Agent
-from browser_use.llm import ChatOpenAI
+from browser_use import Agent, ChatOpenAI
extend_system_message = (
'REMEMBER the most important RULE: ALWAYS open first a new tab and go first to url wikipedia.com no matter the task!!!'
diff --git a/examples/features/initial_actions.py b/examples/features/initial_actions.py
index 49cdab022..0e0f60615 100644
--- a/examples/features/initial_actions.py
+++ b/examples/features/initial_actions.py
@@ -8,8 +8,7 @@ from dotenv import load_dotenv
load_dotenv()
-from browser_use import Agent
-from browser_use.llm import ChatOpenAI
+from browser_use import Agent, ChatOpenAI
llm = ChatOpenAI(model='gpt-4.1')
diff --git a/examples/features/multi-tab_handling.py b/examples/features/multi-tab_handling.py
index 9a83d0be5..27f8f158c 100644
--- a/examples/features/multi-tab_handling.py
+++ b/examples/features/multi-tab_handling.py
@@ -14,8 +14,7 @@ from dotenv import load_dotenv
load_dotenv()
-from browser_use import Agent
-from browser_use.llm import ChatOpenAI
+from browser_use import Agent, ChatOpenAI
# video: https://preview.screen.studio/share/clenCmS6
llm = ChatOpenAI(model='gpt-4.1')
diff --git a/examples/features/pause_agent.py b/examples/features/pause_agent.py
index 914c528a6..3b3dd792f 100644
--- a/examples/features/pause_agent.py
+++ b/examples/features/pause_agent.py
@@ -9,8 +9,7 @@ from dotenv import load_dotenv
load_dotenv()
-from browser_use import Agent
-from browser_use.llm import ChatOpenAI
+from browser_use import Agent, ChatOpenAI
class AgentController:
diff --git a/examples/features/planner.py b/examples/features/planner.py
index cf7ecb2d4..91c63ba78 100644
--- a/examples/features/planner.py
+++ b/examples/features/planner.py
@@ -8,8 +8,7 @@ from dotenv import load_dotenv
load_dotenv()
-from browser_use import Agent
-from browser_use.llm import ChatOpenAI
+from browser_use import Agent, ChatOpenAI
llm = ChatOpenAI(model='gpt-4.1', temperature=0.0)
planner_llm = ChatOpenAI(
diff --git a/examples/features/small_model_for_extraction.py b/examples/features/small_model_for_extraction.py
index 73e60a92e..6d44a2ff2 100644
--- a/examples/features/small_model_for_extraction.py
+++ b/examples/features/small_model_for_extraction.py
@@ -8,8 +8,7 @@ from dotenv import load_dotenv
load_dotenv()
-from browser_use import Agent
-from browser_use.llm import ChatOpenAI
+from browser_use import Agent, ChatOpenAI
llm = ChatOpenAI(model='gpt-4.1', temperature=0.0)
small_llm = ChatOpenAI(model='gpt-4.1-mini', temperature=0.0)
diff --git a/examples/file_system/file_system.py b/examples/file_system/file_system.py
index 82a0e5b98..589a3df9d 100644
--- a/examples/file_system/file_system.py
+++ b/examples/file_system/file_system.py
@@ -5,8 +5,7 @@ import shutil
from dotenv import load_dotenv
-from browser_use import Agent
-from browser_use.llm import ChatOpenAI
+from browser_use import Agent, ChatOpenAI
load_dotenv()
diff --git a/examples/models/gpt-4.1.py b/examples/models/gpt-4.1.py
index 574f34711..8320d11ab 100644
--- a/examples/models/gpt-4.1.py
+++ b/examples/models/gpt-4.1.py
@@ -9,8 +9,7 @@ import asyncio
from dotenv import load_dotenv
from lmnr import Laminar
-from browser_use import Agent
-from browser_use.llm import ChatOpenAI
+from browser_use import Agent, ChatOpenAI
load_dotenv()
diff --git a/examples/models/gpt-5-mini.py b/examples/models/gpt-5-mini.py
index dd03cf715..ce31c872e 100644
--- a/examples/models/gpt-5-mini.py
+++ b/examples/models/gpt-5-mini.py
@@ -9,8 +9,7 @@ import asyncio
from dotenv import load_dotenv
from lmnr import Laminar
-from browser_use import Agent
-from browser_use.llm import ChatOpenAI
+from browser_use import Agent, ChatOpenAI
load_dotenv()
diff --git a/examples/models/novita.py b/examples/models/novita.py
index 46e2dd3e2..77948c72f 100644
--- a/examples/models/novita.py
+++ b/examples/models/novita.py
@@ -15,8 +15,7 @@ from dotenv import load_dotenv
load_dotenv()
-from browser_use import Agent
-from browser_use.llm import ChatOpenAI
+from browser_use import Agent, ChatOpenAI
api_key = os.getenv('NOVITA_API_KEY', '')
if not api_key:
diff --git a/examples/models/openrouter.py b/examples/models/openrouter.py
index 5fc75ffba..0c4e6a828 100644
--- a/examples/models/openrouter.py
+++ b/examples/models/openrouter.py
@@ -10,8 +10,7 @@ import os
from dotenv import load_dotenv
from lmnr import Laminar
-from browser_use import Agent
-from browser_use.llm import ChatOpenAI
+from browser_use import Agent, ChatOpenAI
load_dotenv()
diff --git a/examples/ui/gradio_demo.py b/examples/ui/gradio_demo.py
index 53f65ba8c..50575b203 100644
--- a/examples/ui/gradio_demo.py
+++ b/examples/ui/gradio_demo.py
@@ -17,8 +17,7 @@ from rich.panel import Panel
from rich.text import Text
# Local module imports
-from browser_use import Agent
-from browser_use.llm import ChatOpenAI
+from browser_use import Agent, ChatOpenAI
@dataclass
diff --git a/examples/use-cases/captcha.py b/examples/use-cases/captcha.py
index c0104f8e6..97437a62c 100644
--- a/examples/use-cases/captcha.py
+++ b/examples/use-cases/captcha.py
@@ -18,11 +18,7 @@ from dotenv import load_dotenv
load_dotenv()
-from browser_use import Agent
-from browser_use.llm import ChatOpenAI
-
-if not os.getenv('OPENAI_API_KEY'):
- raise ValueError('OPENAI_API_KEY is not set. Please add it to your environment variables.')
+from browser_use import Agent, ChatOpenAI
async def main():