hsliuping/TradingAgents-CN · critical · ValueError
DeepSeek API密钥未找到。请在 Web 界面配置 API Key (设置 -> 大模型厂家) 或设置 DEEP
Error message
DeepSeek API密钥未找到。请在 Web 界面配置 API Key (设置 -> 大模型厂家) 或设置 DEEPSEEK_API_KEY 环境变量。
What it means
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.
Source
Thrown at tradingagents/llm_adapters/deepseek_adapter.py:90
if '...' in key:
return False
return True
# 从环境变量读取 API Key
env_api_key = os.getenv("DEEPSEEK_API_KEY")
# 验证环境变量中的 API Key 是否有效(排除占位符)
if env_api_key and is_valid_api_key(env_api_key):
api_key = env_api_key
logger.info("✅ [DeepSeek初始化] 使用环境变量中的有效 API Key")
elif env_api_key:
logger.warning("⚠️ [DeepSeek初始化] 环境变量中的 API Key 无效(可能是占位符),将被忽略")
api_key = None
else:
api_key = None
if not api_key:
raise ValueError(
"DeepSeek API密钥未找到。请在 Web 界面配置 API Key "
"(设置 -> 大模型厂家) 或设置 DEEPSEEK_API_KEY 环境变量。"
)
# 初始化父类
super().__init__(
model=model,
openai_api_key=api_key,
openai_api_base=base_url,
temperature=temperature,
max_tokens=max_tokens,
**kwargs
)
self.model_name = model
def _generate(
self,View on GitHub (pinned to 74783e8817)
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
Example fix
# before # .env: DEEPSEEK_API_KEY=sk-your-key-here (placeholder, ignored) llm = DeepSeekAdapter(model="deepseek-chat") # after # .env: DEEPSEEK_API_KEY=sk-realfullkey... llm = DeepSeekAdapter(model="deepseek-chat")
Defensive patterns
Strategy: validation
Validate before calling
import os
k = api_key or os.getenv("DEEPSEEK_API_KEY", "")
if not k or "your" in k.lower() or k.startswith("sk-xxx"):
raise SystemExit("Set a real DEEPSEEK_API_KEY (placeholders are rejected)") Type guard
def is_valid_deepseek_key(k: str | None) -> bool:
return bool(k) and k.startswith("sk-") and "xxx" not in k and "your" not in k.lower() Try / catch
try:
llm = DeepSeekAdapter(model=m)
except ValueError as e:
if "DeepSeek API密钥" in str(e):
raise SystemExit("Provision a valid DeepSeek key") from e
raise Prevention
- Never commit placeholder .env values
- Add a startup lint that flags placeholder-looking secrets
- Fetch keys from a secret manager rather than files
When it happens
Trigger: 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.
Common situations: 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.
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
- 使用DeepSeek需要设置DEEPSEEK_API_KEY环境变量
- 使用Google需要设置GOOGLE_API_KEY环境变量或在数据库中配置API Key
- 使用SiliconFlow需要设置SILICONFLOW_API_KEY环境变量
- 使用OpenRouter需要设置OPENROUTER_API_KEY或OPENAI_API_KEY环境变量
- 使用AiHubMix需要设置AIHUBMIX_API_KEY环境变量
AI-assisted analysis of hsliuping/TradingAgents-CN@74783e8817 (2026-08-28).
Data as JSON: /api/errors/41382c036f22104a.
Report an issue: GitHub.