{"record":{"id":"41382c036f22104a","repo":"hsliuping/TradingAgents-CN","slug":"deepseek-api-web-api-key","errorCode":null,"errorMessage":"DeepSeek API密钥未找到。请在 Web 界面配置 API Key (设置 -> 大模型厂家) 或设置 DEEPSEEK_API_KEY 环境变量。","messagePattern":"DeepSeek API密钥未找到。请在 Web 界面配置 API Key \\(设置 -> 大模型厂家\\) 或设置 DEEPSEEK_API_KEY 环境变量。","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"critical","filePath":"tradingagents/llm_adapters/deepseek_adapter.py","lineNumber":90,"sourceCode":"                    if '...' in key:\n                        return False\n                    return True\n\n            # 从环境变量读取 API Key\n            env_api_key = os.getenv(\"DEEPSEEK_API_KEY\")\n\n            # 验证环境变量中的 API Key 是否有效（排除占位符）\n            if env_api_key and is_valid_api_key(env_api_key):\n                api_key = env_api_key\n                logger.info(\"✅ [DeepSeek初始化] 使用环境变量中的有效 API Key\")\n            elif env_api_key:\n                logger.warning(\"⚠️ [DeepSeek初始化] 环境变量中的 API Key 无效（可能是占位符），将被忽略\")\n                api_key = None\n            else:\n                api_key = None\n\n            if not api_key:\n                raise ValueError(\n                    \"DeepSeek API密钥未找到。请在 Web 界面配置 API Key \"\n                    \"(设置 -> 大模型厂家) 或设置 DEEPSEEK_API_KEY 环境变量。\"\n                )\n        \n        # 初始化父类\n        super().__init__(\n            model=model,\n            openai_api_key=api_key,\n            openai_api_base=base_url,\n            temperature=temperature,\n            max_tokens=max_tokens,\n            **kwargs\n        )\n        \n        self.model_name = model\n        \n    def _generate(\n        self,","sourceCodeStart":72,"sourceCodeEnd":108,"githubUrl":"https://github.com/hsliuping/TradingAgents-CN/blob/74783e8817d6cf6de29867880631cc555153f36b/tradingagents/llm_adapters/deepseek_adapter.py#L72-L108","documentation":"DeepSeekAdapter.__init__ raises this when no valid API key is available: the kwargs/config key is absent and the DEEPSEEK_API_KEY environment variable is either unset or contains a placeholder value (the code explicitly discards placeholder-looking keys). Thrown before the parent OpenAI-compatible init.","triggerScenarios":"Instantiating DeepSeekAdapter without api_key while DEEPSEEK_API_KEY is unset, or set to a placeholder like 'sk-xxx' / 'your-key-here' which the adapter detects and ignores.","commonSituations":"Template .env checked in with placeholder values that pass naive checks; key deleted from environment; blank string; using a CI runner where the secret was never configured — the placeholder heuristic then downgrades it to missing.","solutions":["Set a real DEEPSEEK_API_KEY (starts with sk-, obtained from platform.deepseek.com) in the environment","Or pass api_key explicitly when constructing the adapter / configure it in the web UI (设置 -> 大模型厂家)","Remove placeholder values from .env so they don't mask the real state"],"exampleFix":"# before\n# .env: DEEPSEEK_API_KEY=sk-your-key-here  (placeholder, ignored)\nllm = DeepSeekAdapter(model=\"deepseek-chat\")\n\n# after\n# .env: DEEPSEEK_API_KEY=sk-realfullkey...\nllm = DeepSeekAdapter(model=\"deepseek-chat\")","handlingStrategy":"validation","validationCode":"import os\nk = api_key or os.getenv(\"DEEPSEEK_API_KEY\", \"\")\nif not k or \"your\" in k.lower() or k.startswith(\"sk-xxx\"):\n    raise SystemExit(\"Set a real DEEPSEEK_API_KEY (placeholders are rejected)\")","typeGuard":"def is_valid_deepseek_key(k: str | None) -> bool:\n    return bool(k) and k.startswith(\"sk-\") and \"xxx\" not in k and \"your\" not in k.lower()","tryCatchPattern":"try:\n    llm = DeepSeekAdapter(model=m)\nexcept ValueError as e:\n    if \"DeepSeek API密钥\" in str(e):\n        raise SystemExit(\"Provision a valid DeepSeek key\") from e\n    raise","preventionTips":["Never commit placeholder .env values","Add a startup lint that flags placeholder-looking secrets","Fetch keys from a secret manager rather than files"],"tags":["api-key","missing-env-var","deepseek","placeholder","llm"],"backgroundTag":"missing-api-key","analyzedSha":"74783e8817d6cf6de29867880631cc555153f36b","analyzedAt":"2026-08-28T11:39:07.729Z","schemaVersion":2},"datasetVersion":"2026-08-28T16:17:29.566Z"}