litellm/tests/unit/caching/test_valkey_semantic_cache.py
yuneng-jiang a11a93f44a
test: move tests/test_litellm core utils, routing, responses, caching and rust_bridge into tests/unit (#43199)
* ci: run the unit_selection.sh shard files on every event instead of only fork pull requests

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>

* ci: rename fork-flag to unit-flag now that it applies on every event

* test: move tests/test_litellm root and small trees into tests/unit

Pure renames, no content changes. Follow-up commits in this PR fix
references, merge the three files that already existed in tests/unit,
keep live-provider tests in tests/test_litellm and wire CI.

* test: carry tests/test_litellm conftest isolation into tests/unit

Callback lists, routing fallbacks, cached HTTP clients, logger state, AWS,
proxy-URL and keychain env, and session-end client cleanup now reset for
unit tests too. The environment isolation owns its MonkeyPatch so a test's
own monkeypatch is undone before the model-cost teardown runs.

* test: merge, split and prune the moved root and small-tree tests

Merge batches/test_batch_utils.py and the chat_completions and messages
dispatch tests into the files that already existed in tests/unit. Keep
the live Gemini interactions tests, the async image-fetch format test and
the OpenAI embedding scorer test in tests/test_litellm since they need
real network or keys. Put test_router.py under tests/unit/test_router so
the existing package no longer shadows it. Delete eight tests the audit
found superseded by stronger ones kept in this move.

* ci: run the moved root and small-tree tests under their legacy flags

Add the misc and responses-caching-types flags to unit_selection.sh and
CircleCI, extend enterprise-routing and mcp-integration, and point the
legacy GHA shards, Makefile, redis-compat workflow, merge smoke manifest
and change classifier at the new paths.

* test: make the new tests/unit directories packages

tests/unit/test_package_layout.py requires every directory to carry an
__init__.py, and without one the moved and retained
test_litellm_responses_bridge.py modules collide on import.

* test: scope the unit socket block to tests/unit in shared sessions

The GHA shards collect the legacy test-path and the unit selection in one
pytest session. The unit conftest's loopback-only block leaked into legacy
modules that reach the network at import. The legacy conftest now lifts the
restriction at collect and setup time, and the unit conftest re-applies it
when collecting its own modules.

* test: move tests/test_litellm/llms into tests/unit/llms

Rename-only. Moves the provider tests and the fine-tuning fixtures they
load, mirroring the old paths. Follow-up commits merge, split and wire them.

* test: merge, split and prune the moved llms tests

Merges the Databricks chat transformation tests into the existing unit
file, keeps the tests that need real keys or the network in
tests/test_litellm, deletes the audited tests a stronger unit test
already covers, and points imports at tests.unit.llms.

* ci: run the moved llms tests under their legacy flags

The Vertex AI and All Other Providers shards keep their legacy test-path
for the retained files and add the llm-vertex-ai and llm-other-providers
unit selections. CircleCI gets matching unit jobs.

* test: make the tests/unit/llms directories packages

Adds __init__.py to the moved dirs and drops the legacy ones whose
directories no longer hold tests.

* test: drop script runners and path hacks the llms split left dangling

The __main__ runners in the split openai_like files and the Databricks e2e
runner called tests that now live in the other half of the split or were
deleted. The retained legacy halves also no longer need sys.path edits.

* test: give the shard-script tests their own GITHUB_OUTPUT

They only passed where the runner set it. The CircleCI unit job's env
allowlist drops it, so the script's redirect failed there.

* test: point the router and module-deletion checks at tests/unit

router_code_coverage and code_qa_check_tests only searched tests/test_litellm,
so the moved router tests no longer counted. The two silent-experiment tests
the audit deleted were the only direct callers of those methods; they are
replaced with tests that assert the forwarded shadow request and the
recursion guard.

* test: move tests/test_litellm integrations and secret_managers into tests/unit

Rename-only. Mirrors the old paths, including the directory conftests
and the prompt and JSON fixtures. Follow-up commits prune and wire them.

* test: prune and repoint the moved integrations tests

Deletes the 7 audited tests a stronger test in the same tree already
covers, imports the TLS sink helpers from their new conftest path, and
restores os.environ after each integrations test. Some presets write
OTEL_EXPORTER_OTLP_HEADERS straight into os.environ, and without the
legacy tree's test ordering that header leaked into the AgentOps tests.

* ci: run the moved integrations tests under their legacy flag

The integrations GHA shard and a new CircleCI job run the integrations
unit selection. secret_managers joins the misc selection.

* docs: point integrations and secret_managers references at tests/unit

* test: make the moved integrations directories packages

* test: keep the Databricks manual e2e runner and fix the SageMaker Nova run path

The Databricks e2e file is a manual script whose main() calls the tests
that were pruned, so pruning them broke the documented run. It is back to
its main version. The SageMaker Nova docstring now points at the file's
real location in tests/local_testing.

* test: move tests/test_litellm core utils, routing, responses, caching and rust_bridge into tests/unit

Rename-only. Mirrors the old paths, including fixtures, the stubtest config
and the native-route wheel script. Two files that collide with existing unit
files are merged in a follow-up commit.

* test: merge, prune and repoint the moved core, routing, responses, caching and rust_bridge tests

Merges the two files that collided with existing unit files, folding the
legacy extra case into test_is_chat_completion_cached_dict, and deletes the
9 audited tests a stronger test in the same file already covers.

Keeps what needs the network in tests/test_litellm: test_tokenizers pulls a
tokenizer from the Hugging Face hub, and the gpt2 and r50k_base tokenizer
cases download their BPE files. The unit core_utils conftest points
TIKTOKEN_CACHE_DIR at litellm's bundled encodings so the rest never depend on
import order to stay offline, and FakeSecretVault moves to a shared module
so both trees can build it.

* ci: run the moved core, routing, responses, caching and rust_bridge tests under their flags

core_utils gets a core-utils flag and CircleCI job, and its GHA shard keeps
the legacy path for the retained network tests. router_utils and
router_strategy join enterprise-routing, responses joins
responses-caching-types (minus responses/mcp, which mcp-integration owns),
caching joins caching-local and rust_bridge joins misc. The redis-compat,
test-rust, stubtest and merge-smoke paths follow the move.

* docs: point the Rust crate references at tests/unit

* test: make the moved core, routing and rust_bridge directories packages

* test: keep the no-loop DualCache batch_get_cache regression test

It runs the sync path outside any event loop, which the inside-loop test
cannot, so a change that picks the Redis client by loop state would only
show up there.

* test: keep the job's UNIT_FLAG out of the shard-script tests

* fix(url_utils): block 192.0.0.0/24 on every Python patch release

* test: move the new budget limiter tests into tests/unit/router_strategy

* test: move the new sentry scrubbing tests into tests/unit/litellm_core_utils

* test: move the new zerobus tests into tests/unit/integrations

* test: make tests/unit/integrations/zerobus a package

* test: load litellm's own tiktoken cache setup once instead of resetting it per test

---------

Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
2026-09-25 17:10:13 -07:00

536 lines
17 KiB
Python

import hashlib
import os
import struct
import subprocess
import sys
import textwrap
from types import SimpleNamespace
from unittest.mock import AsyncMock, MagicMock
import pytest
from litellm.caching.valkey_semantic_cache import ValkeySemanticCache
_REPO_ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), "../../.."))
def _make_cache(sync_client=None, async_client=None, similarity_threshold=0.8):
return ValkeySemanticCache(
similarity_threshold=similarity_threshold,
index_name="test_index",
sync_client=sync_client or MagicMock(),
async_client=async_client or AsyncMock(),
)
def _search_result(distance, response='{"content": "Paris"}'):
return SimpleNamespace(
docs=[SimpleNamespace(response=response, vector_distance=str(distance))]
)
def test_build_valkey_url_prefers_valkey_env(monkeypatch):
monkeypatch.setenv("REDIS_HOST", "redis-host")
monkeypatch.setenv("REDIS_PORT", "6379")
monkeypatch.setenv("REDIS_PASSWORD", "rpass")
monkeypatch.setenv("VALKEY_HOST", "valkey-host")
monkeypatch.setenv("VALKEY_PORT", "6380")
monkeypatch.setenv("VALKEY_PASSWORD", "vpass")
assert (
ValkeySemanticCache._build_valkey_url(None, None, None)
== "redis://:vpass@valkey-host:6380"
)
def test_build_valkey_url_supports_passwordless(monkeypatch):
monkeypatch.delenv("REDIS_PASSWORD", raising=False)
monkeypatch.delenv("VALKEY_PASSWORD", raising=False)
monkeypatch.setenv("VALKEY_HOST", "valkey-host")
monkeypatch.setenv("VALKEY_PORT", "6380")
assert (
ValkeySemanticCache._build_valkey_url(None, None, None)
== "redis://valkey-host:6380"
)
def test_build_valkey_url_falls_back_to_redis_env(monkeypatch):
monkeypatch.delenv("VALKEY_HOST", raising=False)
monkeypatch.delenv("VALKEY_PORT", raising=False)
monkeypatch.delenv("VALKEY_PASSWORD", raising=False)
monkeypatch.setenv("REDIS_HOST", "redis-host")
monkeypatch.setenv("REDIS_PORT", "6379")
monkeypatch.setenv("REDIS_PASSWORD", "rpass")
assert (
ValkeySemanticCache._build_valkey_url(None, None, None)
== "redis://:rpass@redis-host:6379"
)
def test_build_valkey_url_requires_host_and_port(monkeypatch):
for var in (
"VALKEY_HOST",
"VALKEY_PORT",
"VALKEY_PASSWORD",
"REDIS_HOST",
"REDIS_PORT",
"REDIS_PASSWORD",
):
monkeypatch.delenv(var, raising=False)
with pytest.raises(ValueError, match="Missing required Valkey configuration"):
ValkeySemanticCache._build_valkey_url(None, None, None)
def test_build_valkey_url_uses_rediss_scheme_when_ssl(monkeypatch):
monkeypatch.setenv("VALKEY_HOST", "valkey-host")
monkeypatch.setenv("VALKEY_PORT", "6379")
monkeypatch.setenv("VALKEY_PASSWORD", "vpass")
assert (
ValkeySemanticCache._build_valkey_url(None, None, None, ssl=True)
== "rediss://:vpass@valkey-host:6379"
)
assert ValkeySemanticCache._build_valkey_url(
"h", "6379", None, ssl=False
).startswith("redis://")
def test_init_requires_similarity_threshold():
with pytest.raises(ValueError, match="similarity_threshold must be provided"):
ValkeySemanticCache(sync_client=MagicMock(), async_client=AsyncMock())
def test_init_stores_embedding_max_input_tokens():
cache = ValkeySemanticCache(
similarity_threshold=0.8,
sync_client=MagicMock(),
async_client=AsyncMock(),
embedding_max_input_tokens=512,
)
assert cache.embedding_max_input_tokens == 512
assert _make_cache().embedding_max_input_tokens is None
def test_init_rejects_cluster_startup_nodes():
with pytest.raises(ValueError, match="cluster-mode-enabled"):
ValkeySemanticCache(
similarity_threshold=0.8,
startup_nodes=[{"host": "shard1", "port": 6379}],
)
def test_cache_dispatch_rejects_cluster_for_valkey_semantic():
from litellm.caching.caching import Cache
from litellm.types.caching import LiteLLMCacheType
with pytest.raises(ValueError, match="cluster-mode-enabled"):
Cache(
type=LiteLLMCacheType.VALKEY_SEMANTIC,
host="valkey-host",
port="6379",
similarity_threshold=0.8,
redis_startup_nodes=[{"host": "shard1", "port": 6379}],
)
def test_scope_tag_is_deterministic_hex():
tag = ValkeySemanticCache._scope_tag("model:gpt-4o::abc-123")
assert tag == hashlib.sha256(b"model:gpt-4o::abc-123").hexdigest()
assert len(tag) == 64
assert ValkeySemanticCache._scope_tag("a") != ValkeySemanticCache._scope_tag("b")
def test_embedding_to_bytes_is_little_endian_float32():
assert ValkeySemanticCache._embedding_to_bytes([1.0, 0.0]) == struct.pack(
"<2f", 1.0, 0.0
)
def test_set_cache_stores_scoped_doc_with_embedding(monkeypatch):
sync_client = MagicMock()
cache = _make_cache(sync_client=sync_client)
cache._get_embedding = MagicMock(return_value=[0.1, 0.2, 0.3])
cache.set_cache(
key="cache-key",
value={"content": "Paris"},
messages=[{"role": "user", "content": "What is the capital of France?"}],
)
sync_client.ft.return_value.create_index.assert_called_once()
assert sync_client.hset.call_count == 1
doc_key, kwargs = (
sync_client.hset.call_args.args[0],
sync_client.hset.call_args.kwargs,
)
mapping = kwargs["mapping"]
scope = ValkeySemanticCache._scope_tag("cache-key")
assert mapping[ValkeySemanticCache.CACHE_KEY_FIELD_NAME] == scope
assert mapping["prompt"] == "What is the capital of France?"
assert mapping["response"] == "{'content': 'Paris'}"
assert mapping["embedding"] == struct.pack("<3f", 0.1, 0.2, 0.3)
assert doc_key.startswith(f"test_index:{scope}:")
def test_set_cache_applies_ttl():
sync_client = MagicMock()
cache = _make_cache(sync_client=sync_client)
cache._get_embedding = MagicMock(return_value=[0.1, 0.2, 0.3])
cache.set_cache(
key="cache-key",
value={"content": "Paris"},
messages=[{"role": "user", "content": "What is the capital of France?"}],
ttl=60,
)
sync_client.expire.assert_called_once()
assert sync_client.expire.call_args.args[1] == 60
def test_set_cache_skips_ttl_when_absent():
sync_client = MagicMock()
cache = _make_cache(sync_client=sync_client)
cache._get_embedding = MagicMock(return_value=[0.1, 0.2, 0.3])
cache.set_cache(
key="cache-key",
value={"content": "Paris"},
messages=[{"role": "user", "content": "What is the capital of France?"}],
)
sync_client.expire.assert_not_called()
def test_get_cache_returns_hit_above_threshold():
sync_client = MagicMock()
sync_client.ft.return_value.search.return_value = _search_result(0.1)
cache = _make_cache(sync_client=sync_client, similarity_threshold=0.8)
cache._get_embedding = MagicMock(return_value=[0.1, 0.2, 0.3])
metadata = {}
result = cache.get_cache(
key="cache-key",
messages=[{"role": "user", "content": "capital of France?"}],
metadata=metadata,
)
assert result == {"content": "Paris"}
assert metadata["semantic-similarity"] == pytest.approx(0.9)
def test_get_cache_misses_below_threshold():
sync_client = MagicMock()
sync_client.ft.return_value.search.return_value = _search_result(0.5)
cache = _make_cache(sync_client=sync_client, similarity_threshold=0.8)
cache._get_embedding = MagicMock(return_value=[0.1, 0.2, 0.3])
metadata = {}
result = cache.get_cache(
key="cache-key",
messages=[{"role": "user", "content": "capital of Germany?"}],
metadata=metadata,
)
assert result is None
assert metadata["semantic-similarity"] == pytest.approx(0.5)
def test_get_cache_misses_when_no_docs():
sync_client = MagicMock()
sync_client.ft.return_value.search.return_value = SimpleNamespace(docs=[])
cache = _make_cache(sync_client=sync_client)
cache._get_embedding = MagicMock(return_value=[0.1, 0.2, 0.3])
metadata = {}
result = cache.get_cache(
key="cache-key",
messages=[{"role": "user", "content": "capital of France?"}],
metadata=metadata,
)
assert result is None
assert metadata["semantic-similarity"] == 0.0
def test_get_cache_query_filters_by_scope_tag():
sync_client = MagicMock()
sync_client.ft.return_value.search.return_value = _search_result(0.1)
cache = _make_cache(sync_client=sync_client)
cache._get_embedding = MagicMock(return_value=[0.1, 0.2, 0.3])
cache.get_cache(
key="cache-key",
messages=[{"role": "user", "content": "capital of France?"}],
metadata={},
)
query = sync_client.ft.return_value.search.call_args.args[0]
scope = ValkeySemanticCache._scope_tag("cache-key")
assert scope in query.query_string()
assert "KNN 1 @embedding" in query.query_string()
def _async_ft(search_distance):
search_obj = SimpleNamespace(
search=AsyncMock(return_value=_search_result(search_distance)),
create_index=AsyncMock(),
)
return MagicMock(return_value=search_obj)
@pytest.mark.asyncio
async def test_async_set_and_get_roundtrip():
async_client = AsyncMock()
async_client.ft = _async_ft(0.05)
cache = _make_cache(async_client=async_client, similarity_threshold=0.8)
cache._get_async_embedding = AsyncMock(return_value=[0.1, 0.2, 0.3])
await cache.async_set_cache(
key="cache-key",
value={"content": "Paris"},
messages=[{"role": "user", "content": "What is the capital of France?"}],
ttl=30,
)
async_client.hset.assert_awaited_once()
async_client.expire.assert_awaited_once()
assert async_client.expire.call_args.args[1] == 30
metadata = {}
result = await cache.async_get_cache(
key="cache-key",
messages=[{"role": "user", "content": "capital city of France"}],
metadata=metadata,
)
assert result == {"content": "Paris"}
assert metadata["semantic-similarity"] == pytest.approx(0.95)
@pytest.mark.asyncio
async def test_async_set_cache_passes_only_metadata_to_get_async_embedding():
async_client = AsyncMock()
async_client.ft = _async_ft(0.05)
cache = _make_cache(async_client=async_client)
captured: dict[str, object] = {}
async def spy_embedding(prompt: str, metadata: dict | None = None) -> list[float]:
captured["prompt"] = prompt
captured["metadata"] = metadata
return [0.1, 0.2, 0.3]
cache._get_async_embedding = spy_embedding
await cache.async_set_cache(
key="cache-key",
value={"content": "Paris"},
messages=[{"role": "user", "content": "What is the capital of France?"}],
metadata={"user_api_key": "sk-test"},
cache_key="abc123",
custom_llm_provider="openai",
)
assert captured["metadata"] == {"user_api_key": "sk-test"}
async_client.hset.assert_awaited_once()
@pytest.mark.asyncio
async def test_async_get_cache_passes_only_metadata_to_get_async_embedding():
async_client = AsyncMock()
async_client.ft = _async_ft(0.05)
cache = _make_cache(async_client=async_client)
captured: dict[str, object] = {}
async def spy_embedding(prompt: str, metadata: dict | None = None) -> list[float]:
captured["prompt"] = prompt
captured["metadata"] = dict(metadata) if metadata is not None else None
return [0.1, 0.2, 0.3]
cache._get_async_embedding = spy_embedding
result = await cache.async_get_cache(
key="cache-key",
messages=[{"role": "user", "content": "What is the capital of France?"}],
metadata={"user_api_key": "sk-test"},
cache_key="abc123",
custom_llm_provider="openai",
)
assert result == {"content": "Paris"}
assert captured["metadata"] == {"user_api_key": "sk-test"}
@pytest.mark.asyncio
async def test_async_get_cache_misses_below_threshold():
async_client = AsyncMock()
async_client.ft = _async_ft(0.4)
cache = _make_cache(async_client=async_client, similarity_threshold=0.8)
cache._get_async_embedding = AsyncMock(return_value=[0.1, 0.2, 0.3])
metadata = {}
result = await cache.async_get_cache(
key="cache-key",
messages=[{"role": "user", "content": "capital of Germany?"}],
metadata=metadata,
)
assert result is None
assert metadata["semantic-similarity"] == pytest.approx(0.6)
def test_ensure_index_swallows_already_exists():
sync_client = MagicMock()
sync_client.ft.return_value.create_index.side_effect = Exception(
"Index test_index already exists."
)
cache = _make_cache(sync_client=sync_client)
cache._ensure_index_sync(3)
assert cache._index_dim == 3
def test_ensure_index_reraises_unexpected_error():
sync_client = MagicMock()
sync_client.ft.return_value.create_index.side_effect = Exception(
"connection refused"
)
cache = _make_cache(sync_client=sync_client)
with pytest.raises(Exception, match="connection refused"):
cache._ensure_index_sync(3)
_FT_INFO_ATTRS_DIM_1536 = [
[b"identifier", b"litellm_cache_key", b"type", b"TAG"],
[
b"identifier",
b"embedding",
b"type",
b"VECTOR",
b"index",
[b"capacity", 10240, b"dimensions", 1536, b"distance_metric", b"COSINE"],
],
]
def test_extract_index_dim_parses_nested_ft_info():
info = {"attributes": _FT_INFO_ATTRS_DIM_1536}
assert ValkeySemanticCache._extract_index_dim(info) == 1536
assert ValkeySemanticCache._extract_index_dim({"attributes": []}) is None
def test_ensure_index_raises_on_dimension_mismatch():
sync_client = MagicMock()
sync_client.ft.return_value.create_index.side_effect = Exception(
"Index test_index already exists."
)
sync_client.ft.return_value.info.return_value = {
"attributes": _FT_INFO_ATTRS_DIM_1536
}
cache = _make_cache(sync_client=sync_client)
with pytest.raises(
ValueError, match="already exists with embedding dimension 1536"
):
cache._ensure_index_sync(768)
assert cache._index_dim is None
def test_ensure_index_accepts_matching_existing_dimension():
sync_client = MagicMock()
sync_client.ft.return_value.create_index.side_effect = Exception(
"Index test_index already exists."
)
sync_client.ft.return_value.info.return_value = {
"attributes": _FT_INFO_ATTRS_DIM_1536
}
cache = _make_cache(sync_client=sync_client)
cache._ensure_index_sync(1536)
assert cache._index_dim == 1536
def test_init_builds_only_missing_client_from_url():
sync_client = MagicMock()
cache = ValkeySemanticCache(
similarity_threshold=0.8,
redis_url="redis://valkey-host:6380",
sync_client=sync_client,
)
assert cache.sync_client is sync_client
assert cache.async_client is not None and cache.async_client is not sync_client
def test_init_uses_both_injected_clients_without_connection_info(monkeypatch):
for var in ("VALKEY_HOST", "VALKEY_PORT", "REDIS_HOST", "REDIS_PORT"):
monkeypatch.delenv(var, raising=False)
sync_client = MagicMock()
async_client = AsyncMock()
cache = ValkeySemanticCache(
similarity_threshold=0.8,
sync_client=sync_client,
async_client=async_client,
)
assert cache.sync_client is sync_client
assert cache.async_client is async_client
def test_cache_dispatches_valkey_semantic_type():
from litellm.caching.caching import Cache
from litellm.types.caching import LiteLLMCacheType
cache = Cache(
type=LiteLLMCacheType.VALKEY_SEMANTIC,
host="valkey-host",
port="6380",
similarity_threshold=0.8,
)
assert isinstance(cache.cache, ValkeySemanticCache)
@pytest.mark.asyncio
async def test_index_info_uses_valkey_ft_info():
# The /health/readiness endpoint calls _index_info() on any
# RedisSemanticCache instance; since ValkeySemanticCache subclasses it,
# the inherited RedisVL implementation (which reads self.llmcache) would
# break. This override must query valkey-search FT.INFO instead.
async_client = AsyncMock()
info_namespace = SimpleNamespace(info=AsyncMock(return_value={"num_docs": 3}))
async_client.ft = MagicMock(return_value=info_namespace)
cache = _make_cache(async_client=async_client)
result = await cache._index_info()
assert result == {"num_docs": 3}
async_client.ft.assert_called_once_with("test_index")
def test_importing_caching_does_not_require_redis():
# redis is an optional dependency (extra_proxy), so the base SDK can be
# installed without it. Selecting valkey-semantic needs redis, but merely
# importing litellm.caching.caching must not, or `import litellm` breaks for
# every base-SDK user. This runs in a subprocess with redis blocked so the
# check is not polluted by redis already being imported in this session.
code = textwrap.dedent("""
import sys
for name in ("redis", "redis.asyncio", "redis.commands",
"redis.commands.search"):
sys.modules[name] = None
import litellm.caching.caching # must not import redis at module top
from litellm.types.caching import LiteLLMCacheType
assert LiteLLMCacheType.VALKEY_SEMANTIC == "valkey-semantic"
print("ok")
""")
result = subprocess.run(
[sys.executable, "-c", code],
capture_output=True,
text=True,
env={**os.environ, "PYTHONPATH": _REPO_ROOT},
)
assert result.returncode == 0, result.stderr
assert "ok" in result.stdout