sgl-project/sglang · error · ValueError

RadixKey slice step must be 1

Error message

RadixKey slice step must be 1

What it means

RadixKey.__getitem__ only supports slices with step 1; any slice like key[::2] or key[::-1] raises ValueError. The implementation converts the slice via slice.indices(len(self)) and rejects step != 1 because strided subkeys have no meaning in the radix tree.

Source

Thrown at python/sglang/srt/mem_cache/radix_cache.py:128

            for i in range(n - 1 if n > 0 else 0):
                yield (t[i], t[i + 1])
        elif n == len(t):
            yield from t
        else:
            for i in range(n):
                yield t[i]

    def __getitem__(self, idx: Union[int, slice]) -> RadixKey:
        # Normalize int -> 1-element slice so the rest handles one shape.
        if isinstance(idx, int):
            if idx < 0:
                idx += len(self)
            if idx < 0 or idx >= len(self):
                raise IndexError(f"RadixKey index out of range: {idx}")
            idx = slice(idx, idx + 1)
        start, stop, step = idx.indices(len(self))
        if step != 1:
            raise ValueError("RadixKey slice step must be 1")

        if self.is_bigram:
            # bigrams [start, stop) span raw tokens [start, stop + 1);
            # empty slice -> empty raw tokens (not a dangling boundary token).
            raw = self.token_ids[start : stop + 1] if stop > start else array("q")
            return RadixKey(
                raw,
                self.extra_key,
                is_bigram=True,
                cache_salt=self.cache_salt,
            )
        return RadixKey(
            self.token_ids[start:stop],
            self.extra_key,
            cache_salt=self.cache_salt,
        )

    def __repr__(self) -> str:

View on GitHub (pinned to 0132848349)

Solutions

  1. Use contiguous slices key[start:stop] only
  2. Materialize to token ids first if you truly need striding: tokens = key.token_ids[::2] (then rebuild a RadixKey if needed)

Example fix

# before
sub = key[::2]
# after
sub = key[:len(key)]  # contiguous only; stride on token_ids if needed
Defensive patterns

Strategy: validation

Validate before calling

assert idx.step in (None, 1), "RadixKey slices must have step 1"

Prevention

When it happens

Trigger: Calling key[::2], key[::-1], or any key[start:stop:step] with step != 1 on a RadixKey instance.

Common situations: Reusing generic tensor/list slicing code (which supports strides) on RadixKey; attempting to reverse a key for suffix matching.

Related errors


AI-assisted analysis of sgl-project/sglang@0132848349 (2026-08-28). Data as JSON: /api/errors/eda052dfaad9c847. Report an issue: GitHub.