{"record":{"id":"9140ef9f0e7c069c","repo":"hsliuping/TradingAgents-CN","slug":"siliconflow-siliconflow-api-key","errorCode":null,"errorMessage":"使用SiliconFlow需要设置SILICONFLOW_API_KEY环境变量","messagePattern":"使用SiliconFlow需要设置SILICONFLOW_API_KEY环境变量","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"critical","filePath":"tradingagents/graph/trading_graph.py","lineNumber":295,"sourceCode":"                temperature=deep_temperature,\n                max_tokens=deep_max_tokens,\n                timeout=deep_timeout,\n                api_key=self.config.get(\"deep_api_key\"),  # 🔥 传递 API Key\n                **_deep_extra,\n            )\n\n            logger.info(f\"✅ [混合模式] LLM 实例创建成功\")\n\n        elif normalized_provider in {\"openai\", \"siliconflow\", \"openrouter\", \"aihubmix\", \"volcengine\", \"ollama\"}:\n            provider = normalized_provider\n            logger.info(f\"🔧 [{provider}-快速模型] max_tokens={quick_max_tokens}, temperature={quick_temperature}, timeout={quick_timeout}s\")\n            logger.info(f\"🔧 [{provider}-深度模型] max_tokens={deep_max_tokens}, temperature={deep_temperature}, timeout={deep_timeout}s\")\n\n            api_key = None\n            if provider == \"siliconflow\":\n                api_key = os.getenv('SILICONFLOW_API_KEY')\n                if not api_key:\n                    raise ValueError(\"使用SiliconFlow需要设置SILICONFLOW_API_KEY环境变量\")\n            elif provider == \"openrouter\":\n                api_key = os.getenv('OPENROUTER_API_KEY') or os.getenv('OPENAI_API_KEY')\n                if not api_key:\n                    raise ValueError(\"使用OpenRouter需要设置OPENROUTER_API_KEY或OPENAI_API_KEY环境变量\")\n            elif provider == \"aihubmix\":\n                api_key = os.getenv('AIHUBMIX_API_KEY')\n                if not api_key:\n                    raise ValueError(\"使用AiHubMix需要设置AIHUBMIX_API_KEY环境变量\")\n            elif provider == \"volcengine\":\n                api_key = os.getenv('VOLCENGINE_API_KEY') or os.getenv('ARK_API_KEY')\n                if not api_key:\n                    raise ValueError(\"使用火山方舟需要设置VOLCENGINE_API_KEY或ARK_API_KEY环境变量\")\n            elif provider == \"volcengine_coding\":\n                api_key = os.getenv('VOLCENGINE_CODING_API_KEY')\n                if not api_key:\n                    raise ValueError(\"使用火山方舟编程需要设置VOLCENGINE_CODING_API_KEY环境变量\")\n\n            self.deep_thinking_llm, self.quick_thinking_llm = _create_provider_pair(","sourceCodeStart":277,"sourceCodeEnd":313,"githubUrl":"https://github.com/hsliuping/TradingAgents-CN/blob/74783e8817d6cf6de29867880631cc555153f36b/tradingagents/graph/trading_graph.py#L277-L313","documentation":"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.","triggerScenarios":"Constructing TradingAgentsGraph with llm_provider=\"siliconflow\" (or config selecting siliconflow) without SILICONFLOW_API_KEY exported in the process environment.","commonSituations":".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.","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"],"exampleFix":"# before\ngraph = TradingAgentsGraph(config={\"llm_provider\": \"siliconflow\"})\n\n# after\nfrom dotenv import load_dotenv; load_dotenv()\ngraph = TradingAgentsGraph(config={\"llm_provider\": \"siliconflow\"})  # .env has SILICONFLOW_API_KEY","handlingStrategy":"validation","validationCode":"import os\nREQUIRED = {\"siliconflow\": [\"SILICONFLOW_API_KEY\"]}\nmissing = [v for v in REQUIRED.get(provider, []) if not os.getenv(v)]\nif missing:\n    raise SystemExit(f\"Missing env vars: {missing}\")","typeGuard":"def provider_env_ok(provider: str) -> bool:\n    return bool(os.getenv(\"SILICONFLOW_API_KEY\")) if provider == \"siliconflow\" else True","tryCatchPattern":"try:\n    graph = TradingAgentsGraph(config=cfg)\nexcept ValueError as e:\n    if \"SILICONFLOW_API_KEY\" in str(e):\n        # surface actionable config guidance or switch provider\n        cfg[\"llm_provider\"] = \"openai\"  # fallback\n        graph = TradingAgentsGraph(config=cfg)\n    else:\n        raise","preventionTips":["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"],"tags":["api-key","missing-env-var","siliconflow","llm","configuration"],"backgroundTag":"missing-api-key","analyzedSha":"74783e8817d6cf6de29867880631cc555153f36b","analyzedAt":"2026-08-28T11:39:07.729Z","schemaVersion":2},"datasetVersion":"2026-08-28T16:17:29.566Z"}