hsliuping/TradingAgents-CN · critical · ValueError

MongoDB连接字符串未配置。请设置环境变量 MONGODB_CONNECTION_STRING\n例如: MONGO

Error message

MongoDB连接字符串未配置。请设置环境变量 MONGODB_CONNECTION_STRING\n例如: MONGODB_CONNECTION_STRING=mongodb://localhost:27017/

What it means

Raised by get_mongodb_config() when the MONGODB_CONNECTION_STRING environment variable is empty or unset. The config layer refuses to fall back to a default, so any validate_config() run without MongoDB configured fails immediately.

Source

Thrown at tradingagents/config/database_config.py:27


class DatabaseConfig:
    """数据库配置管理类"""
    
    @staticmethod
    def get_mongodb_config() -> Dict[str, Any]:
        """
        获取MongoDB配置
        
        Returns:
            Dict[str, Any]: MongoDB配置字典
            
        Raises:
            ValueError: 当必要的配置未设置时
        """
        connection_string = os.getenv('MONGODB_CONNECTION_STRING')
        if not connection_string:
            raise ValueError(
                "MongoDB连接字符串未配置。请设置环境变量 MONGODB_CONNECTION_STRING\n"
                "例如: MONGODB_CONNECTION_STRING=mongodb://localhost:27017/"
            )
        
        return {
            'connection_string': connection_string,
            'database': os.getenv('MONGODB_DATABASE', 'tradingagents'),
            'auth_source': os.getenv('MONGODB_AUTH_SOURCE', 'admin')
        }
    
    @staticmethod
    def get_redis_config() -> Dict[str, Any]:
        """
        获取Redis配置
        
        Returns:
            Dict[str, Any]: Redis配置字典
            

View on GitHub (pinned to 74783e8817)

Solutions

  1. export MONGODB_CONNECTION_STRING=mongodb://localhost:27017/ (or add to .env)
  2. Ensure load_dotenv() runs before validate_config()
  3. If MongoDB is optional, gate the validate_config call behind a feature flag or catch ValueError

Example fix

# before
validate_config()  # raises
# after
os.environ.setdefault('MONGODB_CONNECTION_STRING','mongodb://localhost:27017/')
validate_config()
Defensive patterns

Strategy: validation

Validate before calling

import os
if not os.getenv('MONGODB_CONNECTION_STRING'):
    raise SystemExit('Set MONGODB_CONNECTION_STRING before starting')
validate_config()

Try / catch

try:
    cfg = get_mongodb_config()
except ValueError as e:
    if 'MONGODB_CONNECTION_STRING' in str(e):
        # degrade gracefully without mongo
        cfg = None

Prevention

When it happens

Trigger: Calling tradingagents.config.database_config.validate_config() (which calls get_mongodb_config) with MONGODB_CONNECTION_STRING unset or set to empty string in the process environment / .env.

Common situations: Fresh clone without .env, .env not loaded because python-dotenv load_dotenv() was never called, CI runners lacking the variable, or docker env vars not passed through.

Understand the failure class

Background: "environment variable is not set" and "Missing keys in environment" errors: what missing required env var messages mean and how to fix them — this error's family across 28 libraries.

Related errors


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