hsliuping/TradingAgents-CN · critical · ValueError

使用AiHubMix需要设置AIHUBMIX_API_KEY环境变量

Error message

使用AiHubMix需要设置AIHUBMIX_API_KEY环境变量

What it means

TradingAgentsGraph.__init__ raises this when provider is 'aihubmix' and the AIHUBMIX_API_KEY environment variable is unset. AiHubMix is an OpenAI-compatible aggregator and this branch reads the key strictly from the environment. Thrown during graph construction before any LLM call.

Source

Thrown at tradingagents/graph/trading_graph.py:303

        elif normalized_provider in {"openai", "siliconflow", "openrouter", "aihubmix", "volcengine", "ollama"}:
            provider = normalized_provider
            logger.info(f"🔧 [{provider}-快速模型] max_tokens={quick_max_tokens}, temperature={quick_temperature}, timeout={quick_timeout}s")
            logger.info(f"🔧 [{provider}-深度模型] max_tokens={deep_max_tokens}, temperature={deep_temperature}, timeout={deep_timeout}s")

            api_key = None
            if provider == "siliconflow":
                api_key = os.getenv('SILICONFLOW_API_KEY')
                if not api_key:
                    raise ValueError("使用SiliconFlow需要设置SILICONFLOW_API_KEY环境变量")
            elif provider == "openrouter":
                api_key = os.getenv('OPENROUTER_API_KEY') or os.getenv('OPENAI_API_KEY')
                if not api_key:
                    raise ValueError("使用OpenRouter需要设置OPENROUTER_API_KEY或OPENAI_API_KEY环境变量")
            elif provider == "aihubmix":
                api_key = os.getenv('AIHUBMIX_API_KEY')
                if not api_key:
                    raise ValueError("使用AiHubMix需要设置AIHUBMIX_API_KEY环境变量")
            elif provider == "volcengine":
                api_key = os.getenv('VOLCENGINE_API_KEY') or os.getenv('ARK_API_KEY')
                if not api_key:
                    raise ValueError("使用火山方舟需要设置VOLCENGINE_API_KEY或ARK_API_KEY环境变量")
            elif provider == "volcengine_coding":
                api_key = os.getenv('VOLCENGINE_CODING_API_KEY')
                if not api_key:
                    raise ValueError("使用火山方舟编程需要设置VOLCENGINE_CODING_API_KEY环境变量")

            self.deep_thinking_llm, self.quick_thinking_llm = _create_provider_pair(
                provider=provider,
                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,

View on GitHub (pinned to 74783e8817)

Solutions

  1. export AIHUBMIX_API_KEY=... before starting the app
  2. Add AIHUBMIX_API_KEY to .env / container environment and verify it is loaded prior to graph construction
  3. If running the bundled web app, also save the key in Settings so DB-backed paths work

Example fix

# before
graph = TradingAgentsGraph(config={"llm_provider": "aihubmix"})

# after
import os; assert os.getenv("AIHUBMIX_API_KEY"), "set AIHUBMIX_API_KEY first"
graph = TradingAgentsGraph(config={"llm_provider": "aihubmix"})
Defensive patterns

Strategy: validation

Validate before calling

import os
if provider == "aihubmix" and not os.getenv("AIHUBMIX_API_KEY"):
    raise SystemExit("Set AIHUBMIX_API_KEY before constructing the graph")

Type guard

def has_aihubmix_key() -> bool:
    return bool(os.getenv("AIHUBMIX_API_KEY"))

Try / catch

try:
    graph = TradingAgentsGraph(config=cfg)
except ValueError as e:
    if "AIHUBMIX" in str(e):
        raise SystemExit("Configure AIHUBMIX_API_KEY") from e
    raise

Prevention

When it happens

Trigger: Constructing TradingAgentsGraph (or the web app backend) with llm_provider="aihubmix" and no AIHUBMIX_API_KEY in the process environment.

Common situations: Key configured only in the web UI DB but the code path reads env; .env not loaded; deploying to a new server without copying secrets.

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/b7287b226cb6935f. Report an issue: GitHub.