Skip to content

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

Allow custome output

magmuellerac82f2cbd1a0
diff --git a/browser_use/controller/service.py b/browser_use/controller/service.py
index c925f5f30..62a0adaec 100644
--- a/browser_use/controller/service.py
+++ b/browser_use/controller/service.py
@@ -1,9 +1,11 @@
import asyncio
import json
import logging
+from typing import Optional, Type
from main_content_extractor import MainContentExtractor
from playwright.async_api import Page
+from pydantic import BaseModel
from browser_use.agent.views import ActionModel, ActionResult
from 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_model
self.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.mdx
index 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`
```python
diff --git a/docs/customize/system-prompt.mdx b/docs/customize/system-prompt.mdx
index 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 prompt
agent = Agent(
diff --git a/examples/custom_output.py b/examples/custom_output.py
index bc6b50901..cf76d9dcc 100644
--- a/examples/custom_output.py
+++ b/examples/custom_output.py
@@ -6,6 +6,7 @@ Show how to use custom outputs.
import os
import sys
+from typing import List
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
@@ -19,24 +20,23 @@ from browser_use import ActionResult, Agent, Controller
load_dotenv()
-controller = Controller()
-
-class DoneResult(BaseModel):
+class Post(BaseModel):
post_title: str
post_url: str
num_comments: int
hours_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__':