Skip to content

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

Refactor agent_history_list handling to allow optional input and filter out None values from previous screenshots

Magnus Müller625e9db2f54b
diff --git a/browser_use/agent/message_manager/service.py b/browser_use/agent/message_manager/service.py
index 8cac6ba27..826a95029 100644
--- a/browser_use/agent/message_manager/service.py
+++ b/browser_use/agent/message_manager/service.py
@@ -308,7 +308,7 @@ The file system actions do not change the browser state, so I can also click on
use_vision=True,
page_filtered_actions: str | None = None,
sensitive_data=None,
- agent_history_list=AgentHistoryList, # Pass AgentHistoryList from agent
+ agent_history_list: AgentHistoryList | None = None, # Pass AgentHistoryList from agent
) -> None:
"""Add browser state as human message"""
@@ -317,9 +317,12 @@ The file system actions do not change the browser state, so I can also click on
self.sensitive_data_description = self._get_sensitive_data_description(browser_state_summary.url)
# Extract previous screenshots if we need more than 1 image and have agent history
- previous_screenshots = agent_history_list.screenshots(
- n_last=self.images_per_step - 1, return_none_if_not_screenshot=False
- )
+ previous_screenshots = []
+ if agent_history_list and self.images_per_step > 1:
+ # Get previous screenshots and filter out None values
+ raw_screenshots = agent_history_list.screenshots(n_last=self.images_per_step - 1, return_none_if_not_screenshot=False)
+ previous_screenshots = [s for s in raw_screenshots if s is not None]
+
# add current screenshot to the end
if browser_state_summary.screenshot:
previous_screenshots.append(browser_state_summary.screenshot)