hsliuping/TradingAgents-CN · critical · ValueError
使用DeepSeek需要设置DEEPSEEK_API_KEY环境变量
Error message
使用DeepSeek需要设置DEEPSEEK_API_KEY环境变量
What it means
TradingAgentsGraph.__init__ raises this for provider 'deepseek' when no key is found in config (quick_api_key/deep_api_key) or the DEEPSEEK_API_KEY environment variable. It is raised before _create_provider_pair builds the deep/quick LLM pair. Base URL defaults to https://api.deepseek.com unless overridden.
Source
Thrown at tradingagents/graph/trading_graph.py:408
logger.info("🔧 使用统一 llm_clients 路径初始化阿里百炼/通义千问")
self.deep_thinking_llm, self.quick_thinking_llm = _create_provider_pair(
provider="qwen",
config=self.config,
quick_temperature=quick_temperature,
quick_max_tokens=quick_max_tokens,
quick_timeout=quick_timeout,
deep_temperature=deep_temperature,
deep_max_tokens=deep_max_tokens,
deep_timeout=deep_timeout,
backend_url=self.config.get("backend_url"),
quick_extra_kwargs=_quick_extra if _quick_extra else None,
deep_extra_kwargs=_deep_extra if _deep_extra else None,
)
logger.info("✅ [阿里百炼] 已通过 llm_clients 初始化成功并应用用户配置的模型参数")
elif normalized_provider == "deepseek":
deepseek_api_key = self.config.get("quick_api_key") or self.config.get("deep_api_key") or os.getenv('DEEPSEEK_API_KEY')
if not deepseek_api_key:
raise ValueError("使用DeepSeek需要设置DEEPSEEK_API_KEY环境变量")
deepseek_base_url = self.config.get("backend_url") or os.getenv('DEEPSEEK_BASE_URL', 'https://api.deepseek.com')
self.deep_thinking_llm, self.quick_thinking_llm = _create_provider_pair(
provider="deepseek",
config=self.config,
quick_temperature=quick_temperature,
quick_max_tokens=quick_max_tokens,
quick_timeout=quick_timeout,
deep_temperature=deep_temperature,
deep_max_tokens=deep_max_tokens,
deep_timeout=deep_timeout,
backend_url=deepseek_base_url,
api_key=deepseek_api_key,
quick_extra_kwargs=_quick_extra if _quick_extra else None,
deep_extra_kwargs=_deep_extra if _deep_extra else None,
)
logger.info("✅ [DeepSeek] 已通过 llm_clients 初始化成功并应用用户配置的模型参数")
elif normalized_provider == "custom_openai":View on GitHub (pinned to 74783e8817)
Solutions
- export DEEPSEEK_API_KEY=sk-... or set quick_api_key/deep_api_key in the config dict
- Ensure .env is loaded (load_dotenv()) before graph construction in scripts
- For the web app, save the DeepSeek key under Settings -> LLM Providers so config-based lookup succeeds
Example fix
# before
graph = TradingAgentsGraph(config={"llm_provider": "deepseek"})
# after
cfg = {"llm_provider": "deepseek", "quick_api_key": "sk-..."}
graph = TradingAgentsGraph(config=cfg) Defensive patterns
Strategy: validation
Validate before calling
if provider == "deepseek" and not (cfg.get("quick_api_key") or cfg.get("deep_api_key") or os.getenv("DEEPSEEK_API_KEY")):
raise SystemExit("Set DEEPSEEK_API_KEY or quick_api_key/deep_api_key") Type guard
def has_deepseek_credential(cfg: dict) -> bool:
return bool(cfg.get("quick_api_key") or cfg.get("deep_api_key") or os.getenv("DEEPSEEK_API_KEY")) Try / catch
try:
graph = TradingAgentsGraph(config=cfg)
except ValueError as e:
if "DeepSeek" in str(e):
raise SystemExit("Provision DeepSeek key") from e
raise Prevention
- Centralize provider credentials in config, not scattered env reads
- Preflight-validate the selected provider's credential at startup
- Keep .env in sync across dev/prod
When it happens
Trigger: llm_provider="deepseek" with empty config keys and unset DEEPSEEK_API_KEY when constructing TradingAgentsGraph.
Common situations: Fresh clone without .env; key saved in web UI DB but script constructs the graph directly bypassing DB; env var not forwarded to docker/worker process.
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需要设置GOOGLE_API_KEY环境变量或在数据库中配置API Key
- 使用SiliconFlow需要设置SILICONFLOW_API_KEY环境变量
- 使用OpenRouter需要设置OPENROUTER_API_KEY或OPENAI_API_KEY环境变量
- 使用AiHubMix需要设置AIHUBMIX_API_KEY环境变量
- 使用Google AI需要在数据库中配置API Key或设置GOOGLE_API_KEY环境变量
AI-assisted analysis of hsliuping/TradingAgents-CN@74783e8817 (2026-08-28).
Data as JSON: /api/errors/101911f28b67758b.
Report an issue: GitHub.