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
Remove output_schema handling from Agent and judge system
Cursor Agentb2baabd9342f
diff --git a/browser_use/agent/service.py b/browser_use/agent/service.pyindex 9e7f1e2cc..32ef95d5b 100644--- a/browser_use/agent/service.py+++ b/browser_use/agent/service.py@@ -166,7 +166,6 @@ class Agent(Generic[Context]):],max_actions_per_step: int = 10,use_thinking: bool = True,- output_schema: dict[str, Any] | None = None,page_extraction_llm: BaseChatModel | None = None,planner_llm: BaseChatModel | None = None,planner_interval: int = 1, # Run planner every N steps@@ -234,7 +233,6 @@ class Agent(Generic[Context]):extend_planner_system_message=extend_planner_system_message,use_thinking=use_thinking,calculate_cost=calculate_cost,- output_schema=output_schema,)# Token cost service@@ -919,30 +917,7 @@ class Agent(Generic[Context]):# Determine output format based on whether we have a structured output schemaoutput_format = self.AgentOutput-- # If we have a structured output schema, we need to modify the system message- # to include instructions for the structured output- modified_messages = input_messages- if self.settings.output_schema:- self.logger.info(f'π― Using structured output schema for final result')-- # Add structured output instructions to the system message or as a user message- schema_instruction = f"""-IMPORTANT: When you use the 'done' action and mark success=True, you MUST include the final result in the exact JSON format specified by this schema:--Output Schema: {json.dumps(self.settings.output_schema, indent=2)}--The final result should be a valid JSON object that conforms to this schema. Include this structured JSON in the 'text' field of the 'done' action.-"""-- # Add the schema instruction as a user message before the last system message- from browser_use.llm.messages import UserMessage- schema_message = UserMessage(content=schema_instruction)-- # Insert the schema message before any existing messages- modified_messages = [schema_message] + input_messages-- response = await self.llm.ainvoke(modified_messages, output_format=output_format)+ response = await self.llm.ainvoke(input_messages, output_format=output_format)parsed = response.completion# cut the number of actions to max_actions_per_step if neededdiff --git a/browser_use/agent/views.py b/browser_use/agent/views.pyindex f0aa11db4..2caf88d46 100644--- a/browser_use/agent/views.py+++ b/browser_use/agent/views.py@@ -52,7 +52,6 @@ class AgentSettings(BaseModel):]max_actions_per_step: int = 10use_thinking: bool = True- output_schema: dict[str, Any] | None = None # JSON schema for structured outputpage_extraction_llm: BaseChatModel | None = Noneplanner_llm: BaseChatModel | None = Nonediff --git a/eval/judge_system.py b/eval/judge_system.pyindex 49d1fc12c..a15946903 100644--- a/eval/judge_system.py+++ b/eval/judge_system.py@@ -253,7 +253,6 @@ async def comprehensive_judge(screenshot_paths: list[str],model: BaseChatModel,max_images: int = 10,- output_schema: dict[str, Any] | None = None,) -> JudgeResult:"""Comprehensive judge that evaluates browser-use agent runs with detailed structured feedback.@@ -266,7 +265,6 @@ async def comprehensive_judge(screenshot_paths: List of screenshot file paths from executionmodel: The LLM model to use for evaluationmax_images: Maximum number of images to include in evaluation- output_schema: Structured output schema for validation"""# Prepare inputs with length limits@@ -289,22 +287,7 @@ async def comprehensive_judge(# Build error categories dynamically from enumerror_categories_text = ', '.join([category.value for category in ErrorCategory])- # Add structured output validation section if schema is provided- structured_output_section = ""- if output_schema:- structured_output_section = f"""-**STRUCTURED OUTPUT VALIDATION:**-This task required structured output according to the following schema:-{json.dumps(output_schema, indent=2)}--When evaluating task completion, you MUST verify that:-1. The final result contains properly formatted JSON that conforms to the required schema-2. All required fields are present and have the correct data types-3. The structured data is meaningful and accurate for the task--If the output schema was provided but the final result doesn't contain valid structured JSON conforming to the schema, this significantly impacts the task satisfaction score.-"""# Construct the evaluation promptsystem_prompt = f"""You are an expert judge evaluating browser-use agent performance.@@ -342,7 +325,7 @@ The browser-use agent operates in iterative loops receiving structured input:- Calls done action only when task complete or impossible to continue - not too early- If the agent needs to repeat the same sub task multiple times & has a good trajectory, but hits the max step limit its still very good and can pass the evaluation- Analyse the screenshots. Each interactive element should have exactly one color bounding box.-{structured_output_section}+**EVALUATION CRITERIA:**1. **Task Satisfaction**: Understand the user intent - Is the user satisfied with the final result? - This is the most important criterion.2. **Tool Usage**: How well did the tools work? -Do they work as expected?@@ -563,7 +546,6 @@ async def judge_with_retry(model: BaseChatModel,max_retries: int = 3,max_images: int = 10,- output_schema: dict[str, Any] | None = None,) -> JudgeResult:"""Judge with retry logic for robustness.@@ -577,7 +559,6 @@ async def judge_with_retry(model: The LLM model to use for evaluationmax_retries: Maximum number of retry attemptsmax_images: Maximum number of images to include in evaluation- output_schema: Structured output schema for validation"""for attempt in range(max_retries):try:@@ -589,7 +570,6 @@ async def judge_with_retry(screenshot_paths,model,max_images,- output_schema,)except Exception as e:if attempt == max_retries - 1:@@ -677,12 +657,7 @@ async def evaluate_task_with_comprehensive_judge(task_folder: Path, model: BaseClast_message = result_data.get('last_message', '')screenshot_paths = result_data.get('screenshot_paths', [])- # Extract output schema if available from task data- output_schema = None- task_data = result_data.get('task_data', {})- if isinstance(task_data, dict) and 'output_schema' in task_data:- output_schema = task_data['output_schema']- logger.info(f'π― Found output schema for task {task_folder.name}: {output_schema}')+# Run comprehensive evaluationjudge_result = await judge_with_retry(@@ -693,7 +668,6 @@ async def evaluate_task_with_comprehensive_judge(task_folder: Path, model: BaseCscreenshot_paths=screenshot_paths,model=model,max_images=max_images,- output_schema=output_schema, # Pass the output schema)# Convert to dict for storagediff --git a/eval/service.py b/eval/service.pyindex 8dac82af6..afe8570f5 100644--- a/eval/service.py+++ b/eval/service.py@@ -1331,11 +1331,7 @@ async def run_agent_with_browser("Please remove the 'enable_memory' parameter.")- # Extract output_schema from task if available- output_schema = None- if hasattr(task, 'output_schema') and task.output_schema:- output_schema = task.output_schema- logger.info(f'π― Task {task.task_id}: Using structured output schema: {output_schema}')+agent = Agent(task=task.confirmed_task,@@ -1347,7 +1343,7 @@ async def run_agent_with_browser(validate_output=validate_output,planner_llm=planner_llm,planner_interval=planner_interval,- output_schema=output_schema, # Pass the structured output schema+source='eval_platform',)# get last messagediff --git a/structured_output_implementation_analysis.md b/structured_output_implementation_analysis.mdindex 15ae7edcc..7073278ac 100644--- a/structured_output_implementation_analysis.md+++ b/structured_output_implementation_analysis.md@@ -2,13 +2,13 @@## Overview-The structured output functionality has been successfully implemented throughout the browser-use evaluation system, enabling tasks to specify JSON schemas for agent outputs and having the judge system validate conformance to these schemas.+The structured output functionality implementation in the browser-use evaluation system works by converting JSON schemas from task datasets into Pydantic models that are passed to the Controller. The Controller then creates a custom `done` action that enforces the structured output format. The judge system evaluates the final structured response without needing the original schema.-## Implementation Components Verified+## Correct Implementation Flow### 1. Task Class Enhancement (`eval/service.py`)-β **Status: Fully Implemented**+β **Status: Correctly Implemented**- Added `output_schema` as an optional field in the Task class `__init__` method (line 1074)- Properly included in known_fields set for clean handling (line 1077)@@ -19,147 +19,215 @@ self.output_schema = kwargs.get('output_schema', None) # Add structured outputknown_fields = {'website', 'reference_length', 'level', 'cluster_id', 'login_cookie', 'login_type', 'category', 'output_schema'}```-### 2. Agent Configuration Updates+### 2. Controller Integration (Correct Approach)-β **Status: Fully Implemented**+β **Status: Should be implemented in `create_controller` function**-**AgentSettings Class (`browser_use/agent/views.py`):**-- Added `output_schema: dict[str, Any] | None = None` field (line 54)+The correct implementation should:+- Extract `output_schema` from task in `run_agent_with_browser`+- Convert JSON schema to a Pydantic model+- Pass the model to `Controller(output_model=PydanticModel)`+- Controller automatically creates a structured `done` action-**Agent Constructor (`browser_use/agent/service.py`):**-- Added `output_schema` parameter to Agent `__init__` method (line 168)-- Properly passed to AgentSettings during initialization (line 236)+**Example of correct pattern:**+```python+# From examples/features/custom_output.py+class Posts(BaseModel):+ posts: list[Post]-### 3. Agent Runtime Integration+controller = Controller(output_model=Posts)+```-β **Status: Fully Implemented**+### 3. How Controller Handles Structured Output-**Agent Creation (`eval/service.py`):**-- Extracts output_schema from task in `run_agent_with_browser` function (lines 1333-1337)-- Passes schema to Agent constructor (line 1349)-- Includes informative logging when schema is detected+β **Status: Already implemented in Controller class**-**LLM Prompt Injection (`browser_use/agent/service.py`):**-- Enhanced `get_next_action` method to inject structured output instructions (lines 925-944)-- Adds schema instructions as UserMessage when output_schema exists-- Properly formats JSON schema in instructions for the LLM+When `output_model` is provided to Controller:+- Creates `ExtendedOutputModel` with success parameter + data field+- Registers custom `done` action that enforces the structure+- Returns JSON serialized structured data in `extracted_content````python-if self.settings.output_schema:- self.logger.info(f'π― Using structured output schema for final result')-- schema_instruction = f"""-IMPORTANT: When you use the 'done' action and mark success=True, you MUST include the final result in the exact JSON format specified by this schema:--Output Schema: {json.dumps(self.settings.output_schema, indent=2)}--The final result should be a valid JSON object that conforms to this schema. Include this structured JSON in the 'text' field of the 'done' action.-"""+# From browser_use/controller/service.py lines 77-104+if output_model is not None:+ class ExtendedOutputModel(BaseModel):+ success: bool = True+ data: output_model++ @self.registry.action(+ 'Complete task - with return text and if the task is finished (success=True)...',+ param_model=ExtendedOutputModel,+ )+ async def done(params: ExtendedOutputModel):+ output_dict = params.data.model_dump()+ return ActionResult(+ is_done=True,+ success=params.success,+ extracted_content=json.dumps(output_dict),+ long_term_memory=f'Task completed. Success Status: {params.success}',+ )```-### 4. Judge System Enhancement (`eval/judge_system.py`)--β **Status: Fully Implemented**+### 4. Judge System Evaluation-**comprehensive_judge Function:**-- Added `output_schema` parameter (line 255)-- Enhanced system prompt with structured output validation section (lines 293-306)-- Includes detailed schema validation requirements+β **Status: Correct - No schema needed**-**judge_with_retry Function:**-- Added `output_schema` parameter (line 565)-- Properly passes schema through to comprehensive_judge (line 591)+The judge system correctly evaluates the final structured response without needing the original schema:+- Receives the final JSON output in `final_result`+- Evaluates whether the output is properly structured and meaningful+- No need to pass `output_schema` to judge functions-**Task Evaluation Integration:**-- `evaluate_task_with_comprehensive_judge` extracts output_schema from task_data (lines 680-684)-- Passes schema to judge_with_retry function (line 695)+## Required Changes to Complete Implementation-### 5. Structured Output Validation Logic+### 1. Modify `create_controller` function-β **Status: Comprehensive Implementation**+```python+def create_controller(use_serp: bool = False, output_schema: dict | None = None):+ """Create a controller, optionally with SERP search and structured output"""+ if output_schema:+ # Convert JSON schema to Pydantic model+ output_model = create_pydantic_model_from_schema(output_schema)+ controller = Controller(output_model=output_model)+ else:+ controller = Controller()++ if use_serp:+ # Add SERP search action to existing controller+ add_serp_search_to_controller(controller)++ return controller+```-The judge system prompt includes robust validation criteria:+### 2. Update `run_agent_with_browser` function+```python+async def run_agent_with_browser(...):+ # Extract output_schema from task if available+ output_schema = None+ if hasattr(task, 'output_schema') and task.output_schema:+ output_schema = task.output_schema+ logger.info(f'π― Task {task.task_id}: Using structured output schema')++ # Create controller with structured output support+ controller = create_controller(use_serp=use_serp, output_schema=output_schema)++ agent = Agent(+ task=task.confirmed_task,+ llm=llm,+ controller=controller, # Pass controller with structured output+ # ... other parameters+ )```-**STRUCTURED OUTPUT VALIDATION:**-This task required structured output according to the following schema:-{json schema}-When evaluating task completion, you MUST verify that:-1. The final result contains properly formatted JSON that conforms to the required schema-2. All required fields are present and have the correct data types-3. The structured data is meaningful and accurate for the task+### 3. Schema to Pydantic Conversion Utility-If the output schema was provided but the final result doesn't contain valid structured JSON conforming to the schema, this significantly impacts the task satisfaction score.+Need to implement a utility function to convert JSON schemas to Pydantic models:++```python+def create_pydantic_model_from_schema(schema: dict) -> type[BaseModel]:+ """Convert JSON schema to Pydantic model"""+ # Implementation needed to dynamically create Pydantic models from JSON schemas+ pass```-### 6. Bug Fixes Verified+## Incorrect Previous Implementation++### β Removed: Agent-level output_schema handling+- `output_schema` parameter in Agent constructor+- `output_schema` in AgentSettings+- Schema injection in `get_next_action` method-β **multi_act Method (`browser_use/agent/service.py`):**-- The code for checking new elements after page changes is present and functioning (lines 1283-1386)-- Logic correctly handles element detection and breaks execution when new elements appear-- No missing code sections found+### β Removed: Judge system schema validation+- `output_schema` parameter in judge functions+- Schema validation prompts in judge system+- Schema extraction in evaluation functions-## Technical Flow Analysis+## Technical Flow (Corrected)### End-to-End Process:-1. **Dataset Loading**: JSON datasets can include `output_schema` field in task definitions-2. **Task Creation**: Task class properly loads and stores the schema as an attribute-3. **Agent Initialization**: Schema is extracted from task and passed to Agent constructor-4. **Agent Settings**: Schema is stored in AgentSettings for runtime access-5. **Runtime Execution**: When schema exists, agent injects structured output instructions into LLM prompts-6. **Judge Evaluation**: Judge system receives schema and validates final output against it-7. **Scoring Impact**: Schema validation affects task satisfaction scoring and pass/fail determination+1. **Dataset Loading**: JSON datasets include `output_schema` field in task definitions+2. **Task Creation**: Task class loads and stores the schema as an attribute+3. **Controller Creation**: Schema is converted to Pydantic model and passed to Controller+4. **Controller Setup**: Controller creates custom `done` action with structured output enforcement+5. **Agent Execution**: Agent uses controller with structured `done` action+6. **Output Generation**: Agent calls structured `done` action, returns JSON in `extracted_content`+7. **Judge Evaluation**: Judge evaluates final JSON output for quality and correctness## Key Features### β Backward Compatibility-- All changes are optional and don't break existing functionality-- Tasks without output_schema continue to work normally+- Tasks without output_schema continue to work with standard `done` action+- No breaking changes to existing functionality-### β Comprehensive Validation-- Judge system validates JSON format compliance-- Checks for required fields and correct data types-- Evaluates meaningfulness and accuracy of structured data+### β Proper Separation of Concerns+- Controller handles output structure enforcement+- Agent focuses on task execution+- Judge evaluates final output quality-### β Proper Error Handling-- Graceful handling when schemas are malformed or missing-- Fallback behavior when validation fails+### β Leverages Existing Patterns+- Uses established Controller `output_model` pattern+- Follows existing examples in codebase+- Maintains consistency with browser-use architecture-### β Logging and Debugging-- Clear logging when structured output schemas are detected-- Informative messages throughout the pipeline+## Implementation Status-## Code Quality Assessment+### β Completed Components:+- Task class enhancement+- Controller structured output handling (already exists)+- Judge system evaluation (no changes needed)-### Strengths:-- Clean integration with existing codebase-- Comprehensive error handling-- Good separation of concerns-- Extensive logging for debugging+### π Required Components:+- Modify `create_controller` function to accept `output_schema`+- Update `run_agent_with_browser` to pass schema to controller+- Implement JSON schema to Pydantic model conversion utility-### Areas of Excellence:-- The implementation follows the existing code patterns-- No breaking changes to existing functionality-- Comprehensive validation in the judge system-- Clear documentation in code comments+### β Removed Incorrect Components:+- **Agent-level output_schema handling**: Removed `output_schema` parameter from Agent constructor and AgentSettings+- **Judge system schema validation**: Removed `output_schema` parameters from judge functions and schema validation prompts+- **Schema injection in LLM prompts**: Removed schema instruction injection in `get_next_action` method-## Verification Status+### β Correctly Implemented Components:+- **Task class enhancement**: `output_schema` field properly added to Task class in `eval/service.py`+- **Controller structured output handling**: Already exists in Controller class via `output_model` parameter+- **Judge system evaluation**: Correctly evaluates final structured response without needing schema-All major components have been successfully implemented and verified:+### π Required Implementation:+The correct implementation requires:-- β Task class enhancement-- β Agent configuration updates-- β Agent runtime integration-- β Judge system enhancement-- β Structured output validation-- β Bug fixes (multi_act method)-- β End-to-end flow functionality-- β Backward compatibility maintained+1. **Convert JSON Schema to Pydantic Model**: Create utility function to dynamically generate Pydantic models from JSON schemas+2. **Update `create_controller` function**: Accept `output_schema` parameter and convert to Pydantic model for Controller+3. **Update `run_agent_with_browser`**: Extract schema from task and pass to controller creation-## Conclusion+### Implementation Plan:-The structured output functionality has been comprehensively implemented across all required components of the browser-use evaluation system. The implementation is robust, well-integrated, and maintains backward compatibility while providing powerful new capabilities for structured task evaluation.+```python+# 1. Add schema conversion utility+def create_pydantic_model_from_schema(schema: dict) -> type[BaseModel]:+ """Convert JSON schema to Pydantic model dynamically"""+ # Implementation needed++# 2. Update create_controller function+def create_controller(use_serp: bool = False, output_schema: dict | None = None):+ if output_schema:+ output_model = create_pydantic_model_from_schema(output_schema)+ controller = Controller(output_model=output_model)+ else:+ controller = Controller()++ if use_serp:+ add_serp_search_to_controller(controller)+ return controller++# 3. Update run_agent_with_browser+async def run_agent_with_browser(...):+ output_schema = getattr(task, 'output_schema', None)+ controller = create_controller(use_serp=use_serp, output_schema=output_schema)+ agent = Agent(task=task.confirmed_task, llm=llm, controller=controller, ...)+```++The main remaining work is implementing the JSON schema to Pydantic model conversion utility, which is the core technical challenge for this feature.++## Conclusion-The code is production-ready and follows established patterns in the codebase. All critical paths have been implemented with proper error handling and logging.\ No newline at end of file+The structured output functionality should leverage the existing Controller `output_model` pattern rather than implementing custom schema handling in the Agent or Judge systems. This approach is cleaner, follows established patterns, and maintains proper separation of concerns. The main work required is converting JSON schemas to Pydantic models and updating the controller creation flow.\ No newline at end of file