hsliuping/TradingAgents-CN · critical · ValueError

使用自定义厂家 {provider_name} 需要在数据库配置中设置 default_base_url

Error message

使用自定义厂家 {provider_name} 需要在数据库配置中设置 default_base_url

What it means

TradingAgentsGraph.__init__ raises this for custom providers when the env-var key check passed but config['backend_url'] is empty. Custom providers have no built-in default endpoint, so the OpenAI-compatible base URL must be supplied via configuration. Thrown right after key resolution, before client construction.

Source

Thrown at tradingagents/graph/trading_graph.py:536

            custom_api_key = None
            for env_var in api_key_candidates:
                custom_api_key = os.getenv(env_var)
                if custom_api_key:
                    logger.info(f"✅ 从环境变量 {env_var} 获取到 API Key")
                    break

            if not custom_api_key:
                raise ValueError(
                    f"使用自定义厂家 {provider_name} 需要设置以下环境变量之一:\n"
                    f"  - {provider_name.upper()}_API_KEY\n"
                    f"  - CUSTOM_OPENAI_API_KEY"
                )

            # 获取 backend_url(从配置中获取)
            backend_url = self.config.get("backend_url")
            if not backend_url:
                raise ValueError(
                    f"使用自定义厂家 {provider_name} 需要在数据库配置中设置 default_base_url"
                )

            logger.info(f"🔧 [自定义厂家 {provider_name}] 使用端点: {backend_url}")

            # 🔧 从配置中读取模型参数
            quick_config = self.config.get("quick_model_config", {})
            deep_config = self.config.get("deep_model_config", {})

            quick_max_tokens = quick_config.get("max_tokens", 4000)
            quick_temperature = quick_config.get("temperature", 0.7)
            quick_timeout = quick_config.get("timeout", 180)

            deep_max_tokens = deep_config.get("max_tokens", 4000)
            deep_temperature = deep_config.get("temperature", 0.7)
            deep_timeout = deep_config.get("timeout", 180)

            logger.info(f"🔧 [{provider_name}-快速模型] max_tokens={quick_max_tokens}, temperature={quick_temperature}, timeout={quick_timeout}s")

View on GitHub (pinned to 74783e8817)

Solutions

  1. Set backend_url in the provider config (web UI field default_base_url, or config dict key 'backend_url') to e.g. https://api.myllm.com/v1
  2. Verify the value survives config merging/print config.get('backend_url') before graph init
  3. For scripts, pass config={..., 'backend_url': 'https://...'} explicitly

Example fix

# before
cfg = {"llm_provider": "myllm"}  # no backend_url
graph = TradingAgentsGraph(config=cfg)

# after
cfg = {"llm_provider": "myllm", "backend_url": "https://api.myllm.com/v1"}
graph = TradingAgentsGraph(config=cfg)
Defensive patterns

Strategy: validation

Validate before calling

if provider_is_custom and not cfg.get("backend_url"):
    raise SystemExit("Custom providers require backend_url/default_base_url in config")

Type guard

def custom_provider_config_complete(cfg: dict) -> bool:
    return bool(cfg.get("backend_url"))

Try / catch

try:
    graph = TradingAgentsGraph(config=cfg)
except ValueError as e:
    if "default_base_url" in str(e):
        cfg["backend_url"] = "https://api.default.example/v1"  # known fallback
        graph = TradingAgentsGraph(config=cfg)
    else:
        raise

Prevention

When it happens

Trigger: Custom provider with API key set via env but no backend_url/default_base_url stored in config (e.g. provider record created in web UI without filling the endpoint field).

Common situations: Provider record partially configured — key filled, URL left blank; config dict built manually and backend_url key omitted; URL stored under a different key name (e.g. base_url) that this branch ignores.

Related errors


AI-assisted analysis of hsliuping/TradingAgents-CN@74783e8817 (2026-08-28). Data as JSON: /api/errors/be765d804953d75f. Report an issue: GitHub.