hsliuping/TradingAgents-CN · critical · ValueError
使用SiliconFlow需要设置SILICONFLOW_API_KEY环境变量
Error message
使用SiliconFlow需要设置SILICONFLOW_API_KEY环境变量
What it means
TradingAgentsGraph.__init__ raises this when the selected provider is 'siliconflow' and the SILICONFLOW_API_KEY environment variable is unset/empty. Unlike Google, this branch reads only from the environment — no api_key parameter fallback exists here. It fails before any client is constructed.
Source
Thrown at tradingagents/graph/trading_graph.py:295
temperature=deep_temperature,
max_tokens=deep_max_tokens,
timeout=deep_timeout,
api_key=self.config.get("deep_api_key"), # 🔥 传递 API Key
**_deep_extra,
)
logger.info(f"✅ [混合模式] LLM 实例创建成功")
elif normalized_provider in {"openai", "siliconflow", "openrouter", "aihubmix", "volcengine", "ollama"}:
provider = normalized_provider
logger.info(f"🔧 [{provider}-快速模型] max_tokens={quick_max_tokens}, temperature={quick_temperature}, timeout={quick_timeout}s")
logger.info(f"🔧 [{provider}-深度模型] max_tokens={deep_max_tokens}, temperature={deep_temperature}, timeout={deep_timeout}s")
api_key = None
if provider == "siliconflow":
api_key = os.getenv('SILICONFLOW_API_KEY')
if not api_key:
raise ValueError("使用SiliconFlow需要设置SILICONFLOW_API_KEY环境变量")
elif provider == "openrouter":
api_key = os.getenv('OPENROUTER_API_KEY') or os.getenv('OPENAI_API_KEY')
if not api_key:
raise ValueError("使用OpenRouter需要设置OPENROUTER_API_KEY或OPENAI_API_KEY环境变量")
elif provider == "aihubmix":
api_key = os.getenv('AIHUBMIX_API_KEY')
if not api_key:
raise ValueError("使用AiHubMix需要设置AIHUBMIX_API_KEY环境变量")
elif provider == "volcengine":
api_key = os.getenv('VOLCENGINE_API_KEY') or os.getenv('ARK_API_KEY')
if not api_key:
raise ValueError("使用火山方舟需要设置VOLCENGINE_API_KEY或ARK_API_KEY环境变量")
elif provider == "volcengine_coding":
api_key = os.getenv('VOLCENGINE_CODING_API_KEY')
if not api_key:
raise ValueError("使用火山方舟编程需要设置VOLCENGINE_CODING_API_KEY环境变量")
self.deep_thinking_llm, self.quick_thinking_llm = _create_provider_pair(View on GitHub (pinned to 74783e8817)
Solutions
- export SILICONFLOW_API_KEY=sk-... in the environment that runs the app (or add it to .env and load dotenv before graph construction)
- For docker/systemd, add the variable to the service unit / compose environment block
- Verify with python -c "import os; print(bool(os.getenv('SILICONFLOW_API_KEY')))" in the same process context
Example fix
# before
graph = TradingAgentsGraph(config={"llm_provider": "siliconflow"})
# after
from dotenv import load_dotenv; load_dotenv()
graph = TradingAgentsGraph(config={"llm_provider": "siliconflow"}) # .env has SILICONFLOW_API_KEY Defensive patterns
Strategy: validation
Validate before calling
import os
REQUIRED = {"siliconflow": ["SILICONFLOW_API_KEY"]}
missing = [v for v in REQUIRED.get(provider, []) if not os.getenv(v)]
if missing:
raise SystemExit(f"Missing env vars: {missing}") Type guard
def provider_env_ok(provider: str) -> bool:
return bool(os.getenv("SILICONFLOW_API_KEY")) if provider == "siliconflow" else True Try / catch
try:
graph = TradingAgentsGraph(config=cfg)
except ValueError as e:
if "SILICONFLOW_API_KEY" in str(e):
# surface actionable config guidance or switch provider
cfg["llm_provider"] = "openai" # fallback
graph = TradingAgentsGraph(config=cfg)
else:
raise Prevention
- Keep a per-provider required-env-vars map and validate at boot
- Add secrets to docker-compose/systemd environment explicitly
- Run a smoke test constructing the graph in CI with dummy keys
When it happens
Trigger: Constructing TradingAgentsGraph with llm_provider="siliconflow" (or config selecting siliconflow) without SILICONFLOW_API_KEY exported in the process environment.
Common situations: .env not loaded in the worker/web process that builds the graph; key set in shell but process started by systemd/docker without env passthrough; provider misspelled so it unexpectedly falls into siliconflow branch is rare — usually just a missing export.
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
- 使用OpenRouter需要设置OPENROUTER_API_KEY或OPENAI_API_KEY环境变量
- 使用AiHubMix需要设置AIHUBMIX_API_KEY环境变量
- 使用Google AI需要在数据库中配置API Key或设置GOOGLE_API_KEY环境变量
- 使用DeepSeek需要设置DEEPSEEK_API_KEY环境变量
AI-assisted analysis of hsliuping/TradingAgents-CN@74783e8817 (2026-08-28).
Data as JSON: /api/errors/9140ef9f0e7c069c.
Report an issue: GitHub.