redis/redis-py · error · ValueError

Item not found

Error message

Item not found

What it means

WeightedList.remove (redis/data_structure.py:39) raises a plain ValueError('Item not found') when the item being removed is not present in the internal sorted list. WeightedList is a thread-safe weighted container used internally by the cluster client (e.g. for weighted primary/replica selection); calling remove on an item that was never added, already removed, or passed with a different identity/value raises this.

Solutions

  1. If calling WeightedList directly, guard with a membership check (iterate, since it has no __contains__) before remove, or catch ValueError.
  2. For update_weight, ensure the item you are re-weighting was added with the exact same identity.
  3. In cluster code, treat a stray ValueError here as a topology-staleness signal and trigger a slot-map refresh rather than crashing.

Example fix

// before
wl.remove(node)  # ValueError if already removed
// after
if any(stored is node for stored, _ in wl):
    wl.remove(node)
Defensive patterns

Strategy: try-catch

Validate before calling

def safe_remove(wl, item):
    if any(stored is item or stored == item for stored, _ in wl):
        return wl.remove(item)
    return None

Try / catch

try:
    wl.remove(item)
except ValueError:
    pass  # already absent

Prevention

When it happens

Trigger: Calling WeightedList.remove(item) twice; removing an item that was never add()'ed; passing an object whose __eq__ does not match the stored instance. Also reached transitively through update_weight, which calls remove internally, for a stale item.

Common situations: Mostly an internal-API error seen by contributors extending routing/replica selection. End users essentially never call WeightedList directly; it surfaces as an unexpected ValueError during cluster topology refresh or replica routing if the slot map and weighted list fall out of sync after a reshard/failover.

Related errors


AI-assisted analysis of redis/redis-py@6a6b581b48 (2026-08-10). Data as JSON: /api/errors/76b26301f4b0ca8f. Report an issue: GitHub.

Appendix: source

Thrown at redis/data_structure.py:39

            # Find insertion point using binary search
            left, right = 0, len(self._items)
            while left < right:
                mid = (left + right) // 2
                if self._items[mid][1] < weight:
                    right = mid
                else:
                    left = mid + 1

            self._items.insert(left, (item, weight))

    def remove(self, item):
        """Remove first occurrence of item"""
        with self._lock:
            for i, (stored_item, weight) in enumerate(self._items):
                if stored_item == item:
                    self._items.pop(i)
                    return weight
            raise ValueError("Item not found")

    def get_by_weight_range(
        self, min_weight: float, max_weight: float
    ) -> List[tuple[Any, Number]]:
        """Get all items within weight range"""
        with self._lock:
            result = []
            for item, weight in self._items:
                if min_weight <= weight <= max_weight:
                    result.append((item, weight))
            return result

    def get_top_n(self, n: int) -> List[tuple[Any, Number]]:
        """Get top N the highest weighted items"""
        with self._lock:
            return [(item, weight) for item, weight in self._items[:n]]

    def update_weight(self, item, new_weight: float):

View on GitHub (pinned to 6a6b581b48)