hsliuping/TradingAgents-CN · critical · ValueError
千帆模型需要配置 API Key。请在 Web 界面配置 (设置 -> 大模型厂家) 或设置 QIANFAN_API_K
Error message
千帆模型需要配置 API Key。请在 Web 界面配置 (设置 -> 大模型厂家) 或设置 QIANFAN_API_KEY 环境变量,格式为: bce-v3/ALTAK-xxx/xxx
What it means
The Qianfan (Baidu) branch of OpenAICompatibleBase.__init__ raises this when no API key is found from kwargs/config or the QIANFAN_API_KEY environment variable. Baidu Qianfan keys have a distinctive composite format (bce-v3/ALTAK-xxx/xxx), which the message documents. Thrown before the format check and parent init.
Source
Thrown at tradingagents/llm_adapters/openai_compatible_base.py:283
return False
if key.startswith('your_') or key.startswith('your-'):
return False
if key.endswith('_here') or key.endswith('-here'):
return False
if '...' in key:
return False
return True
env_api_key = os.getenv('QIANFAN_API_KEY')
if env_api_key and is_valid_api_key(env_api_key):
qianfan_api_key = env_api_key
else:
qianfan_api_key = None
else:
qianfan_api_key = api_key
if not qianfan_api_key:
raise ValueError(
"千帆模型需要配置 API Key。"
"请在 Web 界面配置 (设置 -> 大模型厂家) 或设置 QIANFAN_API_KEY 环境变量,"
"格式为: bce-v3/ALTAK-xxx/xxx"
)
if not qianfan_api_key.startswith('bce-v3/'):
raise ValueError(
"QIANFAN_API_KEY格式错误,应为: bce-v3/ALTAK-xxx/xxx"
)
super().__init__(
provider_name="qianfan",
model=model,
api_key_env_var="QIANFAN_API_KEY",
base_url="https://qianfan.baidubce.com/v2",
api_key=qianfan_api_key,
temperature=temperature,
max_tokens=max_tokens,View on GitHub (pinned to 74783e8817)
Solutions
- Generate a bce-v3 API key in the Baidu Qianfan console and export QIANFAN_API_KEY='bce-v3/ALTAK-xxx/xxx'
- Or configure the key in the web UI (设置 -> 大模型厂家) so it is passed via api_key kwarg
- Verify with: python -c "import os; print(os.getenv('QIANFAN_API_KEY','')[:7])" — should print 'bce-v3/'
Example fix
# before # QIANFAN_API_KEY unset or old AK/SK llm = create_llm_client(provider="qianfan", model="ernie-4.0") # after # export QIANFAN_API_KEY='bce-v3/ALTAK-xxxx/yyyy' llm = create_llm_client(provider="qianfan", model="ernie-4.0")
Defensive patterns
Strategy: validation
Validate before calling
import os
k = api_key or os.getenv("QIANFAN_API_KEY")
if not k:
raise SystemExit("Set QIANFAN_API_KEY='bce-v3/ALTAK-xxx/xxx' (new-style key, not AK/SK)") Type guard
def has_qianfan_key(api_key: str | None) -> bool:
return bool(api_key or os.getenv("QIANFAN_API_KEY")) Try / catch
try:
llm = create_llm_client(provider="qianfan", model=m)
except ValueError as e:
if "千帆" in str(e):
raise SystemExit("Provision bce-v3 Qianfan API key") from e
raise Prevention
- Use the console-generated bce-v3 API key, not legacy AK/SK
- Store the full composite key verbatim (no truncation)
- Preflight presence and format before adapter creation
When it happens
Trigger: Creating the Qianfan adapter without api_key while QIANFAN_API_KEY is unset; common when the user copied an old-style QIANFAN_ACCESS_KEY/SECRET pair instead of the new bce-v3 API key.
Common situations: Migrating from Qianfan SDK auth (AK/SK) to the API-key model; key stored in web UI but adapter constructed directly; env var misspelled; empty-string env value.
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
- 使用Google需要设置GOOGLE_API_KEY环境变量或在数据库中配置API Key
- 使用SiliconFlow需要设置SILICONFLOW_API_KEY环境变量
- 使用OpenRouter需要设置OPENROUTER_API_KEY或OPENAI_API_KEY环境变量
- 使用AiHubMix需要设置AIHUBMIX_API_KEY环境变量
- 使用火山方舟需要设置VOLCENGINE_API_KEY或ARK_API_KEY环境变量
AI-assisted analysis of hsliuping/TradingAgents-CN@74783e8817 (2026-08-28).
Data as JSON: /api/errors/2df7e013adabc1b6.
Report an issue: GitHub.