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
- Set both REDIS_HOST=localhost and REDIS_PORT=6379
- Or set REDIS_CONNECTION_STRING=redis://localhost:6379/0 instead
- 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
- Document required env var pairs in README/.env.example
- Validate env at boot with a small checklist function
- Never set only one of REDIS_HOST/REDIS_PORT
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
- MongoDB连接字符串未配置。请设置环境变量 MONGODB_CONNECTION_STRING\n例如: MONGO
- MongoDB连接字符串未配置。请通过以下方式之一进行配置:\n1. 设置环境变量 MONGODB_CONNECTION
- 不支持的数据源: {self.current_source}
- ❌ Alpha Vantage API Key 未配置!\n请通过以下任一方式配置:\n1. Web 后台配置(推荐):
- name
AI-assisted analysis of hsliuping/TradingAgents-CN@74783e8817 (2026-08-28).
Data as JSON: /api/errors/8f68d640609d51bf.
Report an issue: GitHub.