Skip to content

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

Print error trace and fix saveing message

magmueller6ab82b792113
diff --git a/browser_use/agent/service.py b/browser_use/agent/service.py
index 0ca8f8929..24ebecf97 100644
--- a/browser_use/agent/service.py
+++ b/browser_use/agent/service.py
@@ -150,7 +150,7 @@ class Agent:
def _handle_step_error(self, error: Exception, state: BrowserState) -> ActionResult:
"""Handle all types of errors that can occur during a step"""
- error_msg = AgentError.format_error(error)
+ error_msg = AgentError.format_error(error, include_trace=True)
prefix = f'❌ Result failed {self.consecutive_failures + 1}/{self.max_failures} times:\n '
if isinstance(error, (ValidationError, ValueError)):
@@ -250,7 +250,7 @@ class Agent:
parsed: AgentOutput = response['parsed']
- self._update_message_history(response)
+ self._update_message_history(parsed)
self._log_response(parsed)
self._save_conversation(input_messages, parsed)
self.n_steps += 1
@@ -383,7 +383,7 @@ class Agent:
f'🔢 Last Tokens: input: {current_tokens.input_tokens} (cached: {current_tokens.input_token_details.cache_read}) + output: {current_tokens.output_tokens} = {current_tokens.total_tokens} '
)
- def _update_message_history(self, response: Any) -> None:
+ def _update_message_history(self, response: AgentOutput) -> None:
"""Update message history with new interactions"""
self.messages.append(AIMessage(content=response.model_dump_json(exclude_unset=True)))
diff --git a/browser_use/agent/views.py b/browser_use/agent/views.py
index 2b33357fb..cc9c32d4d 100644
--- a/browser_use/agent/views.py
+++ b/browser_use/agent/views.py
@@ -99,10 +99,20 @@ class AgentError:
NO_VALID_ACTION = 'No valid action found'
@staticmethod
- def format_error(error: Exception) -> str:
- """Format error message based on error type"""
+ def format_error(error: Exception, include_trace: bool = False) -> str:
+ """Format error message based on error type and optionally include trace"""
+ message = ''
if isinstance(error, ValidationError):
- return f'{AgentError.VALIDATION_ERROR}\nDetails: {str(error)}'
- if isinstance(error, RateLimitError):
- return AgentError.RATE_LIMIT_ERROR
- return f'Unexpected error: {str(error)}'
+ message = f'{AgentError.VALIDATION_ERROR}\nDetails: {str(error)}'
+ elif isinstance(error, RateLimitError):
+ message = AgentError.RATE_LIMIT_ERROR
+ else:
+ message = f'Unexpected error: {str(error)}'
+
+ if include_trace:
+ import traceback
+
+ trace = ''.join(traceback.format_exception(type(error), error, error.__traceback__))
+ message += f'\nTrace: {trace}'
+
+ return message