Skip to content

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

Add output to task

Magnus Müller02574dec45c0
diff --git a/browser_use/agent/service.py b/browser_use/agent/service.py
index b50d5f893..b47ac6331 100644
--- a/browser_use/agent/service.py
+++ b/browser_use/agent/service.py
@@ -239,7 +239,7 @@ class Agent(Generic[Context, AgentStructuredOutput]):
self.available_file_paths = available_file_paths
# Core components
- self.task = task
+ self.task = self._enhance_task_with_schema(task, output_model_schema)
self.llm = llm
self.directly_open_url = directly_open_url
self.include_recent_events = include_recent_events
@@ -444,6 +444,24 @@ class Agent(Generic[Context, AgentStructuredOutput]):
self._external_pause_event = asyncio.Event()
self._external_pause_event.set()
+ def _enhance_task_with_schema(self, task: str, output_model_schema: type[AgentStructuredOutput] | None) -> str:
+ """Enhance task description with output schema information if provided."""
+ if output_model_schema is None:
+ return task
+
+ try:
+ schema = output_model_schema.model_json_schema()
+ import json
+
+ schema_json = json.dumps(schema, indent=2)
+
+ enhancement = f'\nExpected output format: {output_model_schema.__name__}\n{schema_json}'
+ return task + enhancement
+ except Exception as e:
+ self.logger.debug(f'Could not parse output schema: {e}')
+
+ return task
+
@property
def logger(self) -> logging.Logger:
"""Get instance-specific logger with task ID in the name"""