sgl-project/sglang · error · ValueError

pattern must contain at least one token

Error message

pattern must contain at least one token

What it means

TokenSequenceMatcher implements KMP-style matching over token id sequences and requires a non-empty pattern to build its prefix-length table. An empty pattern has no valid failure function, so __init__ rejects it immediately.

Source

Thrown at python/sglang/srt/utils/token_sequence_matcher.py:21

# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================

from typing import Sequence


class TokenSequenceMatcher:
    def __init__(self, pattern: Sequence[int]):
        if not pattern:
            raise ValueError("pattern must contain at least one token")
        self.pattern = tuple(pattern)
        self.prefix_lengths = self._build_prefix_lengths()

    def _build_prefix_lengths(self) -> tuple[int, ...]:
        prefix_lengths = [0] * len(self.pattern)
        matched = 0
        for index in range(1, len(self.pattern)):
            while matched > 0 and self.pattern[index] != self.pattern[matched]:
                matched = prefix_lengths[matched - 1]
            if self.pattern[index] == self.pattern[matched]:
                matched += 1
            prefix_lengths[index] = matched
        return tuple(prefix_lengths)

    def __len__(self) -> int:
        return len(self.pattern)

    def advance(self, matched: int, token: int) -> int:

View on GitHub (pinned to 0132848349)

Solutions

  1. Guard before construction: if pattern: matcher = TokenSequenceMatcher(pattern)
  2. Skip matching entirely when the pattern list is empty
  3. Validate pattern lists at config-load time and require >=1 token

Example fix

# before
matcher = TokenSequenceMatcher(stop_tokens)  # may be []
# after
matcher = TokenSequenceMatcher(stop_tokens) if stop_tokens else None
if matcher and matcher.search(stream): ...
Defensive patterns

Strategy: validation

Validate before calling

if not pattern:
    return None  # or skip matching
matcher = TokenSequenceMatcher(pattern)

Type guard

def has_tokens(p) -> bool:
    return len(p) > 0

Prevention

When it happens

Trigger: Constructing TokenSequenceMatcher([]) or TokenSequenceMatcher(()) — e.g. from an empty stop-token list or a filtered list that removed every token.

Common situations: Dynamic patterns built from user input or config that can legitimately be empty; upstream filter/list comprehension yielding zero elements.

Related errors


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