Skip to content

The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]

tests(vcr): isolate cassette redis to CASSETTE_REDIS_URL

mateo-berri4c695576214b
diff --git a/tests/_flush_vcr_cache.py b/tests/_flush_vcr_cache.py
index c78958fb016..d236c88fa3c 100644
--- a/tests/_flush_vcr_cache.py
+++ b/tests/_flush_vcr_cache.py
@@ -5,7 +5,7 @@ import sys
import redis
-from tests._vcr_redis_persister import _redis_url_from_env
+from tests._vcr_redis_persister import CASSETTE_REDIS_URL_ENV, _redis_url_from_env
PREFIX = "litellm:vcr:cassette:"
SCAN_BATCH = 500
@@ -14,9 +14,7 @@ SCAN_BATCH = 500
def _client() -> redis.Redis:
url = _redis_url_from_env()
if not url:
- sys.exit(
- "Set REDIS_URL, REDIS_SSL_URL, or REDIS_HOST to flush the VCR cache"
- )
+ sys.exit(f"Set {CASSETTE_REDIS_URL_ENV} to flush the VCR cache")
return redis.Redis.from_url(
url,
socket_timeout=5,
diff --git a/tests/_vcr_redis_persister.py b/tests/_vcr_redis_persister.py
index f9a5ee1aa83..4903a16068a 100644
--- a/tests/_vcr_redis_persister.py
+++ b/tests/_vcr_redis_persister.py
@@ -18,21 +18,15 @@ def redis_key_for(cassette_path: str) -> str:
return f"{REDIS_KEY_PREFIX}{rel}"
+CASSETTE_REDIS_URL_ENV = "CASSETTE_REDIS_URL"
+
+
def _redis_url_from_env() -> Optional[str]:
- for var in ("REDIS_URL", "REDIS_SSL_URL"):
- url = os.environ.get(var)
- if url:
- return url
- host = os.environ.get("REDIS_HOST")
- if not host:
- return None
- scheme = "rediss" if os.environ.get("REDIS_SSL", "").lower() == "true" else "redis"
- auth = ""
- if os.environ.get("REDIS_PASSWORD"):
- user = os.environ.get("REDIS_USERNAME", "")
- auth = f"{user}:{os.environ['REDIS_PASSWORD']}@"
- port = os.environ.get("REDIS_PORT", "6379")
- return f"{scheme}://{auth}{host}:{port}"
+ # Use a dedicated cassette Redis URL so the VCR cache is isolated from any
+ # application Redis used by tests (which may be flushed by other suites).
+ # Intentionally do NOT fall back to REDIS_URL/REDIS_HOST — sharing a Redis
+ # with the app cache risks cassettes being wiped by flushdb/flushall.
+ return os.environ.get(CASSETTE_REDIS_URL_ENV) or None
def _build_default_client():
@@ -41,7 +35,9 @@ def _build_default_client():
url = _redis_url_from_env()
if not url:
raise RuntimeError(
- "Set REDIS_URL, REDIS_SSL_URL, or REDIS_HOST to enable the VCR persister"
+ f"Set {CASSETTE_REDIS_URL_ENV} to enable the VCR persister. "
+ "Cassette Redis is intentionally separate from the application "
+ "Redis (REDIS_URL/REDIS_HOST) to avoid being flushed by tests."
)
return redis.Redis.from_url(
url,
diff --git a/tests/llm_responses_api_testing/conftest.py b/tests/llm_responses_api_testing/conftest.py
index b58508d5aee..cbf59182d77 100644
--- a/tests/llm_responses_api_testing/conftest.py
+++ b/tests/llm_responses_api_testing/conftest.py
@@ -89,9 +89,10 @@ def vcr_config():
def _vcr_disabled() -> bool:
if os.environ.get("LITELLM_VCR_DISABLE") == "1":
return True
- return not any(
- os.environ.get(var) for var in ("REDIS_URL", "REDIS_SSL_URL", "REDIS_HOST")
- )
+ # Cassettes live on a dedicated Redis (CASSETTE_REDIS_URL) so the cache
+ # isn't shared with — and accidentally flushed by — tests that exercise
+ # the application Redis via REDIS_URL/REDIS_HOST.
+ return not os.environ.get("CASSETTE_REDIS_URL")
def pytest_recording_configure(config, vcr):
diff --git a/tests/llm_translation/conftest.py b/tests/llm_translation/conftest.py
index 6969b1167d3..0691113a61f 100644
--- a/tests/llm_translation/conftest.py
+++ b/tests/llm_translation/conftest.py
@@ -113,9 +113,10 @@ def vcr_config():
def _vcr_disabled() -> bool:
if os.environ.get("LITELLM_VCR_DISABLE") == "1":
return True
- return not any(
- os.environ.get(var) for var in ("REDIS_URL", "REDIS_SSL_URL", "REDIS_HOST")
- )
+ # Cassettes live on a dedicated Redis (CASSETTE_REDIS_URL) so the cache
+ # isn't shared with — and accidentally flushed by — tests that exercise
+ # the application Redis via REDIS_URL/REDIS_HOST.
+ return not os.environ.get("CASSETTE_REDIS_URL")
def pytest_recording_configure(config, vcr):