Merge pull request #27025 from BerriAI/litellm_fix-redis-key-generation-d88e

Fix Redis key generation to be stable across working directories
This commit is contained in:
yuneng-jiang 2026-05-01 16:41:12 -07:00 committed by GitHub
commit 12c9a477f1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 32 additions and 1 deletions

View file

@ -13,6 +13,8 @@ CASSETTE_REDIS_URL_ENV = "CASSETTE_REDIS_URL"
VCR_VERBOSE_ENV = "LITELLM_VCR_VERBOSE"
MAX_EPISODES_PER_CASSETTE = 50
_REPO_ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
_log = logging.getLogger(__name__)
_passed_by_cassette_key: dict[str, bool] = {}
@ -22,7 +24,11 @@ def mark_test_outcome_for_cassette(cassette_path: str, passed: bool) -> None:
def redis_key_for(cassette_path: str) -> str:
rel = os.path.relpath(str(cassette_path))
abs_path = os.path.abspath(str(cassette_path))
try:
rel = os.path.relpath(abs_path, start=_REPO_ROOT)
except ValueError:
rel = os.path.basename(abs_path)
if rel.endswith(".yaml"):
rel = rel[: -len(".yaml")]
rel = rel.replace("/cassettes/", "/").lstrip("./")

View file

@ -81,6 +81,31 @@ def test_redis_key_normalizes_path_passed_by_pytest_recording():
)
def test_redis_key_is_stable_across_working_directories(tmp_path, monkeypatch):
repo_root = os.path.dirname(
os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
)
abs_cassette = os.path.join(
repo_root,
"tests/llm_translation/cassettes/test_anthropic/test_streaming.yaml",
)
monkeypatch.chdir(repo_root)
key_from_root = redis_key_for(abs_cassette)
monkeypatch.chdir(os.path.join(repo_root, "tests", "llm_translation"))
key_from_subdir = redis_key_for(abs_cassette)
monkeypatch.chdir(tmp_path)
key_from_tmp = redis_key_for(abs_cassette)
assert key_from_root == key_from_subdir == key_from_tmp
assert (
key_from_root
== "litellm:vcr:cassette:tests/llm_translation/test_anthropic/test_streaming"
)
class _FlakyRedis:
def __init__(self, inner, fail_on: str):
self._inner = inner