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
checkpoint
Nick Sweeting75f14c4784d0
diff --git a/browser_use/agent/views.py b/browser_use/agent/views.pyindex 73f81245f..434cb2ad4 100644--- a/browser_use/agent/views.py+++ b/browser_use/agent/views.py@@ -2,7 +2,6 @@ from __future__ import annotationsimport jsonimport traceback-import uuidfrom dataclasses import dataclassfrom pathlib import Pathfrom typing import Any, Literal@@ -10,6 +9,7 @@ from typing import Any, Literalfrom langchain_core.language_models.chat_models import BaseChatModelfrom openai import RateLimitErrorfrom pydantic import BaseModel, ConfigDict, Field, ValidationError, create_model+from uuid_extensions import uuid7strfrom browser_use.agent.message_manager.views import MessageManagerStatefrom browser_use.browser.views import BrowserStateHistory@@ -35,7 +35,7 @@ REQUIRED_LLM_API_ENV_VARS = {class AgentSettings(BaseModel):- """Options for the agent"""+ """Configuration options for the Agent"""use_vision: bool = Trueuse_vision_for_planner: bool = False@@ -72,13 +72,13 @@ class AgentSettings(BaseModel):extend_planner_system_message: str | None = None# Playwright script generation setting- save_playwright_script_path: str | None = None # Path to save the generated Playwright script+ save_playwright_script_path: str | None = None # Path to save the generated Playwright script TODO: fix or removeclass AgentState(BaseModel):"""Holds all state information for an Agent"""- agent_id: str = Field(default_factory=lambda: str(uuid.uuid4()))+ agent_id: str = Field(default_factory=uuid7str)n_steps: int = 1consecutive_failures: int = 0last_result: list[ActionResult] | None = None@@ -128,7 +128,7 @@ class StepMetadata(BaseModel):class AgentBrain(BaseModel):- """Current state of the agent"""+ """Current internal working memory of the agent, we ask the LLM to decide new values for these on each output"""evaluation_previous_goal: strmemory: str@@ -136,9 +136,20 @@ class AgentBrain(BaseModel):class AgentOutput(BaseModel):- """Output model for agent-- @dev note: this model is extended with custom actions in AgentService. You can also use some fields that are not in this model as provided by the linter, as long as they are registered in the DynamicActions model.+ """+ Output model for LLM, i.e. what we are expecting in LLM structured output in response to our prompt.+ {+ current_state: AgentBrain({+ evaluation_previous_goal: "we did ok, team",+ memory: "filled in xyz into page, still need to do xyz...",+ next_goal: "click on the link at index 127, then open that new tab"+ }),+ "action": [+ ActionModel({action_name: "click_element_by_index", action_params: {index: 127}}),+ ActionModel({action_name: "switch_to_tab", action_params: {page_id: 3}}),+ ... other multi-action steps ...+ ],+ }"""model_config = ConfigDict(arbitrary_types_allowed=True)@@ -209,7 +220,7 @@ class AgentHistory(BaseModel):class AgentHistoryList(BaseModel):- """List of agent history items"""+ """List of AgentHistory messages, i.e. the history of the agent's actions and thoughts."""history: list[AgentHistory]diff --git a/browser_use/browser/profile.py b/browser_use/browser/profile.pyindex c6791982b..d0dba3f0b 100644--- a/browser_use/browser/profile.py+++ b/browser_use/browser/profile.py@@ -546,6 +546,7 @@ class BrowserProfile(BrowserConnectArgs, BrowserLaunchPersistentContextArgs, Bro# ... extends options defined in:# BrowserLaunchPersistentContextArgs, BrowserLaunchArgs, BrowserNewContextArgs, BrowserConnectArgs+ # do something like this someday when we need to store these in a DB# id: str = Field(default_factory=uuid7str)# label: str = 'default'diff --git a/browser_use/controller/registry/service.py b/browser_use/controller/registry/service.pyindex 4d5c26574..1e9148390 100644--- a/browser_use/controller/registry/service.py+++ b/browser_use/controller/registry/service.py@@ -136,6 +136,8 @@ class Registry(Generic[Context]):# 1. There are parameters# 2. First parameter has a BaseModel annotation# 3. AND the function signature actually takes a BaseModel as first param (not auto-generated)+ # 4. AND the first parameter is NOT a special parameter+ special_param_names = set(SpecialActionParameters.model_fields.keys())try:is_pydantic = (parameters@@ -143,9 +145,8 @@ class Registry(Generic[Context]):and hasattr(parameters[0], 'annotation')and parameters[0].annotation != parameters[0].emptyand issubclass(parameters[0].annotation, BaseModel)- # Additional check: make sure the first parameter name suggests it's actually a pydantic model- # and parameters[0].name in ['params', 'param', 'model']- # or parameters[0].name.endswith('_model')+ # IMPORTANT: Exclude special parameters from being considered as pydantic models+ and parameters[0].name not in special_param_names)except (TypeError, AttributeError):is_pydantic = Falsediff --git a/browser_use/telemetry/service.py b/browser_use/telemetry/service.pyindex f06955212..f38b27cf1 100644--- a/browser_use/telemetry/service.py+++ b/browser_use/telemetry/service.py@@ -1,10 +1,10 @@import loggingimport os-import uuidfrom pathlib import Pathfrom dotenv import load_dotenvfrom posthog import Posthog+from uuid_extensions import uuid7strfrom browser_use.telemetry.views import BaseTelemetryEventfrom browser_use.utils import singleton@@ -113,7 +113,7 @@ class ProductTelemetry:if not os.path.exists(self.USER_ID_PATH):os.makedirs(os.path.dirname(self.USER_ID_PATH), exist_ok=True)with open(self.USER_ID_PATH, 'w') as f:- new_user_id = str(uuid.uuid4())+ new_user_id = uuid7str()f.write(new_user_id)self._curr_user_id = new_user_idelse:diff --git a/docs/customize/browser-settings.mdx b/docs/customize/browser-settings.mdxindex ddf260e5a..a2df2e8c3 100644--- a/docs/customize/browser-settings.mdx+++ b/docs/customize/browser-settings.mdx@@ -4,14 +4,23 @@ description: "Configure browser behavior and context settings"icon: "globe"----Browser Use uses playwright (or patchright) to manage its connection with a real browser.+Browser Use uses [playwright](https://playwright.dev/python/docs/api/class-browsertype#browser-type-launch-persistent-context) (or [patchright](https://github.com/Kaliiiiiiiiii-Vinyzu/patchright)) + CDP to manage its connection with a real browser.```pythonfrom browser_use import BrowserProfile, BrowserSession````-- `BrowserSession(**params)` is a connection to a running browser, it takes standard playwright kwargs in and connects to / launches a browser-- `BrowserProfile(**params)` is a collection of static config kwargs that can be passed to playwright to set up a browser+- `BrowserSession(**params)` is Browser Use's object that tracks a connection to a running browser. It holds:+ - the live playwright, browser, browser_context, and page objects for the tabs the agent and human viewer are focues on+ - the helper methods to launch and connect to remote and local browsers+ - methods to interact with the browser window, apply config needed by the Agent, and run the DOMService for element detection+- `BrowserProfile(**params)` is Browser Use's object that holds a collection of static config kwargs, to be used when starting a `BrowserSession`. It holds:+ - all the standard playwright kwargs+ - some extra browser configuration options we provide on top of playwright (e.g. `allowed_domains`, `window_position`, `profile_directory`, `deterministic_rendering`, etc.)+ - some kwargs used to configure Browser Use-specific features related to the browser (e.g. `highlight_elements`, `disable_security`, `cookies_file`)+- [`Browser`](https://playwright.dev/python/docs/api/class-browser): standard [playwright `Browser`](https://playwright.dev/python/docs/api/class-browser) object, refers to a running browser process (remote or local)+- [`BrowserContext`](https://playwright.dev/python/docs/api/class-browsercontext): standard [playwright `BrowserContext`](https://playwright.dev/python/docs/api/class-browsercontext) refers to a window in a live browser (incognito or with a profile)+- [`Page`](https://playwright.dev/python/docs/api/class-page): standard [playwright `Page`](https://playwright.dev/python/docs/api/class-page), the handle for one tab in the browser<Note>The new `BrowserSession` and `BrowserProfile` classes now accept all the same arguments as standard Playwright [launch_persistent_context](https://playwright.dev/python/docs/api/class-browsertype#browser-type-launch-persistent-context) API, giving you full control over browser settings.@@ -426,3 +435,13 @@ async def run_search():- BrowserUse custom settingsFor more details on Playwright's browser context options, see the [official documentation](https://playwright.dev/python/docs/api/class-browsertype#browser-type-launch-persistent-context).++---++In the future we may add support for more browser driving methods:+- Chrome Extension bridge w/ `chrome.debugger` permissions, via relaey server+- Computer-Use x,y coordinate-type mouse & keyboard interaction driver+- `adb` Android debugger for controlling remote a Android browser / apps+- [Appium](https://github.com/appium/appium)-style accessibility UX drivers+- WebDriver-BiDi to extend CDP+- In-page JS as an AI assistant you can provide users on your site (like Intercom or analytics)diff --git a/tests/test_action_registry_normalization.py b/tests/test_action_registry_normalization.pynew file mode 100644index 000000000..0bfc93faf--- /dev/null+++ b/tests/test_action_registry_normalization.py@@ -0,0 +1,450 @@+"""+Tests for action registration normalization and validation.+Ensures both Type 1 and Type 2 patterns work, and new validation rules are enforced.+"""++import pytest+from langchain_core.language_models.chat_models import BaseChatModel+from playwright.async_api import Page+from pydantic import BaseModel++from browser_use.agent.views import ActionResult+from browser_use.browser import BrowserSession+from browser_use.controller.registry.service import Registry+++class MockBrowserSession(BrowserSession):+ """Mock BrowserSession for testing"""++ def __init__(self):+ # Don't call super().__init__() to avoid real browser+ pass+++class MockPage:+ """Mock Page for testing"""++ pass+++class MockLLM(BaseChatModel):+ """Mock LLM for testing"""++ def _generate(self, *args, **kwargs):+ pass++ async def _agenerate(self, *args, **kwargs):+ pass+++# Test Type 1 Pattern (Pydantic model first)+class TestType1Pattern:+ def test_type1_with_param_model(self):+ """Type 1: action(params: Model, special_args...) should work"""+ registry = Registry()++ class ClickAction(BaseModel):+ index: int+ delay: float = 0.0++ @registry.action('Click element', param_model=ClickAction)+ async def click_element(params: ClickAction, browser_session: BrowserSession):+ return ActionResult(extracted_content=f'Clicked {params.index}')++ # Verify registration+ assert 'click_element' in registry.registry.actions+ action = registry.registry.actions['click_element']+ assert action.param_model == ClickAction++ # Verify decorated function signature (should be kwargs-only)+ import inspect++ sig = inspect.signature(click_element)+ params = list(sig.parameters.values())++ # Should have no positional-only or positional-or-keyword params+ for param in params:+ assert param.kind in (inspect.Parameter.KEYWORD_ONLY, inspect.Parameter.VAR_KEYWORD)++ def test_type1_with_multiple_special_params(self):+ """Type 1 with multiple special params should work"""+ registry = Registry()++ class ExtractAction(BaseModel):+ goal: str+ include_links: bool = False++ @registry.action('Extract content', param_model=ExtractAction)+ async def extract_content(+ params: ExtractAction, browser_session: BrowserSession, page: Page, page_extraction_llm: BaseChatModel+ ):+ return ActionResult(extracted_content=params.goal)++ assert 'extract_content' in registry.registry.actions+++# Test Type 2 Pattern (loose parameters)+class TestType2Pattern:+ def test_type2_simple_action(self):+ """Type 2: action(arg1, arg2, special_args...) should work"""+ registry = Registry()++ @registry.action('Fill field')+ async def fill_field(index: int, text: str, page: Page):+ return ActionResult(extracted_content=f'Filled {index} with {text}')++ # Verify registration+ assert 'fill_field' in registry.registry.actions+ action = registry.registry.actions['fill_field']++ # Should auto-generate param model+ assert action.param_model is not None+ assert hasattr(action.param_model, 'index')+ assert hasattr(action.param_model, 'text')++ def test_type2_with_defaults(self):+ """Type 2 with default values should preserve defaults"""+ registry = Registry()++ @registry.action('Scroll page')+ async def scroll_page(direction: str = 'down', amount: int = 100, browser_session: BrowserSession = None):+ return ActionResult(extracted_content=f'Scrolled {direction} by {amount}')++ action = registry.registry.actions['scroll_page']+ # Check that defaults are preserved in generated model+ schema = action.param_model.model_json_schema()+ assert schema['properties']['direction']['default'] == 'down'+ assert schema['properties']['amount']['default'] == 100++ def test_type2_no_action_params(self):+ """Type 2 with only special params should work"""+ registry = Registry()++ @registry.action('Save PDF')+ async def save_pdf(browser_session: BrowserSession, page: Page):+ return ActionResult(extracted_content='Saved PDF')++ action = registry.registry.actions['save_pdf']+ # Should have empty or minimal param model+ fields = action.param_model.model_fields+ assert len(fields) == 0 or all(f in ['title'] for f in fields)+++# Test validation rules+class TestValidationRules:+ def test_error_on_kwargs_in_original_function(self):+ """Should error if original function has kwargs"""+ registry = Registry()++ with pytest.raises(ValueError, match='kwargs.*not allowed'):++ @registry.action('Bad action')+ async def bad_action(index: int, page: Page, **kwargs):+ pass++ def test_error_on_special_param_name_with_wrong_type(self):+ """Should error if special param name used with wrong type"""+ registry = Registry()++ # Using 'page' with str type should error+ with pytest.raises(ValueError, match='conflicts with special argument.*page: Page'):++ @registry.action('Navigate')+ async def navigate_to_page(page: str, browser_session: BrowserSession):+ pass++ # Using 'browser_session' with wrong type should error+ with pytest.raises(ValueError, match='conflicts with special argument.*browser_session: BrowserSession'):++ @registry.action('Bad session')+ async def bad_session(browser_session: str):+ pass++ def test_special_params_must_match_type(self):+ """Special params with correct types should work"""+ registry = Registry()++ @registry.action('Good action')+ async def good_action(+ index: int,+ page: Page, # Correct type+ browser_session: BrowserSession, # Correct type+ ):+ return ActionResult()++ assert 'good_action' in registry.registry.actions+++# Test decorated function behavior+class TestDecoratedFunctionBehavior:+ def test_decorated_function_only_accepts_kwargs(self):+ """Decorated functions should only accept kwargs, no positional args"""+ registry = Registry()++ @registry.action('Click')+ async def click(index: int, browser_session: BrowserSession):+ return ActionResult()++ # Should raise error when called with positional args+ with pytest.raises(TypeError, match='positional arguments'):+ import asyncio++ asyncio.run(click(5, MockBrowserSession()))++ def test_decorated_function_accepts_params_model(self):+ """Decorated function should accept params as model"""+ registry = Registry()++ @registry.action('Input text')+ async def input_text(index: int, text: str, browser_session: BrowserSession):+ return ActionResult(extracted_content=f'{index}:{text}')++ # Get the generated param model class+ action = registry.registry.actions['input_text']+ ParamsModel = action.param_model++ # Should work with params model+ import asyncio++ result = asyncio.run(input_text(params=ParamsModel(index=5, text='hello'), browser_session=MockBrowserSession()))+ assert result.extracted_content == '5:hello'++ def test_decorated_function_ignores_extra_kwargs(self):+ """Decorated function should ignore extra kwargs for easy unpacking"""+ registry = Registry()++ @registry.action('Simple action')+ async def simple_action(value: int, page: Page):+ return ActionResult(extracted_content=str(value))++ # Should work even with extra kwargs+ special_context = {+ 'page': MockPage(),+ 'browser_session': MockBrowserSession(),+ 'page_extraction_llm': MockLLM(),+ 'context': {'extra': 'data'},+ 'unknown_param': 'ignored',+ }++ action = registry.registry.actions['simple_action']+ ParamsModel = action.param_model++ import asyncio++ result = asyncio.run(simple_action(params=ParamsModel(value=42), **special_context))+ assert result.extracted_content == '42'+++# Test params model generation+class TestParamsModelGeneration:+ def test_generates_model_from_non_special_args(self):+ """Should generate param model from non-special positional args"""+ registry = Registry()++ @registry.action('Complex action')+ async def complex_action(+ query: str, max_results: int, include_images: bool = True, page: Page = None, browser_session: BrowserSession = None+ ):+ return ActionResult()++ action = registry.registry.actions['complex_action']+ model_fields = action.param_model.model_fields++ # Should include only non-special params+ assert 'query' in model_fields+ assert 'max_results' in model_fields+ assert 'include_images' in model_fields++ # Should NOT include special params+ assert 'page' not in model_fields+ assert 'browser_session' not in model_fields++ def test_preserves_type_annotations(self):+ """Generated model should preserve type annotations"""+ registry = Registry()++ @registry.action('Typed action')+ async def typed_action(+ count: int, rate: float, enabled: bool, name: str | None = None, browser_session: BrowserSession = None+ ):+ return ActionResult()++ action = registry.registry.actions['typed_action']+ schema = action.param_model.model_json_schema()++ # Check types are preserved+ assert schema['properties']['count']['type'] == 'integer'+ assert schema['properties']['rate']['type'] == 'number'+ assert schema['properties']['enabled']['type'] == 'boolean'+ # Optional should allow null+ assert 'null' in schema['properties']['name']['anyOf'][1]['type']+++# Test error messages+class TestErrorMessages:+ def test_clear_error_for_kwargs(self):+ """Error message for kwargs should be clear"""+ registry = Registry()++ try:++ @registry.action('Bad')+ async def bad(x: int, **kwargs):+ pass++ pytest.fail('Should have raised ValueError')+ except ValueError as e:+ assert 'kwargs' in str(e).lower()+ assert 'not allowed' in str(e).lower()+ assert 'bad' in str(e).lower() # Should mention function name++ def test_clear_error_for_param_conflicts(self):+ """Error message for param conflicts should be helpful"""+ registry = Registry()++ try:++ @registry.action('Bad')+ async def bad(page: str):+ pass++ pytest.fail('Should have raised ValueError')+ except ValueError as e:+ error_msg = str(e)+ assert 'page: str' in error_msg+ assert 'conflicts' in error_msg+ assert 'page: Page' in error_msg # Show expected type+ assert 'bad' in error_msg.lower() # Show function name+++# Integration test with execute_action+class TestExecuteAction:+ @pytest.mark.asyncio+ async def test_execute_with_special_params(self):+ """execute_action should work with new normalized functions"""+ registry = Registry()++ @registry.action('Test action')+ async def test_action(value: str, multiplier: int = 2, browser_session: BrowserSession = None):+ return ActionResult(extracted_content=f'{value * multiplier}')++ # Execute the action+ result = await registry.execute_action(+ action_name='test_action', params={'value': 'hi', 'multiplier': 3}, browser_session=MockBrowserSession()+ )++ assert result.extracted_content == 'hihihi'++ @pytest.mark.asyncio+ async def test_execute_without_params(self):+ """execute_action should work for actions with no params"""+ registry = Registry()++ @registry.action('No params')+ async def no_params(browser_session: BrowserSession):+ return ActionResult(extracted_content='done')++ result = await registry.execute_action(action_name='no_params', params={}, browser_session=MockBrowserSession())++ assert result.extracted_content == 'done'+++# Test calling decorated actions from within other actions+class TestInterActionCalls:+ @pytest.mark.asyncio+ async def test_action_calling_another_action(self):+ """Actions should be able to call other decorated actions using kwargs"""+ registry = Registry()++ @registry.action('Helper action')+ async def helper_action(value: int, browser_session: BrowserSession):+ return ActionResult(extracted_content=f'Helper: {value}')++ @registry.action('Main action')+ async def main_action(multiplier: int, browser_session: BrowserSession):+ # Get the param model for helper action+ helper_model = registry.registry.actions['helper_action'].param_model++ # Call helper action with kwargs+ result = await helper_action(params=helper_model(value=10 * multiplier), browser_session=browser_session)+ return ActionResult(extracted_content=f'Main -> {result.extracted_content}')++ # Execute main action+ result = await registry.execute_action(+ action_name='main_action', params={'multiplier': 3}, browser_session=MockBrowserSession()+ )++ assert result.extracted_content == 'Main -> Helper: 30'++ @pytest.mark.asyncio+ async def test_action_unpacking_special_context(self):+ """Actions should be able to unpack all special context when calling others"""+ registry = Registry()++ @registry.action('Inner action')+ async def inner_action(text: str, page: Page, browser_session: BrowserSession, page_extraction_llm: BaseChatModel):+ return ActionResult(extracted_content=f'Inner: {text}, has page: {page is not None}')++ @registry.action('Outer action')+ async def outer_action(browser_session: BrowserSession, page: Page):+ # Create special context dict+ special_context = {+ 'browser_session': browser_session,+ 'page': page,+ 'page_extraction_llm': MockLLM(),+ 'context': None, # Extra param that will be ignored+ 'has_sensitive_data': False, # Also ignored+ }++ inner_model = registry.registry.actions['inner_action'].param_model++ # Should be able to unpack entire context+ result = await inner_action(params=inner_model(text='Hello'), **special_context)+ return result++ result = await registry.execute_action(+ action_name='outer_action', params={}, browser_session=MockBrowserSession(), page=MockPage()+ )++ assert 'Inner: Hello, has page: True' in result.extracted_content+++# Test mixed ordering of parameters+class TestParameterOrdering:+ def test_mixed_param_ordering(self):+ """Should handle any ordering of action params and special params"""+ registry = Registry()++ # Special params mixed throughout+ @registry.action('Mixed params')+ async def mixed_action(+ first: str,+ browser_session: BrowserSession,+ second: int,+ page: Page,+ third: bool = True,+ page_extraction_llm: BaseChatModel = None,+ ):+ return ActionResult()++ action = registry.registry.actions['mixed_action']+ model_fields = action.param_model.model_fields++ # Only action params in model+ assert set(model_fields.keys()) == {'first', 'second', 'third'}+ assert model_fields['third'].default is True++ def test_all_params_at_end(self):+ """Should work with all action params at the end"""+ registry = Registry()++ @registry.action('Params at end')+ async def params_at_end(browser_session: BrowserSession, page: Page, query: str, limit: int = 10):+ return ActionResult()++ action = registry.registry.actions['params_at_end']+ assert set(action.param_model.model_fields.keys()) == {'query', 'limit'}+++if __name__ == '__main__':+ pytest.main([__file__, '-v'])