{"record":{"id":"45871b50a4318a10","repo":"crewAIInc/crewAI","slug":"no-appropriate-api-key-found-for-model-please-set","errorCode":null,"errorMessage":"No appropriate API key found for model. Please set OPENAI_API_KEY, ANTHROPIC_API_KEY, or GOOGLE_API_KEY","messagePattern":"No appropriate API key found for model\\. Please set OPENAI_API_KEY, ANTHROPIC_API_KEY, or GOOGLE_API_KEY","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"lib/crewai-tools/src/crewai_tools/tools/stagehand_tool/stagehand_tool.py","lineNumber":330,"sourceCode":"                    async def init(self) -> None:\n                        return None\n\n                    async def close(self) -> None:\n                        return None\n\n                self._stagehand = MockStagehand()\n                await self._stagehand.init()\n                self._page = self._stagehand.page\n                self._session_id = self._stagehand.session_id\n\n            return self._stagehand, self._page\n\n        # Normal initialization for non-testing mode\n        if not self._stagehand:\n            model_api_key = self._get_model_api_key()\n\n            if not model_api_key:\n                raise ValueError(\n                    \"No appropriate API key found for model. Please set OPENAI_API_KEY, ANTHROPIC_API_KEY, or GOOGLE_API_KEY\"\n                )\n\n            config = StagehandConfig(\n                env=\"BROWSERBASE\",\n                apiKey=self.api_key,  # Browserbase API key (camelCase)\n                projectId=self.project_id,  # Browserbase project ID (camelCase)\n                modelApiKey=model_api_key,  # LLM API key - auto-detected based on model\n                modelName=self.model_name,\n                apiUrl=self.server_url\n                if self.server_url\n                else \"https://api.stagehand.browserbase.com/v1\",\n                domSettleTimeoutMs=self.dom_settle_timeout_ms,\n                selfHeal=self.self_heal,\n                waitForCaptchaSolves=self.wait_for_captcha_solves,\n                verbose=self.verbose,\n                browserbaseSessionID=session_id or self._session_id,\n            )","sourceCodeStart":312,"sourceCodeEnd":348,"githubUrl":"https://github.com/crewAIInc/crewAI/blob/754d7323beb2fd042e33444a115ea2d5a47193f0/lib/crewai-tools/src/crewai_tools/tools/stagehand_tool/stagehand_tool.py#L312-L348","documentation":"Thrown by StagehandTool when it cannot auto-detect an LLM provider API key for the model you configured. The tool's _get_model_api_key() looks for OPENAI_API_KEY, ANTHROPIC_API_KEY, or GOOGLE_API_KEY depending on the model name; if none matches, init of the Stagehand browser session aborts with this ValueError. It is raised only in the normal (non-testing) code path, after self._stagehand is found uninitialized.","triggerScenarios":"Calling the tool (which lazily builds a StagehandConfig with env=BROWSERBASE) with model_name set to an OpenAI/Anthropic/Google model while none of the corresponding env vars (OPENAI_API_KEY / ANTHROPIC_API_KEY / GOOGLE_API_KEY) is present in the process environment. Does not fire in testing mode (MockStagehand path) or once self._stagehand is already initialized.","commonSituations":"Running in a container/CI where the LLM key was never exported; setting only the Browserbase API key and project id but forgetting the model key; using a custom proxy of the model whose name does not contain 'openai'/'gpt', 'anthropic'/'claude', or 'google'/'gemini', so key detection misses it; .env file loaded after the tool runs.","solutions":["Export the env var matching your model: export OPENAI_API_KEY=sk-... (or ANTHROPIC_API_KEY / GOOGLE_API_KEY) in the same shell/process that runs CrewAI.","If you load config from a .env file, call load_dotenv() (or equivalent) before the Stagehand tool first executes.","If your model runs through a custom gateway, check _get_model_api_key()'s name-matching logic and pick a model_name that maps to a provider you have a key for.","In tests, enable the tool's testing mode so MockStagehand is used and no real key is needed."],"exampleFix":"# before\nstagehand = StagehandTool(model_name=\"gpt-4o\", api_key=bb_key, project_id=pid)\n# runs -> ValueError: No appropriate API key found for model\n\n# after\nimport os\nos.environ[\"OPENAI_API_KEY\"] = \"sk-...\"  # set before first tool run\nstagehand = StagehandTool(model_name=\"gpt-4o\", api_key=bb_key, project_id=pid)","handlingStrategy":"validation","validationCode":"import os\n\ndef stagehand_model_key_ready(model_name: str) -> bool:\n    m = model_name.lower()\n    if any(k in m for k in (\"openai\", \"gpt\")):\n        return bool(os.getenv(\"OPENAI_API_KEY\"))\n    if any(k in m for k in (\"anthropic\", \"claude\")):\n        return bool(os.getenv(\"ANTHROPIC_API_KEY\"))\n    if any(k in m for k in (\"google\", \"gemini\")):\n        return bool(os.getenv(\"GOOGLE_API_KEY\"))\n    return False\n\n# before constructing/running the tool\nassert stagehand_model_key_ready(\"gpt-4o\"), \"set the provider API key env var\"","typeGuard":null,"tryCatchPattern":"try:\n    stagehand_tool.run(\"navigate to https://example.com\")\nexcept ValueError as e:\n    if \"No appropriate API key\" in str(e):\n        raise SystemExit(\"Configure OPENAI/ANTHROPIC/GOOGLE_API_KEY before running\") from e\n    raise","preventionTips":["Set provider API keys in the process environment (or .env loaded before tool use), not just the Browserbase key.","Smoke-test StagehandTool construction in CI with dummy keys to catch config regressions early.","Use the tool's testing mode (MockStagehand) in unit tests to avoid real-key dependencies."],"tags":["stagehand","api-key","configuration","environment-variables","crewai-tools"],"backgroundTag":null,"analyzedSha":"754d7323beb2fd042e33444a115ea2d5a47193f0","analyzedAt":"2026-08-15T04:06:56.746Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}