crewAIInc/crewAI · error · ValueError

API key must be provided either through constructor or MINDS

Error message

API key must be provided either through constructor or MINDS_API_KEY environment variable

What it means

AIMindTool requires an API key at construction: it takes api_key from the constructor argument or falls back to the MINDS_API_KEY environment variable (also declared in env_vars as required). If neither is present it raises ValueError immediately, before importing the minds SDK or creating any client.

Source

Thrown at lib/crewai-tools/src/crewai_tools/tools/ai_mind_tool/ai_mind_tool.py:49

    )
    args_schema: type[BaseModel] = AIMindToolInputSchema
    api_key: str | None = None
    datasources: list[dict[str, Any]] = Field(default_factory=list)
    mind_name: str | None = None
    package_dependencies: list[str] = Field(default_factory=lambda: ["minds-sdk"])
    env_vars: list[EnvVar] = Field(
        default_factory=lambda: [
            EnvVar(
                name="MINDS_API_KEY", description="API key for AI-Minds", required=True
            ),
        ]
    )

    def __init__(self, api_key: str | None = None, **kwargs: Any) -> None:
        super().__init__(**kwargs)
        self.api_key = api_key or os.getenv("MINDS_API_KEY")
        if not self.api_key:
            raise ValueError(
                "API key must be provided either through constructor or MINDS_API_KEY environment variable"
            )

        try:
            from minds.client import Client  # type: ignore[import-not-found]
            from minds.datasources import (  # type: ignore[import-not-found]
                DatabaseConfig,
            )
        except ImportError as e:
            raise ImportError(
                "`minds_sdk` package not found, please run `pip install minds-sdk`"
            ) from e

        minds_client = Client(api_key=self.api_key)

        datasources = []
        for datasource in self.datasources:
            config = DatabaseConfig(

View on GitHub (pinned to 754d7323be)

Solutions

  1. Export the key: `export MINDS_API_KEY=...` (or add it to your .env / secret manager) and re-run.
  2. Or pass it explicitly: AIMindTool(api_key='...', datasources=[...]).
  3. Verify with `python -c "import os; print(bool(os.getenv('MINDS_API_KEY')))"` from the same environment that runs the app.
  4. In Docker/CI, ensure the variable is present in the container/process that constructs the tool, not just the build.

Example fix

# before
tool = AIMindTool(datasources=[...])

# after
tool = AIMindTool(api_key=os.environ["MINDS_API_KEY"], datasources=[...])
Defensive patterns

Strategy: validation

Validate before calling

import os

api_key = os.getenv("MINDS_API_KEY")
if not api_key:
    raise SystemExit("MINDS_API_KEY is required; set it before starting the app")

Try / catch

try:
    tool = AIMindTool(datasources=[...])
except ValueError as e:
    if "MINDS_API_KEY" in str(e):
        # fetch from secret manager and retry construction
        os.environ["MINDS_API_KEY"] = secrets_manager.get("minds")
        tool = AIMindTool(datasources=[...])
    else:
        raise

Prevention

When it happens

Trigger: Instantiating AIMindTool(...) with no api_key argument while MINDS_API_KEY is unset; running in a shell/process where the env var was exported in a different session; CI without secrets configured.

Common situations: Forgot to export MINDS_API_KEY; .env file present but not loaded into the process; key stored under a different name (e.g. MINDS_KEY); deploying CrewAI where the agent-creation step runs before secrets injection.

Related errors


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