hsliuping/TradingAgents-CN · critical · ValueError

Redis连接配置未完整设置。请设置以下环境变量之一:\n1. REDIS_CONNECTION_STRING=redi

Error message

Redis连接配置未完整设置。请设置以下环境变量之一:\n1. REDIS_CONNECTION_STRING=redis://localhost:6379/0\n2. REDIS_HOST + REDIS_PORT (例如: REDIS_HOST=localhost, REDIS_PORT=6379)

What it means

Raised by get_redis_config() when neither REDIS_CONNECTION_STRING nor the REDIS_HOST + REDIS_PORT pair is fully set. The function reads host/port from separate env vars and requires both, so a partial configuration is rejected.

Source

Thrown at tradingagents/config/database_config.py:62

            Dict[str, Any]: Redis配置字典
            
        Raises:
            ValueError: 当必要的配置未设置时
        """
        # 优先使用连接字符串
        connection_string = os.getenv('REDIS_CONNECTION_STRING')
        if connection_string:
            return {
                'connection_string': connection_string,
                'database': int(os.getenv('REDIS_DATABASE', 0))
            }
        
        # 使用分离的配置参数
        host = os.getenv('REDIS_HOST')
        port = os.getenv('REDIS_PORT')
        
        if not host or not port:
            raise ValueError(
                "Redis连接配置未完整设置。请设置以下环境变量之一:\n"
                "1. REDIS_CONNECTION_STRING=redis://localhost:6379/0\n"
                "2. REDIS_HOST + REDIS_PORT (例如: REDIS_HOST=localhost, REDIS_PORT=6379)"
            )
        
        return {
            'host': host,
            'port': int(port),
            'password': os.getenv('REDIS_PASSWORD'),
            'database': int(os.getenv('REDIS_DATABASE', 0))
        }
    
    @staticmethod
    def validate_config() -> Dict[str, bool]:
        """
        验证数据库配置是否完整
        
        Returns:

View on GitHub (pinned to 74783e8817)

Solutions

  1. Set both REDIS_HOST=localhost and REDIS_PORT=6379
  2. Or set REDIS_CONNECTION_STRING=redis://localhost:6379/0 instead
  3. Verify with: python -c "import os; print(os.getenv('REDIS_HOST'), os.getenv('REDIS_PORT'))"

Example fix

# before
# .env has only REDIS_HOST
# after
REDIS_HOST=localhost
REDIS_PORT=6379
Defensive patterns

Strategy: validation

Validate before calling

import os
assert os.getenv('REDIS_CONNECTION_STRING') or (os.getenv('REDIS_HOST') and os.getenv('REDIS_PORT')), 'Redis config incomplete'
validate_config()

Try / catch

try:
    cfg = get_redis_config()
except ValueError as e:
    logger.warning('Redis disabled: %s', e)
    cfg = {'enabled': False}

Prevention

When it happens

Trigger: Calling validate_config() (which calls get_redis_config) with REDIS_HOST set but REDIS_PORT missing (or vice versa), or neither the connection string nor host/port set at all.

Common situations: Copy-pasted .env templates where only REDIS_HOST is defined, someone renaming variables (REDIS_URL vs REDIS_CONNECTION_STRING), or deploying with only the URL-style variable present while the code path reads host/port.

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