{"record":{"id":"2e225a5c344279b0","repo":"hsliuping/TradingAgents-CN","slug":"ai-api-key-zhipu-api-key","errorCode":null,"errorMessage":"使用智谱AI需要在数据库中配置API Key或设置ZHIPU_API_KEY环境变量","messagePattern":"使用智谱AI需要在数据库中配置API Key或设置ZHIPU_API_KEY环境变量","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"critical","filePath":"tradingagents/graph/trading_graph.py","lineNumber":471,"sourceCode":"                provider=\"qianfan\",\n                config=self.config,\n                quick_temperature=quick_temperature,\n                quick_max_tokens=quick_max_tokens,\n                quick_timeout=quick_timeout,\n                deep_temperature=deep_temperature,\n                deep_max_tokens=deep_max_tokens,\n                deep_timeout=deep_timeout,\n                quick_extra_kwargs=_quick_extra if _quick_extra else None,\n                deep_extra_kwargs=_deep_extra if _deep_extra else None,\n            )\n            logger.info(\"✅ [千帆] 文心一言适配器已配置成功并应用用户配置的模型参数\")\n        elif normalized_provider == \"glm\":\n            # 🔥 优先使用数据库配置的 API Key，否则从环境变量读取\n            zhipu_api_key = self.config.get(\"quick_api_key\") or self.config.get(\"deep_api_key\") or os.getenv('ZHIPU_API_KEY')\n            logger.info(f\"🔑 [智谱AI] API Key 来源: {'数据库配置' if self.config.get('quick_api_key') or self.config.get('deep_api_key') else '环境变量'}\")\n            \n            if not zhipu_api_key:\n                raise ValueError(\"使用智谱AI需要在数据库中配置API Key或设置ZHIPU_API_KEY环境变量\")\n            \n            # 🔧 从配置中读取模型参数（优先使用用户配置，否则使用默认值）\n            quick_config = self.config.get(\"quick_model_config\", {})\n            deep_config = self.config.get(\"deep_model_config\", {})\n            \n            quick_max_tokens = quick_config.get(\"max_tokens\", 4000)\n            quick_temperature = quick_config.get(\"temperature\", 0.7)\n            quick_timeout = quick_config.get(\"timeout\", 180)\n            \n            deep_max_tokens = deep_config.get(\"max_tokens\", 4000)\n            deep_temperature = deep_config.get(\"temperature\", 0.7)\n            deep_timeout = deep_config.get(\"timeout\", 180)\n            \n            logger.info(f\"🔧 [智谱AI-快速模型] max_tokens={quick_max_tokens}, temperature={quick_temperature}, timeout={quick_timeout}s\")\n            logger.info(f\"🔧 [智谱AI-深度模型] max_tokens={deep_max_tokens}, temperature={deep_temperature}, timeout={deep_timeout}s\")\n            \n            # 获取 backend_url（如果配置中有的话）\n            backend_url = self.config.get(\"backend_url\")","sourceCodeStart":453,"sourceCodeEnd":489,"githubUrl":"https://github.com/hsliuping/TradingAgents-CN/blob/74783e8817d6cf6de29867880631cc555153f36b/tradingagents/graph/trading_graph.py#L453-L489","documentation":"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.","triggerScenarios":"llm_provider=\"glm\" with empty quick_api_key/deep_api_key in config and no ZHIPU_API_KEY exported.","commonSituations":"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.","solutions":["export ZHIPU_API_KEY=<zhipu-key> (exact name; not GLM_API_KEY)","Or set quick_api_key/deep_api_key in the config dict / web UI Settings for the glm provider","Verify with print(bool(os.getenv('ZHIPU_API_KEY'))) in the same process before constructing the graph"],"exampleFix":"# before\n# GLM_API_KEY set (wrong name)\ngraph = TradingAgentsGraph(config={\"llm_provider\": \"glm\"})\n\n# after\n# export ZHIPU_API_KEY=...\ncfg = {\"llm_provider\": \"glm\", \"quick_api_key\": \"...id.secret\"}\ngraph = TradingAgentsGraph(config=cfg)","handlingStrategy":"validation","validationCode":"if provider == \"glm\" and not (cfg.get(\"quick_api_key\") or cfg.get(\"deep_api_key\") or os.getenv(\"ZHIPU_API_KEY\")):\n    raise SystemExit(\"Set ZHIPU_API_KEY (not GLM_API_KEY) or config keys\")","typeGuard":"def has_zhipu_credential(cfg: dict) -> bool:\n    return bool(cfg.get(\"quick_api_key\") or cfg.get(\"deep_api_key\") or os.getenv(\"ZHIPU_API_KEY\"))","tryCatchPattern":"try:\n    graph = TradingAgentsGraph(config=cfg)\nexcept ValueError as e:\n    if \"智谱\" in str(e):\n        raise SystemExit(\"Set ZHIPU_API_KEY env var or configure key in web UI\") from e\n    raise","preventionTips":["Remember provider 'glm' maps to env var ZHIPU_API_KEY","Store the key in web UI config as the durable path","Add a provider→env-var mapping table to onboarding docs"],"tags":["api-key","missing-env-var","zhipu","glm","llm"],"backgroundTag":"missing-api-key","analyzedSha":"74783e8817d6cf6de29867880631cc555153f36b","analyzedAt":"2026-08-28T11:39:07.729Z","schemaVersion":2},"datasetVersion":"2026-08-28T16:17:29.566Z"}