{"record":{"id":"d6deb4269357510e","repo":"hsliuping/TradingAgents-CN","slug":"qianfan-api-key-bce-v3-altak-xxx-xxx","errorCode":null,"errorMessage":"QIANFAN_API_KEY格式错误，应为: bce-v3/ALTAK-xxx/xxx","messagePattern":"QIANFAN_API_KEY格式错误，应为: bce-v3/ALTAK-xxx/xxx","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"critical","filePath":"tradingagents/llm_adapters/openai_compatible_base.py","lineNumber":290,"sourceCode":"                    return True\n\n            env_api_key = os.getenv('QIANFAN_API_KEY')\n            if env_api_key and is_valid_api_key(env_api_key):\n                qianfan_api_key = env_api_key\n            else:\n                qianfan_api_key = None\n        else:\n            qianfan_api_key = api_key\n\n        if not qianfan_api_key:\n            raise ValueError(\n                \"千帆模型需要配置 API Key。\"\n                \"请在 Web 界面配置 (设置 -> 大模型厂家) 或设置 QIANFAN_API_KEY 环境变量，\"\n                \"格式为: bce-v3/ALTAK-xxx/xxx\"\n            )\n\n        if not qianfan_api_key.startswith('bce-v3/'):\n            raise ValueError(\n                \"QIANFAN_API_KEY格式错误，应为: bce-v3/ALTAK-xxx/xxx\"\n            )\n        \n        super().__init__(\n            provider_name=\"qianfan\",\n            model=model,\n            api_key_env_var=\"QIANFAN_API_KEY\",\n            base_url=\"https://qianfan.baidubce.com/v2\",\n            api_key=qianfan_api_key,\n            temperature=temperature,\n            max_tokens=max_tokens,\n            **kwargs\n        )\n    \n    def _estimate_tokens(self, text: str) -> int:\n        \"\"\"估算文本的token数量（千帆模型专用）\"\"\"\n        # 千帆模型的token估算：中文约1.5字符/token，英文约4字符/token\n        # 保守估算：2字符/token","sourceCodeStart":272,"sourceCodeEnd":308,"githubUrl":"https://github.com/hsliuping/TradingAgents-CN/blob/74783e8817d6cf6de29867880631cc555153f36b/tradingagents/llm_adapters/openai_compatible_base.py#L272-L308","documentation":"The Qianfan branch of OpenAICompatibleBase.__init__ raises this when a key IS present but does not start with the required 'bce-v3/' prefix. Baidu Qianfan API keys are composite tokens whose format encodes the version, key id, and secret; anything else (an AK, a random string, or whitespace-padded values after prefix check) is rejected before the client is built.","triggerScenarios":"QIANFAN_API_KEY set to a legacy AccessKey, an OpenAI-style sk- key, or a value with leading whitespace/quotes that breaks the prefix check — the format guard fires right after the presence check.","commonSituations":"Pasting a Qianfan AK instead of a bce-v3 API key; shell quoting artifacts (quotes included in the exported value); using an OAuth token from another Baidu product; truncated key from manual copy.","solutions":["Regenerate/copy the API key from Qianfan console ensuring it starts with bce-v3/ and export the full string","Strip stray quotes/whitespace: export QIANFAN_API_KEY=$(printf '%s' \"$RAW\") or verify with startswith('bce-v3/')","If authenticating with AK/SK instead, use the official Qianfan SDK rather than this OpenAI-compatible adapter"],"exampleFix":"# before\n# export QIANFAN_API_KEY='ALTAK-nnn'   # wrong: missing bce-v3/ prefix\nllm = create_llm_client(provider=\"qianfan\", model=\"ernie-4.0\")\n\n# after\n# export QIANFAN_API_KEY='bce-v3/ALTAK-nnn/sss'\nllm = create_llm_client(provider=\"qianfan\", model=\"ernie-4.0\")","handlingStrategy":"type-guard","validationCode":"import os\nk = (api_key or os.getenv(\"QIANFAN_API_KEY\") or '').strip()\nif not k.startswith(\"bce-v3/\"):\n    raise SystemExit(\"QIANFAN_API_KEY must look like bce-v3/ALTAK-xxx/xxx\")","typeGuard":"def is_valid_qianfan_key(k: str | None) -> bool:\n    k = (k or '').strip()\n    return k.startswith(\"bce-v3/\") and k.count('/') >= 3 and len(k) > len(\"bce-v3/\")","tryCatchPattern":"try:\n    llm = create_llm_client(provider=\"qianfan\", model=m)\nexcept ValueError as e:\n    if \"格式错误\" in str(e):\n        raise SystemExit(\"Re-copy the full bce-v3 key from the Qianfan console\") from e\n    raise","preventionTips":["Strip whitespace/quotes when loading secrets from files or env","Validate the bce-v3/ prefix in config-save paths, not only at adapter init","Keep keys intact — partial copies fail the format check"],"tags":["api-key","invalid-format","qianfan","baidu","validation"],"backgroundTag":"invalid-api-key-format","analyzedSha":"74783e8817d6cf6de29867880631cc555153f36b","analyzedAt":"2026-08-28T11:39:07.729Z","schemaVersion":2},"datasetVersion":"2026-08-28T16:17:29.566Z"}