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
simplify warning messages for bad API keys
Nick Sweetinga34d201eb416
diff --git a/browser_use/agent/service.py b/browser_use/agent/service.pyindex 1c8f86afc..10045a05e 100644--- a/browser_use/agent/service.py+++ b/browser_use/agent/service.py@@ -231,15 +231,8 @@ class Agent(Generic[Context]):f'extraction_model={getattr(self.settings.page_extraction_llm, "model_name", None)} ')- # Start non-blocking LLM connection verification using create_task, checked later in step()- # This will run in parallel with browser launch without leaving dangling coroutines on unclean exits- self.llm._verified_api_keys = False- try:- self._verification_task = asyncio.create_task(self._verify_llm_connection())- self._verification_task.add_done_callback(lambda _: None)- except RuntimeError:- # ppl kept reporting _verify_llm_connection was never awaited errors but it's a red-herring:- logger.warning('No running event loop found. All browser-use code must be run inside an async context.')+ # Verify we can connect to the LLM+ self._verify_llm_connection()# Initialize available actions for system prompt (only non-filtered actions)# These will be used for the system prompt to maintain caching@@ -277,8 +270,7 @@ class Agent(Generic[Context]):)except ImportError:logger.warning(- 'Memory functionality was enabled but required packages are not installed. '- "Install with 'pip install browser-use[memory]' to use memory features."+ '⚠️ Agent(enable_memory=True) is set but missing some required packages, install and re-run to use memory features: pip install browser-use[memory]')self.memory = Noneself.enable_memory = False@@ -798,7 +790,7 @@ class Agent(Generic[Context]):)signal_handler.register()- # Wait for verification task to complete if it exists+ # Wait for LLM API _validate_llm_connection() task to complete if it existsif hasattr(self, '_verification_task') and not self._verification_task.done():try:await self._verification_task@@ -806,16 +798,6 @@ class Agent(Generic[Context]):# Error already logged in the taskpass- # Check that verification was successful- try:- assert self.llm._verified_api_keys or SKIP_LLM_API_KEY_VERIFICATION, (- 'Failed to connect to LLM API or LLM API is not responding correctly'- )- except AssertionError:- logger.warning(- '⚠️ Could not connect to LLM API! Continuing anyway for now... (are your API keys valid and do you have enough money in your account?)'- )-try:self._log_agent_run()@@ -1224,41 +1206,41 @@ class Agent(Generic[Context]):return converted_actions- async def _verify_llm_connection(self) -> bool:+ def _verify_llm_connection(self) -> bool:"""Verify that the LLM API keys are setup and the LLM API is responding properly.Helps prevent errors due to running out of API credits, missing env vars, or network issues."""- if getattr(self.llm, '_verified_api_keys', None) is True:- return True # If the LLM API keys have already been verified during a previous run, skip the test+ logger.debug(f'Verifying the {self.llm.__class__.__name__} LLM knows the capital of France...')++ if getattr(self.llm, '_verified_api_keys', None) is True or SKIP_LLM_API_KEY_VERIFICATION:+ # skip roundtrip connection test for speed in cloud environment+ # If the LLM API keys have already been verified during a previous run, skip the test+ self.llm._verified_api_keys = True+ return True- # Check if required environment variables are set for the model we're using+ # show a warning if it looks like any required environment variables are missingrequired_keys = REQUIRED_LLM_API_ENV_VARS.get(self.llm.__class__.__name__, [])if required_keys and not check_env_variables(required_keys, any_or_all=all):- error = f'LLM API Key environment variables not set up for {self.llm.__class__.__name__}, missing: {required_keys}'+ error = f'Expected LLM API Key environment variables might be missing for {self.llm.__class__.__name__}: {" ".join(required_keys)}'logger.warning(f'❌ {error}')- if not SKIP_LLM_API_KEY_VERIFICATION:- self.llm._verified_api_keys = False- raise ValueError(error)-- if SKIP_LLM_API_KEY_VERIFICATION: # skip roundtrip connection test for speed in cloud environment- self.llm._verified_api_keys = True- return True+ # send a basic sanity-test question to the LLM and verify the responsetest_prompt = 'What is the capital of France? Respond with a single word.'test_answer = 'paris'try:- response = await self.llm.ainvoke([HumanMessage(content=test_prompt)])+ # dont convert this to async! it *should* block any subsequent llm calls from running+ response = self.llm.invoke([HumanMessage(content=test_prompt)]) # noqa: ASYNCresponse_text = str(response.content).lower()if test_answer in response_text:logger.debug(- f'🧠 LLM API keys {", ".join(required_keys)} verified, {self.llm.__class__.__name__} model is connected and responding correctly.'+ f'🪪 LLM API keys {", ".join(required_keys)} work, {self.llm.__class__.__name__} model is connected & responding correctly.')self.llm._verified_api_keys = Truereturn Trueelse:- logger.debug(+ logger.warning('❌ Got bad LLM response to basic sanity check question: \n\t %s\n\t\tEXPECTING: %s\n\t\tGOT: %s',test_prompt,test_answer,@@ -1270,7 +1252,7 @@ class Agent(Generic[Context]):logger.error(f'\n\n❌ LLM {self.llm.__class__.__name__} connection test failed. Check that {", ".join(required_keys)} is set correctly in .env and that the LLM API account has sufficient funding.\n\n{e}\n')- raise Exception(f'LLM API connection test failed: {e}') from e+ return Falseasync def _run_planner(self) -> Optional[str]:"""Run the planner to analyze state and suggest next steps"""diff --git a/examples/features/pause_agent.py b/examples/features/pause_agent.pyindex 8e66a84e7..3b20d5474 100644--- a/examples/features/pause_agent.py+++ b/examples/features/pause_agent.py@@ -2,6 +2,10 @@ import asyncioimport osimport sys+import dotenv++dotenv.load_dotenv()+sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))import threading@@ -61,7 +65,10 @@ async def main():while True:print_menu()- choice = input('Enter your choice (1-5): ')+ try:+ choice = input('Enter your choice (1-5): ')+ except KeyboardInterrupt:+ choice = '5'if choice == '1' and not agent_thread:print('Starting agent...')