Skip to content

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

Example how to use validator

magmuellerd46ce11fc5be
diff --git a/browser_use/agent/service.py b/browser_use/agent/service.py
index 2d63f5d87..5cf1750e1 100644
--- a/browser_use/agent/service.py
+++ b/browser_use/agent/service.py
@@ -325,8 +325,9 @@ class Agent:
parsed: ValidationResult = response['parsed']
is_valid = parsed.is_valid
if not is_valid:
- logger.warning(f'❌ Validator decision: {parsed.reason}')
+ logger.info(f'❌ Validator decision: {parsed.reason}')
msg = f'The ouput is not yet correct. {parsed.reason}.'
self._last_result = ActionResult(extracted_content=msg, include_in_memory=True)
-
+ else:
+ logger.info(f'✅ Validator decision: {parsed.reason}')
return is_valid
diff --git a/examples/validate_output.py b/examples/validate_output.py
new file mode 100644
index 000000000..11a2c185c
--- /dev/null
+++ b/examples/validate_output.py
@@ -0,0 +1,57 @@
+"""
+Find and apply to jobs.
+
+@dev You need to add OPENAI_API_KEY to your environment variables.
+
+Also you have to install PyPDF2 to read pdf files: pip install PyPDF2
+"""
+
+import csv
+import os
+import sys
+from pathlib import Path
+
+from PyPDF2 import PdfReader
+
+sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
+
+import asyncio
+from typing import List, Optional
+
+from dotenv import load_dotenv
+from langchain_openai import ChatOpenAI
+from pydantic import BaseModel
+
+from browser_use import ActionResult, Agent, Browser, Controller
+
+load_dotenv()
+
+controller = Controller(keep_open=True)
+
+
+class DoneResult(BaseModel):
+ title: str
+ comments: str
+ hours_since_start: int
+
+
+# we overwrite done() in this example to demonstrate the validator
[email protected]('Done with task', param_model=DoneResult)
+async def done(params: DoneResult):
+ result = ActionResult(is_done=True, extracted_content=params.model_dump_json())
+ print(result)
+ # NOTE: this is clearly wrong - to demonstrate the validator
+ # return result
+ return 'blablabla'
+
+
+async def main():
+ task = 'Go to hackernews hn and give me the top 1 post'
+ model = ChatOpenAI(model='gpt-4o')
+ agent = Agent(task=task, llm=model, controller=controller, validate_output=True)
+
+ await agent.run()
+
+
+if __name__ == '__main__':
+ asyncio.run(main())