{"record":{"id":"638897a65112e7fd","repo":"hsliuping/TradingAgents-CN","slug":"google-google-api-key-api-key","errorCode":null,"errorMessage":"使用Google需要设置GOOGLE_API_KEY环境变量或在数据库中配置API Key","messagePattern":"使用Google需要设置GOOGLE_API_KEY环境变量或在数据库中配置API Key","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"critical","filePath":"tradingagents/graph/trading_graph.py","lineNumber":90,"sourceCode":"\n        factory_provider = \"openai\" if normalized_provider == \"siliconflow\" else normalized_provider\n        client = create_llm_client(\n            provider=factory_provider,\n            model=model,\n            base_url=backend_url,\n            api_key=api_key,\n            temperature=temperature,\n            max_tokens=max_tokens,\n            timeout=timeout,\n            **extra_kwargs,\n        )\n        return client.get_llm()\n\n    if normalized_provider == \"google\":\n        # 优先使用传入的 API Key，否则从环境变量读取\n        google_api_key = api_key or os.getenv('GOOGLE_API_KEY')\n        if not google_api_key:\n            raise ValueError(\"使用Google需要设置GOOGLE_API_KEY环境变量或在数据库中配置API Key\")\n\n        client = create_llm_client(\n            provider=\"google\",\n            model=model,\n            base_url=backend_url if backend_url else None,\n            api_key=google_api_key,\n            temperature=temperature,\n            max_tokens=max_tokens,\n            timeout=timeout,\n            **extra_kwargs,\n        )\n        return client.get_llm()\n\n    elif normalized_provider == \"anthropic\":\n        from langchain_anthropic import ChatAnthropic\n\n        return ChatAnthropic(\n            model=model,","sourceCodeStart":72,"sourceCodeEnd":108,"githubUrl":"https://github.com/hsliuping/TradingAgents-CN/blob/74783e8817d6cf6de29867880631cc555153f36b/tradingagents/graph/trading_graph.py#L72-L108","documentation":"create_llm_by_provider raises this when provider is 'google' and no API key is available: neither the api_key argument nor the GOOGLE_API_KEY environment variable is set. The Google provider requires a key for every ChatGoogle-style client instantiation. This is a fail-fast check before any network call is attempted.","triggerScenarios":"Calling create_llm_by_provider(provider=\"google\", model=...) with api_key=None while GOOGLE_API_KEY is unset; common in fresh deployments or CI where .env was not loaded.","commonSituations":"Forgot to export GOOGLE_API_KEY; .env file not loaded (python-dotenv not called); key stored only in the web UI database but calling create_llm_by_provider directly bypasses DB lookup; typo in env var name.","solutions":["Export GOOGLE_API_KEY or pass api_key=\"...\" explicitly to create_llm_by_provider","If using dotenv, ensure load_dotenv() runs before LLM creation and .env contains GOOGLE_API_KEY=...","If the key lives in the app database, fetch it first and pass it via the api_key parameter"],"exampleFix":"# before\nllm = create_llm_by_provider(provider=\"google\", model=\"gemini-2.0-flash\")\n\n# after\nllm = create_llm_by_provider(provider=\"google\", model=\"gemini-2.0-flash\", api_key=os.environ[\"GOOGLE_API_KEY\"])","handlingStrategy":"validation","validationCode":"from dotenv import load_dotenv; load_dotenv()\nimport os\nif os.getenv(\"GOOGLE_API_KEY\") is None and api_key is None:\n    raise SystemExit(\"Missing GOOGLE_API_KEY — set it before creating the LLM\")","typeGuard":"def has_google_key(api_key: str | None) -> bool:\n    return bool(api_key or os.getenv(\"GOOGLE_API_KEY\"))","tryCatchPattern":"try:\n    llm = create_llm_by_provider(provider=\"google\", model=m, api_key=k)\nexcept ValueError as e:\n    if \"GOOGLE_API_KEY\" in str(e):\n        raise SystemExit(\"Configure GOOGLE_API_KEY (Settings -> LLM Providers or env)\") from e\n    raise","preventionTips":["Load .env at process start before any LLM construction","Use a startup preflight that asserts all required provider env vars","Pass api_key explicitly when it is known, rather than relying on env"],"tags":["api-key","missing-env-var","google","llm","configuration"],"backgroundTag":"missing-api-key","analyzedSha":"74783e8817d6cf6de29867880631cc555153f36b","analyzedAt":"2026-08-28T11:39:07.729Z","schemaVersion":2},"datasetVersion":"2026-08-28T16:17:29.566Z"}