Skip to content

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

cleanup formatting issues with get_trace()

Alezander9907e82603e79
diff --git a/browser_use/agent/service.py b/browser_use/agent/service.py
index c6a7f1e96..8f9ad0f37 100644
--- a/browser_use/agent/service.py
+++ b/browser_use/agent/service.py
@@ -1739,6 +1739,14 @@ class Agent(Generic[Context, AgentStructuredOutput]):
match = re.search(url_pattern, task_text, re.IGNORECASE)
return match.group(0) if match else None
+ def _get_complete_history_without_screenshots(history_data: dict[str, Any]) -> str:
+ if 'history' in history_data:
+ for item in history_data['history']:
+ if 'state' in item and 'screenshot' in item['state']:
+ item['state']['screenshot'] = None
+
+ return json.dumps(history_data)
+
# Generate autogenerated fields
trace_id = uuid7str()
timestamp = datetime.now().isoformat()
@@ -1747,6 +1755,8 @@ class Agent(Generic[Context, AgentStructuredOutput]):
structured_output = self.state.history.structured_output
final_result = self.state.history.final_result()
+
+
return {
'trace': {
# Autogenerated fields
@@ -1769,11 +1779,7 @@ class Agent(Generic[Context, AgentStructuredOutput]):
else json.dumps(structured_output.model_dump()))
if structured_output else None
),
- 'action_history_truncated': (
- json.dumps(self.state.history.action_history())[:20000]
- if len(json.dumps(self.state.history.action_history())) > 20000
- else json.dumps(self.state.history.action_history())
- ) if self.state.history.action_history() else None,
+ 'action_history_truncated': json.dumps(self.state.history.action_history()) if self.state.history.action_history() else None,
'action_errors': json.dumps(self.state.history.errors()) if self.state.history.errors() else None,
'urls': json.dumps(self.state.history.urls()) if self.state.history.urls() else None,
'final_result_response_truncated': (
@@ -1796,7 +1802,6 @@ class Agent(Generic[Context, AgentStructuredOutput]):
# AgentHistoryList methods
'structured_output': json.dumps(structured_output.model_dump()) if structured_output else None,
'final_result_response': final_result,
- 'complete_history': json.dumps(self.state.history.model_dump()),
- 'action_history': json.dumps(self.state.history.action_history()) if self.state.history.action_history() else None,
+ 'complete_history': _get_complete_history_without_screenshots(self.state.history.model_dump()),
}
}
diff --git a/browser_use/agent/views.py b/browser_use/agent/views.py
index faf1ea5b5..5170e62e3 100644
--- a/browser_use/agent/views.py
+++ b/browser_use/agent/views.py
@@ -489,7 +489,7 @@ class AgentHistoryList(BaseModel, Generic[AgentStructuredOutput]):
return outputs
def action_history(self) -> list[list[dict]]:
- """Get action history structured by steps, including results"""
+ """Get truncated action history with only essential fields"""
step_outputs = []
for h in self.history:
@@ -503,7 +503,8 @@ class AgentHistoryList(BaseModel, Generic[AgentStructuredOutput]):
):
action_output = action.model_dump(exclude_none=True)
action_output['interacted_element'] = interacted_element
- action_output['result'] = result.model_dump(exclude_none=True) if result else None
+ # Only keep long_term_memory from result
+ action_output['result'] = result.long_term_memory if result and result.long_term_memory else None
step_actions.append(action_output)
step_outputs.append(step_actions)