hsliuping/TradingAgents-CN · critical · ValueError
使用Google需要设置GOOGLE_API_KEY环境变量或在数据库中配置API Key
Error message
使用Google需要设置GOOGLE_API_KEY环境变量或在数据库中配置API Key
What it means
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.
Source
Thrown at tradingagents/graph/trading_graph.py:90
factory_provider = "openai" if normalized_provider == "siliconflow" else normalized_provider
client = create_llm_client(
provider=factory_provider,
model=model,
base_url=backend_url,
api_key=api_key,
temperature=temperature,
max_tokens=max_tokens,
timeout=timeout,
**extra_kwargs,
)
return client.get_llm()
if normalized_provider == "google":
# 优先使用传入的 API Key,否则从环境变量读取
google_api_key = api_key or os.getenv('GOOGLE_API_KEY')
if not google_api_key:
raise ValueError("使用Google需要设置GOOGLE_API_KEY环境变量或在数据库中配置API Key")
client = create_llm_client(
provider="google",
model=model,
base_url=backend_url if backend_url else None,
api_key=google_api_key,
temperature=temperature,
max_tokens=max_tokens,
timeout=timeout,
**extra_kwargs,
)
return client.get_llm()
elif normalized_provider == "anthropic":
from langchain_anthropic import ChatAnthropic
return ChatAnthropic(
model=model,View on GitHub (pinned to 74783e8817)
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
Example fix
# before llm = create_llm_by_provider(provider="google", model="gemini-2.0-flash") # after llm = create_llm_by_provider(provider="google", model="gemini-2.0-flash", api_key=os.environ["GOOGLE_API_KEY"])
Defensive patterns
Strategy: validation
Validate before calling
from dotenv import load_dotenv; load_dotenv()
import os
if os.getenv("GOOGLE_API_KEY") is None and api_key is None:
raise SystemExit("Missing GOOGLE_API_KEY — set it before creating the LLM") Type guard
def has_google_key(api_key: str | None) -> bool:
return bool(api_key or os.getenv("GOOGLE_API_KEY")) Try / catch
try:
llm = create_llm_by_provider(provider="google", model=m, api_key=k)
except ValueError as e:
if "GOOGLE_API_KEY" in str(e):
raise SystemExit("Configure GOOGLE_API_KEY (Settings -> LLM Providers or env)") from e
raise Prevention
- 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
When it happens
Trigger: 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.
Common situations: 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.
Understand the failure class
Background: "API key is required" / "API key not found" / "No API key was set": the missing-api-key error family across 16 libraries — this error's family across 16 libraries.
Related errors
- 使用Google AI需要在数据库中配置API Key或设置GOOGLE_API_KEY环境变量
- 使用SiliconFlow需要设置SILICONFLOW_API_KEY环境变量
- 使用OpenRouter需要设置OPENROUTER_API_KEY或OPENAI_API_KEY环境变量
- 使用AiHubMix需要设置AIHUBMIX_API_KEY环境变量
- 使用DeepSeek需要设置DEEPSEEK_API_KEY环境变量
AI-assisted analysis of hsliuping/TradingAgents-CN@74783e8817 (2026-08-28).
Data as JSON: /api/errors/638897a65112e7fd.
Report an issue: GitHub.