{"record":{"id":"ce3a84b831318045","repo":"crewAIInc/crewAI","slug":"api-key-must-be-provided-either-through-constructo","errorCode":null,"errorMessage":"API key must be provided either through constructor or MINDS_API_KEY environment variable","messagePattern":"API key must be provided either through constructor or MINDS_API_KEY environment variable","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"lib/crewai-tools/src/crewai_tools/tools/ai_mind_tool/ai_mind_tool.py","lineNumber":49,"sourceCode":"    )\n    args_schema: type[BaseModel] = AIMindToolInputSchema\n    api_key: str | None = None\n    datasources: list[dict[str, Any]] = Field(default_factory=list)\n    mind_name: str | None = None\n    package_dependencies: list[str] = Field(default_factory=lambda: [\"minds-sdk\"])\n    env_vars: list[EnvVar] = Field(\n        default_factory=lambda: [\n            EnvVar(\n                name=\"MINDS_API_KEY\", description=\"API key for AI-Minds\", required=True\n            ),\n        ]\n    )\n\n    def __init__(self, api_key: str | None = None, **kwargs: Any) -> None:\n        super().__init__(**kwargs)\n        self.api_key = api_key or os.getenv(\"MINDS_API_KEY\")\n        if not self.api_key:\n            raise ValueError(\n                \"API key must be provided either through constructor or MINDS_API_KEY environment variable\"\n            )\n\n        try:\n            from minds.client import Client  # type: ignore[import-not-found]\n            from minds.datasources import (  # type: ignore[import-not-found]\n                DatabaseConfig,\n            )\n        except ImportError as e:\n            raise ImportError(\n                \"`minds_sdk` package not found, please run `pip install minds-sdk`\"\n            ) from e\n\n        minds_client = Client(api_key=self.api_key)\n\n        datasources = []\n        for datasource in self.datasources:\n            config = DatabaseConfig(","sourceCodeStart":31,"sourceCodeEnd":67,"githubUrl":"https://github.com/crewAIInc/crewAI/blob/754d7323beb2fd042e33444a115ea2d5a47193f0/lib/crewai-tools/src/crewai_tools/tools/ai_mind_tool/ai_mind_tool.py#L31-L67","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Export the key: `export MINDS_API_KEY=...` (or add it to your .env / secret manager) and re-run.","Or pass it explicitly: AIMindTool(api_key='...', datasources=[...]).","Verify with `python -c \"import os; print(bool(os.getenv('MINDS_API_KEY')))\"` from the same environment that runs the app.","In Docker/CI, ensure the variable is present in the container/process that constructs the tool, not just the build."],"exampleFix":"# before\ntool = AIMindTool(datasources=[...])\n\n# after\ntool = AIMindTool(api_key=os.environ[\"MINDS_API_KEY\"], datasources=[...])","handlingStrategy":"validation","validationCode":"import os\n\napi_key = os.getenv(\"MINDS_API_KEY\")\nif not api_key:\n    raise SystemExit(\"MINDS_API_KEY is required; set it before starting the app\")","typeGuard":null,"tryCatchPattern":"try:\n    tool = AIMindTool(datasources=[...])\nexcept ValueError as e:\n    if \"MINDS_API_KEY\" in str(e):\n        # fetch from secret manager and retry construction\n        os.environ[\"MINDS_API_KEY\"] = secrets_manager.get(\"minds\")\n        tool = AIMindTool(datasources=[...])\n    else:\n        raise","preventionTips":["Fail fast on missing secrets at app startup rather than at tool construction deep in a run.","Load .env files once at process entry, before any tool is built.","Use the tool's declared env_vars metadata (MINDS_API_KEY, required) in preflight checks."],"tags":["authentication","configuration","environment-variables","api-key"],"backgroundTag":null,"analyzedSha":"754d7323beb2fd042e33444a115ea2d5a47193f0","analyzedAt":"2026-08-15T04:06:56.746Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}