crewAIInc/crewAI · error · ValueError

No appropriate API key found for model. Please set OPENAI_AP

Error message

No appropriate API key found for model. Please set OPENAI_API_KEY, ANTHROPIC_API_KEY, or GOOGLE_API_KEY

What it means

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.

Source

Thrown at lib/crewai-tools/src/crewai_tools/tools/stagehand_tool/stagehand_tool.py:330

                    async def init(self) -> None:
                        return None

                    async def close(self) -> None:
                        return None

                self._stagehand = MockStagehand()
                await self._stagehand.init()
                self._page = self._stagehand.page
                self._session_id = self._stagehand.session_id

            return self._stagehand, self._page

        # Normal initialization for non-testing mode
        if not self._stagehand:
            model_api_key = self._get_model_api_key()

            if not model_api_key:
                raise ValueError(
                    "No appropriate API key found for model. Please set OPENAI_API_KEY, ANTHROPIC_API_KEY, or GOOGLE_API_KEY"
                )

            config = StagehandConfig(
                env="BROWSERBASE",
                apiKey=self.api_key,  # Browserbase API key (camelCase)
                projectId=self.project_id,  # Browserbase project ID (camelCase)
                modelApiKey=model_api_key,  # LLM API key - auto-detected based on model
                modelName=self.model_name,
                apiUrl=self.server_url
                if self.server_url
                else "https://api.stagehand.browserbase.com/v1",
                domSettleTimeoutMs=self.dom_settle_timeout_ms,
                selfHeal=self.self_heal,
                waitForCaptchaSolves=self.wait_for_captcha_solves,
                verbose=self.verbose,
                browserbaseSessionID=session_id or self._session_id,
            )

View on GitHub (pinned to 754d7323be)

Solutions

  1. 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.
  2. If you load config from a .env file, call load_dotenv() (or equivalent) before the Stagehand tool first executes.
  3. 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.
  4. In tests, enable the tool's testing mode so MockStagehand is used and no real key is needed.

Example fix

# before
stagehand = StagehandTool(model_name="gpt-4o", api_key=bb_key, project_id=pid)
# runs -> ValueError: No appropriate API key found for model

# after
import os
os.environ["OPENAI_API_KEY"] = "sk-..."  # set before first tool run
stagehand = StagehandTool(model_name="gpt-4o", api_key=bb_key, project_id=pid)
Defensive patterns

Strategy: validation

Validate before calling

import os

def stagehand_model_key_ready(model_name: str) -> bool:
    m = model_name.lower()
    if any(k in m for k in ("openai", "gpt")):
        return bool(os.getenv("OPENAI_API_KEY"))
    if any(k in m for k in ("anthropic", "claude")):
        return bool(os.getenv("ANTHROPIC_API_KEY"))
    if any(k in m for k in ("google", "gemini")):
        return bool(os.getenv("GOOGLE_API_KEY"))
    return False

# before constructing/running the tool
assert stagehand_model_key_ready("gpt-4o"), "set the provider API key env var"

Try / catch

try:
    stagehand_tool.run("navigate to https://example.com")
except ValueError as e:
    if "No appropriate API key" in str(e):
        raise SystemExit("Configure OPENAI/ANTHROPIC/GOOGLE_API_KEY before running") from e
    raise

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of crewAIInc/crewAI@754d7323be (2026-08-15). Data as JSON: /api/errors/45871b50a4318a10. Report an issue: GitHub.