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
Refactor ActionResult handling across multiple services to standardize the use of long_term_memory, replacing memory references. Update related logic to ensure extracted content is consistently managed for improved clarity and error handling.
Magnus Müllerce880e5e354e
diff --git a/browser_use/agent/message_manager/service.py b/browser_use/agent/message_manager/service.pyindex 01834bf93..a687e49a3 100644--- a/browser_use/agent/message_manager/service.py+++ b/browser_use/agent/message_manager/service.py@@ -381,19 +381,20 @@ My next action is to click on the iPhone link at index [4] to navigate to Apple'action_results = ''for idx, action_result in enumerate(result):- if action_result.include_in_memory:+ if action_result.update_only_read_state and action_result.extracted_content:self.read_state_description += action_result.extracted_content + '\n'- if action_result.memory:- action_results += f'Action {idx + 1} Result: {action_result.memory}\n'- elif action_result.error:- action_results += f'Action {idx + 1} Error: {action_result.error[:200]}\n'- elif action_result.extracted_content and not action_result.include_in_memory:+ logger.debug(f'Added extracted_content to read_state_description: {action_result.extracted_content}')++ if action_result.long_term_memory:+ action_results += f'Action {idx + 1} Result: {action_result.long_term_memory}\n'+ logger.debug(f'Added long_term_memory to action_results: {action_result.long_term_memory}')+ elif action_result.extracted_content and not action_result.update_only_read_state:action_results += f'Action {idx + 1} Result: {action_result.extracted_content}\n'- logger.warning(- '⚠️ ActionResult does not have memory but has extracted_content. This is not recommended as extracted_content can be too long.'- )- else:- logger.debug(f'Action {idx + 1} has no memory or error:\n{action_result}')+ logger.debug(f'Added extracted_content to action_results: {action_result.extracted_content}')++ if action_result.error:+ action_results += f'Action {idx + 1} Error: {action_result.error[:200]}\n'+ logger.debug(f'Added error to action_results: {action_result.error[:200]}')self.agent_history_description += f"""## Step {step_number}Evaluation: {model_output.current_state.evaluation_previous_goal}diff --git a/browser_use/agent/service.py b/browser_use/agent/service.pyindex ee4867fab..314557d9a 100644--- a/browser_use/agent/service.py+++ b/browser_use/agent/service.py@@ -494,13 +494,13 @@ class Agent(Generic[Context]):async def write_file(file_name: str, content: str):result = await self.file_system.write_file(file_name, content)logger.info(f'💾 {result}')- return ActionResult(extracted_content=result, include_in_memory=True, memory=result)+ return ActionResult(extracted_content=result, include_in_memory=True, long_term_memory=result)@self.controller.registry.action('Append content to file_name in file system')async def append_file(file_name: str, content: str):result = await self.file_system.append_file(file_name, content)logger.info(f'💾 {result}')- return ActionResult(extracted_content=result, include_in_memory=True, memory=result)+ return ActionResult(extracted_content=result, include_in_memory=True, long_term_memory=result)@self.controller.registry.action('Read file_name from file system')async def read_file(file_name: str):@@ -512,7 +512,9 @@ class Agent(Generic[Context]):display_result = resultlogger.info(f'💾 {display_result}')memory = result.split('\n')[-1]- return ActionResult(extracted_content=result, include_in_memory=True, memory=memory, update_read_state=True)+ return ActionResult(+ extracted_content=result, include_in_memory=True, long_term_memory=memory, update_only_read_state=True+ )def _set_message_context(self) -> str | None:if self.tool_calling_method == 'raw':@@ -1635,7 +1637,7 @@ class Agent(Generic[Context]):if orig_target_hash != new_target_hash:msg = f'Element index changed after action {i} / {len(actions)}, because page changed.'logger.info(msg)- results.append(ActionResult(extracted_content=msg, include_in_memory=True, memory=msg))+ results.append(ActionResult(extracted_content=msg, include_in_memory=True, long_term_memory=msg))breaknew_path_hashes = {e.hash.branch_path_hash for e in new_selector_map.values()}@@ -1643,7 +1645,7 @@ class Agent(Generic[Context]):# next action requires index but there are new elements on the pagemsg = f'Something new appeared after action {i} / {len(actions)}, following actions are NOT executed and should be retried.'logger.info(msg)- results.append(ActionResult(extracted_content=msg, include_in_memory=True, memory=msg))+ results.append(ActionResult(extracted_content=msg, include_in_memory=True, long_term_memory=msg))breaktry:@@ -1723,7 +1725,7 @@ class Agent(Generic[Context]):if not is_valid:self.logger.info(f'❌ Validator decision: {parsed.reason}')msg = f'The output is not yet correct. {parsed.reason}.'- self.state.last_result = [ActionResult(extracted_content=msg, include_in_memory=True, memory=msg)]+ self.state.last_result = [ActionResult(extracted_content=msg, include_in_memory=True, long_term_memory=msg)]else:self.logger.info(f'✅ Validator decision: {parsed.reason}')return is_validdiff --git a/browser_use/agent/views.py b/browser_use/agent/views.pyindex 8b1ec2023..dd5a7117b 100644--- a/browser_use/agent/views.py+++ b/browser_use/agent/views.py@@ -104,15 +104,27 @@ class AgentStepInfo:class ActionResult(BaseModel):"""Result of executing an action"""+ # For done actionis_done: bool | None = Falsesuccess: bool | None = None- extracted_content: str | None = None++ # Error handling - always include in long term memoryerror: str | None = None- include_in_memory: bool = False # whether to include in past messages as context or not- memory: str | None = None # Memory of this action- update_read_state: bool = False # Whether the extracted content should be used to update the read_state++ # Filesattachments: list[str] | None = None # Files to display in the done message+ # Always include in long term memory+ long_term_memory: str | None = None # Memory of this action++ # if update_only_read_state is True we add the extracted_content to the agent context only once for the next step+ # if update_only_read_state is False we add the extracted_content to the agent long term memory if no long_term_memory is provided+ extracted_content: str | None = None+ update_only_read_state: bool = False # Whether the extracted content should be used to update the read_state++ # Deprecated+ include_in_memory: bool = False # whether to include in extracted_content inside long_term_memory+@model_validator(mode='after')def validate_success_requires_done(self):"""Ensure success=True can only be set when is_done=True"""diff --git a/browser_use/controller/service.py b/browser_use/controller/service.pyindex 03a7070e2..2301b6452 100644--- a/browser_use/controller/service.py+++ b/browser_use/controller/service.py@@ -102,7 +102,7 @@ class Controller(Generic[Context]):is_done=True,success=params.success,extracted_content=json.dumps(output_dict),- memory=f'Task completed. Success Status: {params.success}',+ long_term_memory=f'Task completed. Success Status: {params.success}',)else:@@ -138,7 +138,11 @@ class Controller(Generic[Context]):attachments = [str(file_system.get_dir() / file_name) for file_name in attachments]return ActionResult(- is_done=True, success=params.success, extracted_content=user_message, memory=memory, attachments=attachments+ is_done=True,+ success=params.success,+ extracted_content=user_message,+ long_term_memory=memory,+ attachments=attachments,)# Basic Navigation Actions@@ -158,7 +162,9 @@ class Controller(Generic[Context]):msg = f'🔍 Searched for "{params.query}" in Google'logger.info(msg)- return ActionResult(extracted_content=msg, include_in_memory=True, memory=f"Searched Google for '{params.query}'")+ return ActionResult(+ extracted_content=msg, include_in_memory=True, long_term_memory=f"Searched Google for '{params.query}'"+ )@self.registry.action('Navigate to URL in the current tab', param_model=GoToUrlAction)async def go_to_url(params: GoToUrlAction, browser_session: BrowserSession):@@ -171,7 +177,7 @@ class Controller(Generic[Context]):page = await browser_session.create_new_tab(params.url)msg = f'Navigated to {params.url}'logger.info(f'🔗 {msg}')- return ActionResult(extracted_content=msg, include_in_memory=True, memory=msg)+ return ActionResult(extracted_content=msg, include_in_memory=True, long_term_memory=msg)except Exception as e:error_msg = str(e)# Check for network-related errors@@ -188,7 +194,7 @@ class Controller(Generic[Context]):site_unavailable_msg = f'Site unavailable: {params.url} - {error_msg}'logger.warning(site_unavailable_msg)return ActionResult(- success=False, error=site_unavailable_msg, include_in_memory=True, memory=site_unavailable_msg+ success=False, error=site_unavailable_msg, include_in_memory=True, long_term_memory=site_unavailable_msg)else:# Re-raise non-network errors@@ -199,7 +205,7 @@ class Controller(Generic[Context]):await browser_session.go_back()msg = '🔙 Navigated back'logger.info(msg)- return ActionResult(extracted_content=msg, include_in_memory=True, memory='Navigated back')+ return ActionResult(extracted_content=msg, include_in_memory=True, long_term_memory='Navigated back')# wait for x seconds@self.registry.action('Wait for x seconds default 3')@@ -207,7 +213,7 @@ class Controller(Generic[Context]):msg = f'🕒 Waiting for {seconds} seconds'logger.info(msg)await asyncio.sleep(seconds)- return ActionResult(extracted_content=msg, include_in_memory=True, memory=f'Waited for {seconds} seconds')+ return ActionResult(extracted_content=msg, include_in_memory=True, long_term_memory=f'Waited for {seconds} seconds')# Element Interaction Actions@self.registry.action('Click element by index', param_model=ClickElementAction)@@ -228,7 +234,7 @@ class Controller(Generic[Context]):# Return informative message with the new state instead of errormax_index = max(selector_map.keys()) if selector_map else -1msg = f'Element with index {params.index} does not exist. Page has {len(selector_map)} interactive elements (indices 0-{max_index}). State has been refreshed - please use the updated element indices.'- return ActionResult(extracted_content=msg, include_in_memory=True, success=False, memory=msg)+ return ActionResult(extracted_content=msg, include_in_memory=True, success=False, long_term_memory=msg)element_node = await browser_session.get_dom_element_by_index(params.index)initial_pages = len(browser_session.tabs)@@ -237,7 +243,7 @@ class Controller(Generic[Context]):if await browser_session.find_file_upload_element_by_index(params.index) is not None:msg = f'Index {params.index} - has an element which opens file upload dialog. To upload files please use a specific function to upload files 'logger.info(msg)- return ActionResult(extracted_content=msg, include_in_memory=True, success=False, memory=msg)+ return ActionResult(extracted_content=msg, include_in_memory=True, success=False, long_term_memory=msg)msg = None@@ -256,7 +262,9 @@ class Controller(Generic[Context]):msg += f' - {new_tab_msg}'logger.info(new_tab_msg)await browser_session.switch_to_tab(-1)- return ActionResult(extracted_content=msg, include_in_memory=True, memory=f'Clicked element {params.index}')+ return ActionResult(+ extracted_content=msg, include_in_memory=True, long_term_memory=f'Clicked element {params.index}'+ )except Exception as e:error_msg = str(e)if 'Execution context was destroyed' in error_msg or 'Cannot find context with specified id' in error_msg:@@ -288,7 +296,9 @@ class Controller(Generic[Context]):logger.info(msg)logger.debug(f'Element xpath: {element_node.xpath}')return ActionResult(- extracted_content=msg, include_in_memory=True, memory=f"Input '{params.text}' into element {params.index}."+ extracted_content=msg,+ include_in_memory=True,+ long_term_memory=f"Input '{params.text}' into element {params.index}.",)# Save PDF@@ -302,7 +312,9 @@ class Controller(Generic[Context]):await page.pdf(path=sanitized_filename, format='A4', print_background=False)msg = f'Saving page with URL {page.url} as PDF to ./{sanitized_filename}'logger.info(msg)- return ActionResult(extracted_content=msg, include_in_memory=True, memory=f'Saved PDF to {sanitized_filename}')+ return ActionResult(+ extracted_content=msg, include_in_memory=True, long_term_memory=f'Saved PDF to {sanitized_filename}'+ )# Tab Management Actions@self.registry.action('Switch tab', param_model=SwitchTabAction)@@ -316,7 +328,9 @@ class Controller(Generic[Context]):passmsg = f'🔄 Switched to tab #{params.page_id} with url {page.url}'logger.info(msg)- return ActionResult(extracted_content=msg, include_in_memory=True, memory=f'Switched to tab {params.page_id}')+ return ActionResult(+ extracted_content=msg, include_in_memory=True, long_term_memory=f'Switched to tab {params.page_id}'+ )@self.registry.action('Open a specific url in new tab', param_model=OpenTabAction)async def open_tab(params: OpenTabAction, browser_session: BrowserSession):@@ -324,7 +338,9 @@ class Controller(Generic[Context]):tab_idx = browser_session.tabs.index(page)msg = f'🔗 Opened new tab #{tab_idx} with url {params.url}'logger.info(msg)- return ActionResult(extracted_content=msg, include_in_memory=True, memory=f'Opened new tab with URL {params.url}')+ return ActionResult(+ extracted_content=msg, include_in_memory=True, long_term_memory=f'Opened new tab with URL {params.url}'+ )@self.registry.action('Close an existing tab', param_model=CloseTabAction)async def close_tab(params: CloseTabAction, browser_session: BrowserSession):@@ -339,7 +355,7 @@ class Controller(Generic[Context]):return ActionResult(extracted_content=msg,include_in_memory=True,- memory=f'Closed tab {params.page_id} with url {url}, now focused on tab {new_page_idx} with url {new_page.url}.',+ long_term_memory=f'Closed tab {params.page_id} with url {url}, now focused on tab {new_page_idx} with url {new_page.url}.',)# Content Actions@@ -424,8 +440,8 @@ Explain the content of the page and that the requested information is not availareturn ActionResult(extracted_content=extracted_content,include_in_memory=False,- update_read_state=True,- memory=memory,+ update_only_read_state=True,+ long_term_memory=memory,)except Exception as e:logger.debug(f'Error extracting content: {e}')@@ -453,7 +469,10 @@ Explain the content of the page and that the requested information is not availamsg = '\n'.join(lines)logger.info(msg)return ActionResult(- extracted_content=msg, include_in_memory=False, memory='Retrieved accessibility tree', update_read_state=True+ extracted_content=msg,+ include_in_memory=False,+ long_term_memory='Retrieved accessibility tree',+ update_only_read_state=True,)@self.registry.action(@@ -487,7 +506,9 @@ Explain the content of the page and that the requested information is not availaamount_str = f'{params.amount} pixels' if params.amount is not None else 'one page'msg = f'🔍 Scrolled down the page by {amount_str}'logger.info(msg)- return ActionResult(extracted_content=msg, include_in_memory=True, memory=f'Scrolled down the page by {amount_str}')+ return ActionResult(+ extracted_content=msg, include_in_memory=True, long_term_memory=f'Scrolled down the page by {amount_str}'+ )@self.registry.action('Scroll up the page by pixel amount - if none is given, scroll one page',@@ -515,7 +536,9 @@ Explain the content of the page and that the requested information is not availaamount_str = f'{params.amount} pixels' if params.amount is not None else 'one page'msg = f'🔍 Scrolled up the page by {amount_str}'logger.info(msg)- return ActionResult(extracted_content=msg, include_in_memory=True, memory=f'Scrolled up the page by{amount_str}')+ return ActionResult(+ extracted_content=msg, include_in_memory=True, long_term_memory=f'Scrolled up the page by{amount_str}'+ )# send keys@self.registry.action(@@ -538,7 +561,7 @@ Explain the content of the page and that the requested information is not availaraise emsg = f'⌨️ Sent keys: {params.keys}'logger.info(msg)- return ActionResult(extracted_content=msg, include_in_memory=True, memory=f'Sent keys: {params.keys}')+ return ActionResult(extracted_content=msg, include_in_memory=True, long_term_memory=f'Sent keys: {params.keys}')@self.registry.action(description='If you dont find something which you want to interact with, scroll to it',@@ -566,7 +589,9 @@ Explain the content of the page and that the requested information is not availaawait asyncio.sleep(0.5) # Wait for scroll to completemsg = f'🔍 Scrolled to text: {text}'logger.info(msg)- return ActionResult(extracted_content=msg, include_in_memory=True, memory=f'Scrolled to text: {text}')+ return ActionResult(+ extracted_content=msg, include_in_memory=True, long_term_memory=f'Scrolled to text: {text}'+ )except Exception as e:logger.debug(f'Locator attempt failed: {str(e)}')@@ -575,7 +600,9 @@ Explain the content of the page and that the requested information is not availamsg = f"Text '{text}' not found or not visible on page"logger.info(msg)return ActionResult(- extracted_content=msg, include_in_memory=True, memory=f"Tried scrolling to text '{text}' but it was not found"+ extracted_content=msg,+ include_in_memory=True,+ long_term_memory=f"Tried scrolling to text '{text}' but it was not found",)except Exception as e:@@ -644,13 +671,15 @@ Explain the content of the page and that the requested information is not availareturn ActionResult(extracted_content=msg,include_in_memory=True,- memory=f'Found dropdown options for index {index}.',- update_read_state=True,+ long_term_memory=f'Found dropdown options for index {index}.',+ update_only_read_state=True,)else:msg = 'No options found in any frame for dropdown'logger.info(msg)- return ActionResult(extracted_content=msg, include_in_memory=True, memory='No dropdown options found')+ return ActionResult(+ extracted_content=msg, include_in_memory=True, long_term_memory='No dropdown options found'+ )except Exception as e:logger.error(f'Failed to get dropdown options: {str(e)}')@@ -675,7 +704,7 @@ Explain the content of the page and that the requested information is not availaif dom_element.tag_name != 'select':logger.error(f'Element is not a select! Tag: {dom_element.tag_name}, Attributes: {dom_element.attributes}')msg = f'Cannot select option: Element with index {index} is a {dom_element.tag_name}, not a select'- return ActionResult(extracted_content=msg, include_in_memory=True, memory=msg)+ return ActionResult(extracted_content=msg, include_in_memory=True, long_term_memory=msg)logger.debug(f"Attempting to select '{text}' using xpath: {dom_element.xpath}")logger.debug(f'Element attributes: {dom_element.attributes}')@@ -736,7 +765,9 @@ Explain the content of the page and that the requested information is not availamsg = f'selected option {text} with value {selected_option_values}'logger.info(msg + f' in frame {frame_index}')- return ActionResult(extracted_content=msg, include_in_memory=True, memory=f"Selected option '{text}'")+ return ActionResult(+ extracted_content=msg, include_in_memory=True, long_term_memory=f"Selected option '{text}'"+ )except Exception as frame_e:logger.error(f'Frame {frame_index} attempt failed: {str(frame_e)}')@@ -747,7 +778,7 @@ Explain the content of the page and that the requested information is not availamsg = f"Could not select option '{text}' in any frame"logger.info(msg)- return ActionResult(extracted_content=msg, include_in_memory=True, memory=msg)+ return ActionResult(extracted_content=msg, include_in_memory=True, long_term_memory=msg)except Exception as e:msg = f'Selection failed: {str(e)}'@@ -959,7 +990,7 @@ Explain the content of the page and that the requested information is not availamsg = f'🖱️ Dragged from ({source_x}, {source_y}) to ({target_x}, {target_y})'logger.info(msg)- return ActionResult(extracted_content=msg, include_in_memory=True, memory=msg)+ return ActionResult(extracted_content=msg, include_in_memory=True, long_term_memory=msg)except Exception as e:error_msg = f'Failed to perform drag and drop: {str(e)}'@@ -976,7 +1007,10 @@ Explain the content of the page and that the requested information is not availaextracted_tsv = await page.evaluate('() => navigator.clipboard.readText()')return ActionResult(- extracted_content=extracted_tsv, include_in_memory=True, memory='Retrieved sheet contents', update_read_state=True+ extracted_content=extracted_tsv,+ include_in_memory=True,+ long_term_memory='Retrieved sheet contents',+ update_only_read_state=True,)@self.registry.action('Google Sheets: Get the contents of a cell or range of cells', domains=['https://docs.google.com'])@@ -991,8 +1025,8 @@ Explain the content of the page and that the requested information is not availareturn ActionResult(extracted_content=extracted_tsv,include_in_memory=True,- memory=f'Retrieved contents from {cell_or_range}',- update_read_state=True,+ long_term_memory=f'Retrieved contents from {cell_or_range}',+ update_only_read_state=True,)@self.registry.action(@@ -1013,7 +1047,7 @@ Explain the content of the page and that the requested information is not availareturn ActionResult(extracted_content=f'Updated cells: {cell_or_range} = {new_contents_tsv}',include_in_memory=False,- memory=f'Updated cells {cell_or_range} with {new_contents_tsv}',+ long_term_memory=f'Updated cells {cell_or_range} with {new_contents_tsv}',)@self.registry.action('Google Sheets: Clear whatever cells are currently selected', domains=['https://docs.google.com'])@@ -1026,7 +1060,7 @@ Explain the content of the page and that the requested information is not availareturn ActionResult(extracted_content=f'Cleared cells: {cell_or_range}',include_in_memory=False,- memory=f'Cleared cells {cell_or_range}',+ long_term_memory=f'Cleared cells {cell_or_range}',)@self.registry.action('Google Sheets: Select a specific cell or range of cells', domains=['https://docs.google.com'])@@ -1047,7 +1081,7 @@ Explain the content of the page and that the requested information is not availareturn ActionResult(extracted_content=f'Selected cells: {cell_or_range}',include_in_memory=False,- memory=f'Selected cells {cell_or_range}',+ long_term_memory=f'Selected cells {cell_or_range}',)@self.registry.action(@@ -1059,7 +1093,9 @@ Explain the content of the page and that the requested information is not availaawait page.keyboard.press('Enter') # make sure to commit the input so it doesn't get overwritten by the next actionawait page.keyboard.press('ArrowUp')return ActionResult(- extracted_content=f'Inputted text {text}', include_in_memory=False, memory=f"Inputted text '{text}' into cell"+ extracted_content=f'Inputted text {text}',+ include_in_memory=False,+ long_term_memory=f"Inputted text '{text}' into cell",)# Register ---------------------------------------------------------------diff --git a/eval/service.py b/eval/service.pyindex 800f64f21..cd86a48a4 100644--- a/eval/service.py+++ b/eval/service.py@@ -464,11 +464,11 @@ def create_controller_with_serp_search():# Convert to string for the agentserp_data_str = json.dumps(serp_data)- return ActionResult(extracted_content=serp_data_str, include_in_memory=False)+ return ActionResult(extracted_content=serp_data_str, include_in_memory=False, update_only_read_state=True)except Exception as e:logger.error(f'Error in SERP search: {type(e).__name__}: {e}')- return ActionResult(extracted_content=f'Search error: {str(e)}', include_in_memory=False)+ return ActionResult(error=f'Search error: {str(e)}')return controllerdiff --git a/examples/custom-functions/advanced_search.py b/examples/custom-functions/advanced_search.pyindex e95a56548..fe50a87de 100644--- a/examples/custom-functions/advanced_search.py+++ b/examples/custom-functions/advanced_search.py@@ -63,7 +63,7 @@ async def search_web(query: str):# to stringorganic_str = json.dumps(organic)- return ActionResult(extracted_content=organic_str, include_in_memory=False)+ return ActionResult(extracted_content=organic_str, include_in_memory=False, update_only_read_state=True)names = [