hsliuping/TradingAgents-CN · critical · ValueError

使用火山方舟需要设置VOLCENGINE_API_KEY或ARK_API_KEY环境变量

Error message

使用火山方舟需要设置VOLCENGINE_API_KEY或ARK_API_KEY环境变量

What it means

TradingAgentsGraph.__init__ raises this when provider is 'volcengine' (Volcengine Ark) and neither VOLCENGINE_API_KEY nor ARK_API_KEY is set. Two env names are accepted because Ark keys were originally exposed as ARK_API_KEY. Failure occurs at construction time, before any model call.

Source

Thrown at tradingagents/graph/trading_graph.py:307

            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,
                backend_url=self.config["backend_url"],
                api_key=api_key,
                quick_extra_kwargs=_quick_extra if _quick_extra else None,
                deep_extra_kwargs=_deep_extra if _deep_extra else None,

View on GitHub (pinned to 74783e8817)

Solutions

  1. export VOLCENGINE_API_KEY=... or ARK_API_KEY=...
  2. Persist the variable in .env / compose and ensure dotenv loads before TradingAgentsGraph(...) is called
  3. Double-check the key is for the Ark (方舟) endpoint, not a generic Volcengine AccessKey

Example fix

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

# after
# export ARK_API_KEY=<ark-key>
graph = TradingAgentsGraph(config={"llm_provider": "volcengine"})
Defensive patterns

Strategy: validation

Validate before calling

import os
if provider == "volcengine" and not (os.getenv("VOLCENGINE_API_KEY") or os.getenv("ARK_API_KEY")):
    raise SystemExit("Set VOLCENGINE_API_KEY or ARK_API_KEY")

Type guard

def has_volcengine_key() -> bool:
    return bool(os.getenv("VOLCENGINE_API_KEY") or os.getenv("ARK_API_KEY"))

Try / catch

try:
    graph = TradingAgentsGraph(config=cfg)
except ValueError as e:
    if "火山方舟" in str(e):
        raise SystemExit("Provision an Ark API key") from e
    raise

Prevention

When it happens

Trigger: Building the graph with llm_provider="volcengine" while both VOLCENGINE_API_KEY and ARK_API_KEY are missing from the environment.

Common situations: Copied Ark key into a differently-named variable (e.g. ARK_API_TOKEN); env not propagated to docker/service; switching providers in config without provisioning the matching key.

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