BerriAI/litellm · error · ValueError

Either 'REDIS_URL' or both 'REDIS_HOST' and 'REDIS_PORT'…

Error message

Either 'REDIS_URL' or both 'REDIS_HOST' and 'REDIS_PORT' must be specified for Redis.

What it means

get_redis_url_from_environment() in litellm/_redis.py builds a Redis URL purely from env vars. It first checks REDIS_URL; otherwise it requires both REDIS_HOST and REDIS_PORT, raising ValueError if either is missing. REDIS_SSL=true switches the scheme to rediss, and REDIS_USERNAME/REDIS_PASSWORD are folded into the auth portion of the URL.

Solutions

  1. Set REDIS_URL=redis://[[user]:pass]@host:port[/db] — a single var covers everything
  2. Or set both REDIS_HOST and REDIS_PORT (and optionally REDIS_PASSWORD, REDIS_USERNAME, REDIS_SSL=true for TLS)
  3. Verify the env reaches the process: docker exec ... env | grep REDIS
  4. If Redis is optional, guard the config so cache init is skipped when env is absent

Example fix

# before
export REDIS_HOST=redis.internal
# REDIS_PORT missing -> ValueError: Either 'REDIS_URL' or both 'REDIS_HOST' and 'REDIS_PORT' must be specified

# after
export REDIS_URL="redis://:password@redis.internal:6379/0"
# or
export REDIS_HOST=redis.internal REDIS_PORT=6379
Defensive patterns

Strategy: validation

Validate before calling

import os

url = os.environ.get("REDIS_URL")
host, port = os.environ.get("REDIS_HOST"), os.environ.get("REDIS_PORT")
if not url and not (host and port):
    raise RuntimeError(
        "Redis not configured: set REDIS_URL or both REDIS_HOST and REDIS_PORT"
    )

Prevention

When it happens

Trigger: Proxy/cache init calls get_redis_url_from_environment() when no explicit Redis URL/host was configured and the environment has none of REDIS_URL, or has REDIS_HOST without REDIS_PORT (or vice versa). Typical when cache: type: redis in config.yaml with the connection details expected from env that aren't set.

Common situations: Forgetting to export REDIS_HOST/REDIS_PORT in the shell or container; setting only REDIS_HOST assuming default port; typos in var names (REDISHOST); env file not loaded by docker-compose/systemd.

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 BerriAI/litellm@77b7c6c40c (2026-08-18). Data as JSON: /api/errors/bf1e97e20f99dff0. Report an issue: GitHub.

Appendix: source

Thrown at litellm/_redis.py:337

            auth_response = self.read_response()

        if str_if_bytes(auth_response) != "OK":
            raise AuthenticationError("Azure AD authentication failed for Redis")

    # Attach the live credential object so async paths can wrap it in
    # AzureADCredentialProvider for refresh-aware token retrieval. The raw
    # client_id/tenant_id/secret are intentionally NOT exposed here — the
    # credential closure already holds them.
    ad_connect._azure_credential = credential
    return ad_connect


def get_redis_url_from_environment():
    if "REDIS_URL" in os.environ:
        return os.environ["REDIS_URL"]

    if "REDIS_HOST" not in os.environ or "REDIS_PORT" not in os.environ:
        raise ValueError("Either 'REDIS_URL' or both 'REDIS_HOST' and 'REDIS_PORT' must be specified for Redis.")

    if "REDIS_SSL" in os.environ and os.environ["REDIS_SSL"].lower() == "true":
        redis_protocol = "rediss"
    else:
        redis_protocol = "redis"

    # Build authentication part of URL
    auth_part = ""
    if "REDIS_USERNAME" in os.environ and "REDIS_PASSWORD" in os.environ:
        auth_part = f"{os.environ['REDIS_USERNAME']}:{os.environ['REDIS_PASSWORD']}@"
    elif "REDIS_PASSWORD" in os.environ:
        auth_part = f"{os.environ['REDIS_PASSWORD']}@"

    return f"{redis_protocol}://{auth_part}{os.environ['REDIS_HOST']}:{os.environ['REDIS_PORT']}"


def _get_redis_client_logic(**env_overrides):
    """

View on GitHub (pinned to 77b7c6c40c)