BerriAI/litellm · error · ValueError

Valkey semantic-cache index

Error message

Valkey semantic-cache index '{self.index_name}' already exists with embedding dimension {existing_dim}, but the configured embedding model produced dimension {dim}. Use a different valkey_semantic_cache_index_name or drop the existing index.

What it means

Error "Valkey semantic-cache index '{self.index_name}' already exists with embedding dimension {existing_dim}, but the configured embedding model produced dimension {dim}. Use a different valkey_semantic_cache_index_name or drop the existing index." thrown in BerriAI/litellm.

Solutions

  1. Use a different valkey_semantic_cache_index_name so a new index is created with the correct embedding dimension.
  2. Or drop the existing index (FT.DROPINDEX) so it is recreated with the new dimension.
  3. Or switch to an embedding model whose dimension matches the existing index.
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at litellm/caching/valkey_semantic_cache.py:155 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of BerriAI/litellm@77b7c6c40c (2026-08-18). Data as JSON: /api/errors/18e67616ef35564b. Report an issue: GitHub.

Appendix: source

Thrown at litellm/caching/valkey_semantic_cache.py:155

    @staticmethod
    def _extract_index_dim(info: dict) -> int | None:
        # FT.INFO nests the vector field's "dimensions" one level inside its
        # "index" block, so flatten each field descriptor a single level and
        # scan for the dimensions marker.
        for field in info.get("attributes") or []:
            if not isinstance(field, (list, tuple)):
                continue
            flat = [sub for item in field for sub in (item if isinstance(item, (list, tuple)) else [item])]
            for i, marker in enumerate(flat):
                if marker in (b"dimensions", "dimensions") and i + 1 < len(flat):
                    return int(flat[i + 1])
        return None

    def _assert_dim_matches(self, info: dict, dim: int) -> None:
        existing_dim: Final = self._extract_index_dim(info)
        if existing_dim is not None and existing_dim != dim:
            raise ValueError(
                f"Valkey semantic-cache index '{self.index_name}' already exists with "
                f"embedding dimension {existing_dim}, but the configured embedding "
                f"model produced dimension {dim}. Use a different "
                f"valkey_semantic_cache_index_name or drop the existing index."
            )

    def _ensure_index_sync(self, dim: int) -> None:
        if self._index_dim == dim:
            return
        try:
            self.sync_client.ft(self.index_name).create_index(
                self._index_schema(dim), definition=self._index_definition()
            )
        except Exception as exc:
            if not self._is_index_exists_error(exc):
                raise
            self._assert_dim_matches(self.sync_client.ft(self.index_name).info(), dim)
        self._index_dim = dim

View on GitHub (pinned to 77b7c6c40c)