Skip to content

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

include original LLM status_code in raised exception

Nick Sweeting656a129fbaf2
diff --git a/browser_use/agent/service.py b/browser_use/agent/service.py
index 42c58f2c4..56d266b7d 100644
--- a/browser_use/agent/service.py
+++ b/browser_use/agent/service.py
@@ -1076,7 +1076,10 @@ class Agent(Generic[Context]):
response = {'raw': output, 'parsed': None}
except Exception as e:
self.logger.error(f'Failed to invoke model: {str(e)}')
- raise LLMException(401, 'LLM API call failed') from e
+ # Extract status code if available (e.g., from HTTP exceptions)
+ status_code = getattr(e, 'status_code', None) or getattr(e, 'code', None) or 500
+ error_msg = f'LLM API call failed: {type(e).__name__}: {str(e)}'
+ raise LLMException(status_code, error_msg) from e
# TODO: currently invoke does not return reasoning_content, we should override invoke
output.content = self._remove_think_tags(str(output.content))
try:
@@ -1850,7 +1853,10 @@ class Agent(Generic[Context]):
response = await self.settings.planner_llm.ainvoke(planner_messages)
except Exception as e:
self.logger.error(f'Failed to invoke planner: {str(e)}')
- raise LLMException(401, 'LLM API call failed') from e
+ # Extract status code if available (e.g., from HTTP exceptions)
+ status_code = getattr(e, 'status_code', None) or getattr(e, 'code', None) or 500
+ error_msg = f'Planner LLM API call failed: {type(e).__name__}: {str(e)}'
+ raise LLMException(status_code, error_msg) from e
plan = str(response.content)
# if deepseek-reasoner, remove think tags
diff --git a/tests/ci/test_agent_multiprocessing.py b/tests/ci/test_agent_multiprocessing.py
index 53e901bd2..a799e146a 100644
--- a/tests/ci/test_agent_multiprocessing.py
+++ b/tests/ci/test_agent_multiprocessing.py
@@ -35,6 +35,9 @@ def create_mock_llm():
mock._verified_api_keys = True
mock._verified_tool_calling_method = 'raw'
+ # Add model_name attribute for logging
+ mock.model_name = 'mock-llm'
+
# Create the response
response_content = """
{
@@ -72,6 +75,9 @@ def create_mock_llm_with_actions(action_sequence):
mock._verified_api_keys = True
mock._verified_tool_calling_method = 'raw'
+ # Add model_name attribute for logging
+ mock.model_name = 'mock-llm'
+
action_index = 0
def mock_invoke(*args, **kwargs):
@@ -128,6 +134,7 @@ def run_agent_in_subprocess_module(task_description):
mock_llm = AsyncMock(spec=BaseChatModel)
mock_llm._verified_api_keys = True
mock_llm._verified_tool_calling_method = 'raw'
+ mock_llm.model_name = 'mock-llm'
response_content = """
{
diff --git a/tests/ci/test_browser_session_start.py b/tests/ci/test_browser_session_start.py
index 30e4efac6..97e5cd485 100644
--- a/tests/ci/test_browser_session_start.py
+++ b/tests/ci/test_browser_session_start.py
@@ -570,6 +570,7 @@ class TestBrowserSessionReusePatterns:
# Skip verification by setting these attributes
mock._verified_api_keys = True
mock._verified_tool_calling_method = 'raw'
+ mock.model_name = 'mock-llm'
# Mock the invoke method to return a proper response
def mock_invoke(*args, **kwargs):