Skip to content

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

Update screenshot handling in prompts and service modules to ensure proper filtering and usage of screenshots based on availability

Magnus Müllerf85cb5ba8920
diff --git a/browser_use/agent/message_manager/service.py b/browser_use/agent/message_manager/service.py
index 826a95029..82b6ed8c1 100644
--- a/browser_use/agent/message_manager/service.py
+++ b/browser_use/agent/message_manager/service.py
@@ -317,15 +317,15 @@ 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 = []
+ 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]
+ 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)
+ screenshots.append(browser_state_summary.screenshot)
# otherwise add state message and result to next message (which will not stay in memory)
assert browser_state_summary
@@ -340,7 +340,7 @@ The file system actions do not change the browser state, so I can also click on
page_filtered_actions=page_filtered_actions,
sensitive_data=self.sensitive_data_description,
available_file_paths=self.available_file_paths,
- screenshots=previous_screenshots,
+ screenshots=screenshots,
).get_user_message(use_vision)
self._add_message_with_type(state_message)
diff --git a/browser_use/agent/prompts.py b/browser_use/agent/prompts.py
index c845cd9f4..be805dc89 100644
--- a/browser_use/agent/prompts.py
+++ b/browser_use/agent/prompts.py
@@ -201,7 +201,7 @@ Interactive elements from top layer of the current page inside the viewport{trun
state_description += 'For this page, these additional actions are available:\n'
state_description += self.page_filtered_actions + '\n'
- if use_vision is True:
+ if use_vision is True and self.screenshots:
# Start with text description
content_parts: list[ContentPartTextParam | ContentPartImageParam] = [ContentPartTextParam(text=state_description)]
for screenshot in self.screenshots: