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
LLM should get clean messages without emojis and weird error logs
mertunsall3ac58d9457f4
diff --git a/browser_use/browser/default_action_watchdog.py b/browser_use/browser/default_action_watchdog.pyindex 05964c2e0..d9384c827 100644--- a/browser_use/browser/default_action_watchdog.py+++ b/browser_use/browser/default_action_watchdog.py@@ -203,10 +203,10 @@ class DefaultActionWatchdog(BaseWatchdog):element_type = element_node.attributes.get('type', '').lower() if element_node.attributes else ''if tag_name == 'select':- raise Exception('Cannot click on <select> elements. Use select_dropdown_option action instead.')+ raise Exception('<llm_error_msg>Cannot click on <select> elements. Use select_dropdown_option action instead.</llm_error_msg>')if tag_name == 'input' and element_type == 'file':- raise Exception('Cannot click on file input elements. File uploads must be handled programmatically.')+ raise Exception('<llm_error_msg>Cannot click on file input elements. File uploads must be handled programmatically.</llm_error_msg>')# Get CDP clientcdp_session = await self.browser_session.get_or_create_cdp_session()@@ -495,7 +495,12 @@ class DefaultActionWatchdog(BaseWatchdog):except URLNotAllowedError as e:raise eexcept Exception as e:- raise Exception(f'Failed to click element: {repr(element_node)}. Error: {str(e)}')+ # Extract key element info for error message+ element_info = f"<{element_node.tag_name or 'unknown'}"+ if element_node.element_index:+ element_info += f" index={element_node.element_index}"+ element_info += ">"+ raise Exception(f'<llm_error_msg>Failed to click element {element_info}. The element may not be interactable or visible.</llm_error_msg> Details: {str(e)}')async def _type_to_page(self, text: str):"""@@ -1090,7 +1095,7 @@ class DefaultActionWatchdog(BaseWatchdog):# Check if it's a file inputif not self.browser_session.is_file_input(element_node):- raise Exception(f'Element {index_for_logging} is not a file input')+ raise Exception(f'<llm_error_msg>Element {index_for_logging} is not a file input. Use click_element_by_index for non-file input elements.</llm_error_msg>')# Get CDP client and sessioncdp_client = self.browser_session.cdp_clientdiff --git a/browser_use/controller/service.py b/browser_use/controller/service.pyindex 078a8e694..d7628c75d 100644--- a/browser_use/controller/service.py+++ b/browser_use/controller/service.py@@ -64,6 +64,28 @@ Context = TypeVar('Context')T = TypeVar('T', bound=BaseModel)+def extract_llm_error_message(error: Exception) -> str:+ """+ Extract the clean error message from an exception that may contain <llm_error_msg> tags.++ If the tags are found, returns the content between them.+ Otherwise, returns the original error string.+ """+ import re++ error_str = str(error)++ # Look for content between <llm_error_msg> tags+ pattern = r'<llm_error_msg>(.*?)</llm_error_msg>'+ match = re.search(pattern, error_str, re.DOTALL)++ if match:+ return match.group(1).strip()++ # Fallback: return the original error string+ return error_str++class Controller(Generic[Context]):def __init__(self,@@ -151,14 +173,16 @@ class Controller(Generic[Context]):)await eventawait event.event_result(raise_if_any=True, raise_if_none=False)- msg = f'π Searched for "{params.query}" in Google'+ memory = f"Searched Google for '{params.query}'"+ msg = f'π {memory}'logger.info(msg)return ActionResult(- extracted_content=msg, include_in_memory=True, long_term_memory=f"Searched Google for '{params.query}'"+ extracted_content=memory, include_in_memory=True, long_term_memory=memory)except Exception as e:logger.error(f'Failed to search Google: {e}')- return ActionResult(error=f'Failed to search Google for "{params.query}": {e}')+ clean_msg = extract_llm_error_message(e)+ return ActionResult(error=f'Failed to search Google for "{params.query}": {clean_msg}')@self.registry.action('Navigate to URL, set new_tab=True to open in new tab, False to navigate in current tab', param_model=GoToUrlAction@@ -183,6 +207,7 @@ class Controller(Generic[Context]):error_msg = str(e)# Always log the actual error first for debuggingbrowser_session.logger.error(f'β Navigation failed: {error_msg}')+ clean_msg = extract_llm_error_message(e)# Check if it's specifically a RuntimeError about CDP clientif isinstance(e, RuntimeError) and 'CDP client not initialized' in error_msg:@@ -204,19 +229,21 @@ class Controller(Generic[Context]):return ActionResult(error=site_unavailable_msg)else:# Return error in ActionResult instead of re-raising- return ActionResult(error=f'Navigation failed: {error_msg}')+ return ActionResult(error=f'Navigation failed: {clean_msg}')@self.registry.action('Go back', param_model=NoParamsAction)async def go_back(_: NoParamsAction, browser_session: BrowserSession):try:event = browser_session.event_bus.dispatch(GoBackEvent())await event- msg = 'π Navigated back'+ memory = 'Navigated back'+ msg = f'π {memory}'logger.info(msg)- return ActionResult(extracted_content=msg)+ return ActionResult(extracted_content=memory)except Exception as e:logger.error(f'Failed to dispatch GoBackEvent: {type(e).__name__}: {e}')- error_msg = f'Failed to go back: {e}'+ clean_msg = extract_llm_error_message(e)+ error_msg = f'Failed to go back: {clean_msg}'return ActionResult(error=error_msg)@self.registry.action(@@ -226,11 +253,13 @@ class Controller(Generic[Context]):# Cap wait time at maximum 10 seconds# Reduce the wait time by 3 seconds to account for the llm call which takes at least 3 seconds# So if the model decides to wait for 5 seconds, the llm call took at least 3 seconds, so we only need to wait for 2 seconds- actual_seconds = min(max(seconds - 3, 0), 10)- msg = f'π Waiting for {actual_seconds + 3} seconds'- logger.info(msg)+ # Note by Mert: the above doesnt make sense because we do the LLM call right after this or this could be followed by another action after which we would like to wait+ # so I revert this.+ actual_seconds = min(max(seconds, 0), 10)+ memory = f'Waited for {actual_seconds} seconds'+ logger.info(f'π {memory}')await asyncio.sleep(actual_seconds)- return ActionResult(extracted_content=msg)+ return ActionResult(extracted_content=memory, long_term_memory=memory)# Element Interaction Actions@@ -254,12 +283,14 @@ class Controller(Generic[Context]):await event# Wait for handler to complete and get any exception (None is expected on success)await event.event_result(raise_if_any=True, raise_if_none=False)- msg = f'π±οΈ Clicked element with index {params.index}'+ memory = f'Clicked element with index {params.index}'+ msg = f'π±οΈ {memory}'logger.info(msg)- return ActionResult(extracted_content=msg, include_in_memory=True, long_term_memory=msg)+ return ActionResult(extracted_content=memory, include_in_memory=True, long_term_memory=memory)except Exception as e:logger.error(f'Failed to execute ClickElementEvent: {type(e).__name__}: {e}')- error_msg = f'Failed to click element {params.index}: {type(e).__name__}: {e}'+ clean_msg = extract_llm_error_message(e)+ error_msg = f'Failed to click element {params.index}: {clean_msg}'return ActionResult(error=error_msg)@self.registry.action(@@ -435,14 +466,16 @@ class Controller(Generic[Context]):event = browser_session.event_bus.dispatch(SwitchTabEvent(tab_index=params.page_id))await eventawait event.event_result(raise_if_any=True, raise_if_none=False)- msg = f'π Switched to tab #{params.page_id}'+ memory = f'Switched to tab #{params.page_id}'+ msg = f'π {memory}'logger.info(msg)return ActionResult(- extracted_content=msg, include_in_memory=True, long_term_memory=f'Switched to tab {params.page_id}'+ extracted_content=memory, include_in_memory=True, long_term_memory=memory)except Exception as e:logger.error(f'Failed to switch tab: {e}')- return ActionResult(error=f'Failed to switch to tab {params.page_id}: {e}')+ clean_msg = extract_llm_error_message(e)+ return ActionResult(error=f'Failed to switch to tab {params.page_id}: {clean_msg}')@self.registry.action('Close an existing tab', param_model=CloseTabAction)async def close_tab(params: CloseTabAction, browser_session: BrowserSession):@@ -451,16 +484,18 @@ class Controller(Generic[Context]):event = browser_session.event_bus.dispatch(CloseTabEvent(tab_index=params.page_id))await eventawait event.event_result(raise_if_any=True, raise_if_none=False)- msg = f'β Closed tab #{params.page_id}'+ memory = f'Closed tab #{params.page_id}'+ msg = f'β {memory}'logger.info(msg)return ActionResult(- extracted_content=msg,+ extracted_content=memory,include_in_memory=True,- long_term_memory=f'Closed tab {params.page_id}',+ long_term_memory=memory,)except Exception as e:logger.error(f'Failed to close tab: {e}')- return ActionResult(error=f'Failed to close tab {params.page_id}: {e}')+ clean_msg = extract_llm_error_message(e)+ return ActionResult(error=f'Failed to close tab {params.page_id}: {clean_msg}')# Content Actions@@ -612,7 +647,8 @@ Provide the extracted information in a clear, structured format."""return ActionResult(extracted_content=msg, include_in_memory=True, long_term_memory=long_term_memory)except Exception as e:logger.error(f'Failed to dispatch ScrollEvent: {type(e).__name__}: {e}')- error_msg = f'Failed to scroll: {e}'+ clean_msg = extract_llm_error_message(e)+ error_msg = f'Failed to scroll: {clean_msg}'return ActionResult(error=error_msg)@self.registry.action(@@ -625,12 +661,14 @@ Provide the extracted information in a clear, structured format."""event = browser_session.event_bus.dispatch(SendKeysEvent(keys=params.keys))await eventawait event.event_result(raise_if_any=True, raise_if_none=False)- msg = f'β¨οΈ Sent keys: {params.keys}'+ memory = f'Sent keys: {params.keys}'+ msg = f'β¨οΈ {memory}'logger.info(msg)- return ActionResult(extracted_content=msg, include_in_memory=True, long_term_memory=f'Sent keys: {params.keys}')+ return ActionResult(extracted_content=memory, include_in_memory=True, long_term_memory=memory)except Exception as e:logger.error(f'Failed to dispatch SendKeysEvent: {type(e).__name__}: {e}')- error_msg = f'Failed to send keys: {e}'+ clean_msg = extract_llm_error_message(e)+ error_msg = f'Failed to send keys: {clean_msg}'return ActionResult(error=error_msg)@self.registry.action(@@ -643,9 +681,10 @@ Provide the extracted information in a clear, structured format."""try:# The handler returns None on success or raises an exception if text not foundawait event.event_result(raise_if_any=True, raise_if_none=False)- msg = f'π Scrolled to text: {text}'+ memory = f'Scrolled to text: {text}'+ msg = f'π {memory}'logger.info(msg)- return ActionResult(extracted_content=msg, include_in_memory=True, long_term_memory=f'Scrolled to text: {text}')+ return ActionResult(extracted_content=memory, include_in_memory=True, long_term_memory=memory)except Exception as e:# Text not foundmsg = f"Text '{text}' not found or not visible on page"@@ -1103,14 +1142,54 @@ Provide the extracted information in a clear, structured format."""long_term_memory=f"Selected dropdown option '{params.text}' at index {params.index}",)else:- error_msg = selection_result.get('error', f'Failed to select option: {params.text}')- logger.error(f'β {error_msg}')- raise ValueError(error_msg)+ # Handle error case with clean error message and extracted options+ full_error = selection_result.get('error', f'Failed to select option: {params.text}')++ # Parse available options from the error message if present+ available_options = None+ if 'Available options:' in full_error:+ try:+ # Extract JSON part from error message+ json_start = full_error.find('Available options:') + len('Available options:')+ json_str = full_error[json_start:].strip()+ available_options = json.loads(json_str)+ except (json.JSONDecodeError, ValueError):+ # If parsing fails, just use the full error+ pass++ # Create clean error message+ clean_error = f"Failed to select dropdown option: {params.text}"+ logger.error(f'β {clean_error}')++ # Format available options for extracted_content if we have them+ if available_options:+ formatted_options = []+ for i, opt in enumerate(available_options):+ text = opt.get('text', '')+ value = opt.get('value', '')+ if text:+ formatted_options.append(f'{i}: text="{text}", value="{value}"')++ extracted_content = 'Available dropdown options:\n' + '\n'.join(formatted_options)++ return ActionResult(+ error=clean_error,+ extracted_content=extracted_content,+ include_extracted_content_only_once=True,+ )+ else:+ # No parseable options, return the clean error+ return ActionResult(+ error=clean_error,+ )except Exception as e:- error_msg = f'Failed to select dropdown option: {str(e)}'- logger.error(error_msg)- raise ValueError(error_msg) from e+ # Handle unexpected exceptions+ clean_error = f"Failed to select dropdown option: {params.text}"+ logger.error(f'β {clean_error} - Details: {str(e)}')+ return ActionResult(+ error=clean_error,+ )# File System Actions@self.registry.action(