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): refuse to persist cassettes past 50 episodes

mateo-berria47c4e7d1b6b
diff --git a/tests/_vcr_redis_persister.py b/tests/_vcr_redis_persister.py
index 08e7d09c6ad..3483f9d2533 100644
--- a/tests/_vcr_redis_persister.py
+++ b/tests/_vcr_redis_persister.py
@@ -10,6 +10,15 @@ from vcr.serialize import deserialize, serialize
CASSETTE_TTL_SECONDS = 24 * 60 * 60
REDIS_KEY_PREFIX = "litellm:vcr:cassette:"
+# Healthy cassettes hold 1–5 episodes (a single test rarely makes more than a
+# handful of distinct HTTP calls). When a cassette balloons past this, it
+# usually means a test produces non-deterministic request bodies (e.g. uuid)
+# under record_mode=new_episodes, and every CI run is appending fresh
+# unmatched episodes instead of replaying. That growth is unbounded over
+# time and silently inflates Redis. Refuse to persist past this threshold so
+# the pathology surfaces loudly instead.
+MAX_EPISODES_PER_CASSETTE = 50
+
_log = logging.getLogger(__name__)
@@ -124,6 +133,23 @@ def make_redis_persister(
# (e.g. cassette saved outside a test context) so non-test usage
# still works.
passed = _passed_by_cassette_key.pop(key, True)
+ episode_count = len(cassette_dict.get("requests", []) or [])
+ if episode_count > MAX_EPISODES_PER_CASSETTE:
+ # Pathology: the test is producing non-deterministic request
+ # bodies and accumulating unbounded episodes. Refuse the save
+ # so the cassette can't keep ballooning, and surface a loud
+ # warning so someone investigates / opts the test out.
+ _log.warning(
+ "VCR redis save refused for %s; cassette has %d episodes "
+ "(> MAX_EPISODES_PER_CASSETTE=%d). The test likely produces "
+ "non-deterministic request bodies (e.g. uuid) and is "
+ "appending instead of replaying. Opt it out with the "
+ "no-vcr list in conftest, or stabilize its request body.",
+ cassette_path,
+ episode_count,
+ MAX_EPISODES_PER_CASSETTE,
+ )
+ return
if not passed:
_log.info(
"VCR redis save skipped for %s; test did not pass — "
diff --git a/tests/llm_translation/test_vcr_redis_persister.py b/tests/llm_translation/test_vcr_redis_persister.py
index 7d345cbcfe2..1e4b6c83aab 100644
--- a/tests/llm_translation/test_vcr_redis_persister.py
+++ b/tests/llm_translation/test_vcr_redis_persister.py
@@ -14,6 +14,7 @@ sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "..",
from tests._vcr_redis_persister import ( # noqa: E402
CASSETTE_TTL_SECONDS,
+ MAX_EPISODES_PER_CASSETTE,
filter_non_2xx_response,
make_redis_persister,
mark_test_outcome_for_cassette,
@@ -151,6 +152,62 @@ def test_save_proceeds_when_test_marked_passed():
assert fake.get(key) is not None
+def test_save_refused_when_cassette_exceeds_max_episodes():
+ # Pathological cassettes (non-deterministic body → unbounded episode growth)
+ # should be refused. Any prior good payload stays intact.
+ fake, persister = _persister_with_fake_redis()
+ cassette_id = "tests/llm_translation/test_x/test_runaway"
+ key = redis_key_for(cassette_id)
+
+ persister.save_cassette(cassette_id, _sample_cassette_dict(), yamlserializer)
+ seed_payload = fake.get(key)
+
+ request = Request(
+ method="POST",
+ uri="https://api.anthropic.com/v1/messages",
+ body=b"x",
+ headers={"content-type": "application/json"},
+ )
+ response = {
+ "status": {"code": 200, "message": "OK"},
+ "headers": {},
+ "body": {"string": b"{}"},
+ }
+ bloated = {
+ "requests": [request] * (MAX_EPISODES_PER_CASSETTE + 1),
+ "responses": [response] * (MAX_EPISODES_PER_CASSETTE + 1),
+ }
+ persister.save_cassette(cassette_id, bloated, yamlserializer)
+
+ # Refused — the seed payload is unchanged.
+ assert fake.get(key) == seed_payload
+
+
+def test_save_proceeds_at_max_episodes_threshold():
+ fake, persister = _persister_with_fake_redis()
+ cassette_id = "tests/llm_translation/test_x/test_at_threshold"
+ key = redis_key_for(cassette_id)
+
+ request = Request(
+ method="POST",
+ uri="https://api.anthropic.com/v1/messages",
+ body=b"x",
+ headers={"content-type": "application/json"},
+ )
+ response = {
+ "status": {"code": 200, "message": "OK"},
+ "headers": {},
+ "body": {"string": b"{}"},
+ }
+ at_threshold = {
+ "requests": [request] * MAX_EPISODES_PER_CASSETTE,
+ "responses": [response] * MAX_EPISODES_PER_CASSETTE,
+ }
+ persister.save_cassette(cassette_id, at_threshold, yamlserializer)
+
+ assert fake.get(key) is not None
+
+
def test_save_proceeds_when_outcome_unknown():
# Used outside a pytest run (e.g. ad-hoc scripts), the outcome gate is
# bypassed so the persister still works.