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
For default models like openai - include tool call in content because otherwise they need a response with the id
magmueller1a86635b1c05
diff --git a/browser_use/agent/message_manager/service.py b/browser_use/agent/message_manager/service.pyindex 95bc991cf..3c8fe27b9 100644--- a/browser_use/agent/message_manager/service.py+++ b/browser_use/agent/message_manager/service.py@@ -34,6 +34,7 @@ class MessageManager:include_attributes: list[str] = [],max_error_length: int = 400,max_actions_per_step: int = 10,+ tool_call_in_content: bool = True,):self.llm = llmself.system_prompt_class = system_prompt_class@@ -54,27 +55,35 @@ class MessageManager:self._add_message_with_tokens(system_message)self.system_prompt = system_message- wait_for_human = AIMessage(- content='',- additional_kwargs={},- response_metadata={},- tool_calls=[- {- 'name': 'AgentOutput',- 'args': {- 'current_state': {- 'evaluation_previous_goal': 'Unknown - No previous actions to evaluate.',- 'memory': '',- 'next_goal': 'Obtain task from user',- },- 'action': [],+ self.tool_call_in_content = tool_call_in_content+ tool_calls = [+ {+ 'name': 'AgentOutput',+ 'args': {+ 'current_state': {+ 'evaluation_previous_goal': 'Unknown - No previous actions to evaluate.',+ 'memory': '',+ 'next_goal': 'Obtain task from user',},- 'id': '',- 'type': 'tool_call',- }- ],- )- self._add_message_with_tokens(wait_for_human)+ 'action': [],+ },+ 'id': '',+ 'type': 'tool_call',+ }+ ]+ if self.tool_call_in_content:+ # openai throws error if tool_calls are not responded -> move to content+ example_tool_call = AIMessage(+ content=f'{tool_calls}',+ tool_calls=[],+ )+ else:+ example_tool_call = AIMessage(+ content=f'',+ tool_calls=tool_calls,+ )++ self._add_message_with_tokens(example_tool_call)task_message = HumanMessage(content=f'Your task is: {task}')self._add_message_with_tokens(task_message)@@ -120,18 +129,25 @@ class MessageManager:def add_model_output(self, model_output: AgentOutput) -> None:"""Add model output as AI message"""+ tool_calls = [+ {+ 'name': 'AgentOutput',+ 'args': model_output.model_dump(mode='json', exclude_unset=True),+ 'id': '',+ 'type': 'tool_call',+ }+ ]+ if self.tool_call_in_content:+ msg = AIMessage(+ content=f'{tool_calls}',+ tool_calls=[],+ )+ else:+ msg = AIMessage(+ content='',+ tool_calls=tool_calls,+ )- msg = AIMessage(- content='',- tool_calls=[- {- 'name': 'AgentOutput',- 'args': model_output.model_dump(mode='json', exclude_unset=True),- 'id': '',- 'type': 'tool_call',- }- ],- )self._add_message_with_tokens(msg)def get_messages(self) -> List[BaseMessage]:diff --git a/browser_use/agent/service.py b/browser_use/agent/service.pyindex ee2414400..c7e51f25d 100644--- a/browser_use/agent/service.py+++ b/browser_use/agent/service.py@@ -86,6 +86,7 @@ class Agent:],max_error_length: int = 400,max_actions_per_step: int = 10,+ tool_call_in_content: bool = True,):self.agent_id = str(uuid.uuid4()) # unique identifier for the agent@@ -139,6 +140,7 @@ class Agent:include_attributes=self.include_attributes,max_error_length=self.max_error_length,max_actions_per_step=self.max_actions_per_step,+ tool_call_in_content=tool_call_in_content,)# Tracking variables@@ -592,6 +594,10 @@ class Agent:returnimages = []+ # if history is empty or first screenshot is None, we can't create a gif+ if not self.history.history or not self.history.history[0].state.screenshot:+ logger.warning('No history or first screenshot to create GIF from')+ return# Try to load nicer fontstry:@@ -675,7 +681,7 @@ class Agent:loop=0,optimize=False,)- logger.info(f'Created history GIF at {output_path}')+ logger.info(f'Created GIF at {output_path}')else:logger.warning('No images found in history to create GIF')@@ -697,22 +703,6 @@ class Agent:# Calculate vertical center of imagecenter_y = image.height // 2- # Draw "Task:" title with larger font- title = 'Task:'- title_font_size = title_font.size + 20 # Increase title font size by 20- larger_title_font = ImageFont.truetype(title_font.path, title_font_size)- title_bbox = draw.textbbox((0, 0), title, font=larger_title_font)- title_width = title_bbox[2] - title_bbox[0]- title_x = (image.width - title_width) // 2- title_y = center_y - 150 # Increased spacing from center-- draw.text(- (title_x, title_y),- title,- font=larger_title_font,- fill=(255, 255, 255),- )-# Draw task text with increased font sizemargin = 140 # Increased marginmax_width = image.width - (2 * margin)