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
Allow custome output
magmuellerac82f2cbd1a0
diff --git a/browser_use/controller/service.py b/browser_use/controller/service.pyindex c925f5f30..62a0adaec 100644--- a/browser_use/controller/service.py+++ b/browser_use/controller/service.py@@ -1,9 +1,11 @@import asyncioimport jsonimport logging+from typing import Optional, Typefrom main_content_extractor import MainContentExtractorfrom playwright.async_api import Page+from pydantic import BaseModelfrom browser_use.agent.views import ActionModel, ActionResultfrom browser_use.browser.context import BrowserContext@@ -29,14 +31,27 @@ class Controller:def __init__(self,exclude_actions: list[str] = [],+ output_model: Optional[Type[BaseModel]] = None,):self.exclude_actions = exclude_actions+ self.output_model = output_modelself.registry = Registry(exclude_actions)self._register_default_actions()def _register_default_actions(self):"""Register all default browser actions"""+ if self.output_model is not None:++ @self.registry.action('Complete task', param_model=self.output_model)+ async def done(params: BaseModel):+ return ActionResult(is_done=True, extracted_content=params.model_dump_json())+ else:++ @self.registry.action('Complete task', param_model=DoneAction)+ async def done(params: DoneAction):+ return ActionResult(is_done=True, extracted_content=params.text)+# Basic Navigation Actions@self.registry.action('Search Google in the current tab',@@ -157,10 +172,6 @@ class Controller:logger.info(msg)return ActionResult(extracted_content=msg)- @self.registry.action('Complete task', param_model=DoneAction)- async def done(params: DoneAction):- return ActionResult(is_done=True, extracted_content=params.text)-@self.registry.action('Scroll down the page by pixel amount - if no amount is specified, scroll down one page',param_model=ScrollAction,diff --git a/docs/customize/langchain-models.mdx b/docs/customize/langchain-models.mdxindex bc4c5055a..23cd4c77f 100644--- a/docs/customize/langchain-models.mdx+++ b/docs/customize/langchain-models.mdx@@ -163,7 +163,7 @@ DEEPSEEK_API_KEY=Many users asked for local models. Here they are.1. Download Ollama from [here](https://ollama.ai/download)-2. Run `ollama pull model_name`. You can pick a model which supports tool-calling from [here](https://ollama.com/search?c=tools)+2. Run `ollama pull model_name`. Pick a model which supports tool-calling from [here](https://ollama.com/search?c=tools)3. Run `ollama start````pythondiff --git a/docs/customize/system-prompt.mdx b/docs/customize/system-prompt.mdxindex a3aa20c31..be2aeee8d 100644--- a/docs/customize/system-prompt.mdx+++ b/docs/customize/system-prompt.mdx@@ -55,7 +55,7 @@ Apply your custom system prompt when creating an agent:from langchain_openai import ChatOpenAI# Initialize the model-model = ChatOpenAI(model='gpt-4')+model = ChatOpenAI(model='gpt-4o')# Create agent with custom system promptagent = Agent(diff --git a/examples/custom_output.py b/examples/custom_output.pyindex bc6b50901..cf76d9dcc 100644--- a/examples/custom_output.py+++ b/examples/custom_output.py@@ -6,6 +6,7 @@ Show how to use custom outputs.import osimport sys+from typing import Listsys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))@@ -19,24 +20,23 @@ from browser_use import ActionResult, Agent, Controllerload_dotenv()-controller = Controller()--class DoneResult(BaseModel):+class Post(BaseModel):post_title: strpost_url: strnum_comments: inthours_since_post: int[email protected]('Done with task', param_model=DoneResult)-async def done(params: DoneResult):- result = ActionResult(is_done=True, extracted_content=params.model_dump_json())- return result+class Posts(BaseModel):+ posts: List[Post]+++controller = Controller(output_model=Posts)async def main():- task = 'Go to hackernews show hn and give me the number 1 post in the list'+ task = 'Go to hackernews show hn and give me the first 5 posts'model = ChatOpenAI(model='gpt-4o')agent = Agent(task=task, llm=model, controller=controller)@@ -44,12 +44,16 @@ async def main():result = history.final_result()if result:- parsed = DoneResult.model_validate_json(result)- print('--------------------------------')- print(f'Title: {parsed.post_title}')- print(f'URL: {parsed.post_url}')- print(f'Comments: {parsed.num_comments}')- print(f'Hours since post: {parsed.hours_since_post}')+ parsed: Posts = Posts.model_validate_json(result)++ for post in parsed.posts:+ print('\n--------------------------------')+ print(f'Title: {post.post_title}')+ print(f'URL: {post.post_url}')+ print(f'Comments: {post.num_comments}')+ print(f'Hours since post: {post.hours_since_post}')+ else:+ print('No result')if __name__ == '__main__':