hsliuping/TradingAgents-CN · critical · ValueError

使用智谱AI需要在数据库中配置API Key或设置ZHIPU_API_KEY环境变量

Error message

使用智谱AI需要在数据库中配置API Key或设置ZHIPU_API_KEY环境变量

What it means

TradingAgentsGraph.__init__ raises this for provider 'glm' (Zhipu AI) when neither config keys quick_api_key/deep_api_key nor the ZHIPU_API_KEY environment variable provide a key. The provider name in config is 'glm' but the env var is ZHIPU_API_KEY — a common mismatch. Raised before model parameters are read and clients built.

Source

Thrown at tradingagents/graph/trading_graph.py:471

                provider="qianfan",
                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,
                quick_extra_kwargs=_quick_extra if _quick_extra else None,
                deep_extra_kwargs=_deep_extra if _deep_extra else None,
            )
            logger.info("✅ [千帆] 文心一言适配器已配置成功并应用用户配置的模型参数")
        elif normalized_provider == "glm":
            # 🔥 优先使用数据库配置的 API Key,否则从环境变量读取
            zhipu_api_key = self.config.get("quick_api_key") or self.config.get("deep_api_key") or os.getenv('ZHIPU_API_KEY')
            logger.info(f"🔑 [智谱AI] API Key 来源: {'数据库配置' if self.config.get('quick_api_key') or self.config.get('deep_api_key') else '环境变量'}")
            
            if not zhipu_api_key:
                raise ValueError("使用智谱AI需要在数据库中配置API Key或设置ZHIPU_API_KEY环境变量")
            
            # 🔧 从配置中读取模型参数(优先使用用户配置,否则使用默认值)
            quick_config = self.config.get("quick_model_config", {})
            deep_config = self.config.get("deep_model_config", {})
            
            quick_max_tokens = quick_config.get("max_tokens", 4000)
            quick_temperature = quick_config.get("temperature", 0.7)
            quick_timeout = quick_config.get("timeout", 180)
            
            deep_max_tokens = deep_config.get("max_tokens", 4000)
            deep_temperature = deep_config.get("temperature", 0.7)
            deep_timeout = deep_config.get("timeout", 180)
            
            logger.info(f"🔧 [智谱AI-快速模型] max_tokens={quick_max_tokens}, temperature={quick_temperature}, timeout={quick_timeout}s")
            logger.info(f"🔧 [智谱AI-深度模型] max_tokens={deep_max_tokens}, temperature={deep_temperature}, timeout={deep_timeout}s")
            
            # 获取 backend_url(如果配置中有的话)
            backend_url = self.config.get("backend_url")

View on GitHub (pinned to 74783e8817)

Solutions

  1. export ZHIPU_API_KEY=<zhipu-key> (exact name; not GLM_API_KEY)
  2. Or set quick_api_key/deep_api_key in the config dict / web UI Settings for the glm provider
  3. Verify with print(bool(os.getenv('ZHIPU_API_KEY'))) in the same process before constructing the graph

Example fix

# before
# GLM_API_KEY set (wrong name)
graph = TradingAgentsGraph(config={"llm_provider": "glm"})

# after
# export ZHIPU_API_KEY=...
cfg = {"llm_provider": "glm", "quick_api_key": "...id.secret"}
graph = TradingAgentsGraph(config=cfg)
Defensive patterns

Strategy: validation

Validate before calling

if provider == "glm" and not (cfg.get("quick_api_key") or cfg.get("deep_api_key") or os.getenv("ZHIPU_API_KEY")):
    raise SystemExit("Set ZHIPU_API_KEY (not GLM_API_KEY) or config keys")

Type guard

def has_zhipu_credential(cfg: dict) -> bool:
    return bool(cfg.get("quick_api_key") or cfg.get("deep_api_key") or os.getenv("ZHIPU_API_KEY"))

Try / catch

try:
    graph = TradingAgentsGraph(config=cfg)
except ValueError as e:
    if "智谱" in str(e):
        raise SystemExit("Set ZHIPU_API_KEY env var or configure key in web UI") from e
    raise

Prevention

When it happens

Trigger: llm_provider="glm" with empty quick_api_key/deep_api_key in config and no ZHIPU_API_KEY exported.

Common situations: User sets GLM_API_KEY (wrong name) instead of ZHIPU_API_KEY; key stored in web UI under another provider profile; deployment missing env passthrough.

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


AI-assisted analysis of hsliuping/TradingAgents-CN@74783e8817 (2026-08-28). Data as JSON: /api/errors/2e225a5c344279b0. Report an issue: GitHub.