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
Merge branch 'main' into feature/exception-handling-llm-call
Anish Hiranandanid6133380e71e
diff --cc browser_use/agent/service.pyindex 1f338d452,a29d341f2..d24f147fc--- a/browser_use/agent/service.py+++ b/browser_use/agent/service.py@@@ -545,34 -531,51 +545,58 @@@ class Agent(Generic[Context])elif self.tool_calling_method is None:structured_llm = self.llm.with_structured_output(self.AgentOutput, include_raw=True)- response: dict[str, Any] = await structured_llm.ainvoke(input_messages) # type: ignore- parsed: AgentOutput | None = response['parsed']+ try:+ response: dict[str, Any] = await structured_llm.ainvoke(input_messages) # type: ignore+ parsed: AgentOutput | None = response['parsed']++ except Exception as e:+ logger.error(f'Failed to invoke model: {str(e)}')+ raise LLMException(401, 'LLM API call failed') from e+else:+ logger.debug(f"Using {self.tool_calling_method} for {self.chat_model_library}")structured_llm = self.llm.with_structured_output(self.AgentOutput, include_raw=True, method=self.tool_calling_method)- try:- response: dict[str, Any] = await structured_llm.ainvoke(input_messages) # type: ignore- parsed: AgentOutput | None = response['parsed']- except Exception as e:- logger.error(f'Failed to invoke model: {str(e)}')- raise LLMException(401, 'LLM API call failed') from e+ response: dict[str, Any] = await structured_llm.ainvoke(input_messages) # type: ignore- if not parsed:- try:- parsed_json = extract_json_from_model_output(response["raw"].content)- parsed = self.AgentOutput(**parsed_json)- except:- logger.warning(f'Failed to parse model output: {response["raw"].content} {str(e)}')- raise ValueError('Could not parse response.')+ # Handle tool call responses+ if response.get('parsing_error') and 'raw' in response:+ raw_msg = response['raw']+ if hasattr(raw_msg, 'tool_calls') and raw_msg.tool_calls:+ # Convert tool calls to AgentOutput format++ tool_call = raw_msg.tool_calls[0] # Take first tool call++ # Create current state+ tool_call_name = tool_call['name']+ tool_call_args = tool_call['args']++ current_state = {+ 'page_summary': 'Processing tool call',+ 'evaluation_previous_goal': 'Executing action',+ 'memory': 'Using tool call',+ 'next_goal': f'Execute {tool_call_name}'+ }++ # Create action from tool call+ action = {tool_call_name: tool_call_args}++ parsed = self.AgentOutput(+ current_state=current_state,+ action=[self.ActionModel(**action)]+ )+ else:+ parsed = None+ else:+ parsed = response['parsed']++ if not parsed:+ try:+ parsed_json = extract_json_from_model_output(response["raw"].content)+ parsed = self.AgentOutput(**parsed_json)+ except:+ logger.warning(f'Failed to parse model output: {response["raw"].content} {str(e)}')+ raise ValueError('Could not parse response.')- if parsed is None:- raise ValueError('Could not parse response.')# cut the number of actions to max_actions_per_step if neededif len(parsed.action) > self.settings.max_actions_per_step: