pola-rs/polars · error · KeyError

{key!r} not found in cache

Error message

{key!r} not found in cache

What it means

KeyError raised by __delitem__ on the LRU cache in _utils/cache.py: code attempted to remove key {key!r} that is not present in the OrderedDict backing the cache. It is a sentinel guard for callers mutating the cache with stale or already-evicted keys.

Source

Thrown at py-polars/src/polars/_utils/cache.py:65

        >>> cache.get("xyz", "not found")
        'not found'
        """
        self._items: OrderedDict[K, V] = OrderedDict()
        self.maxsize = maxsize

    def __bool__(self) -> bool:
        """Returns True if the cache is not empty, False otherwise."""
        return bool(self._items)

    def __contains__(self, key: Any) -> bool:
        """Check if the key is in the cache."""
        return key in self._items

    def __delitem__(self, key: K) -> None:
        """Remove the item with the specified key from the cache."""
        if key not in self._items:
            msg = f"{key!r} not found in cache"
            raise KeyError(msg)
        del self._items[key]

    def __getitem__(self, key: K) -> V:
        """Raises KeyError if the key is not found."""
        if key not in self._items:
            msg = f"{key!r} not found in cache"
            raise KeyError(msg)

        # moving accessed items to the end marks them as recently used
        self._items.move_to_end(key)
        return self._items[key]

    def __iter__(self) -> Iterator[K]:
        """Iterate over the keys in the cache."""
        yield from self._items

    def __len__(self) -> int:
        """Number of items in the cache."""

View on GitHub (pinned to 9b5d73fd00)

Solutions

  1. The cache key does not exist (already evicted or never stored); check the key before access or recompute the value.

Example fix

value = cache.get(key) or recompute(key)
Defensive patterns

Strategy: validation

When it happens

Trigger: Looking up a key that is not present in the cache.

Common situations: See trigger scenarios.


AI-assisted analysis of pola-rs/polars@9b5d73fd00 (2026-08-19). Data as JSON: /api/errors/99ad020a559e49ff. Report an issue: GitHub.