sgl-project/sglang · error · ValueError

Strict reasoning format requested but the grammar backend do

Error message

Strict reasoning format requested but the grammar backend does not support token filtering. Use a grammar backend that supports token filtering (e.g., xgrammar) or disable strict reasoning mode.

What it means

ReasonerGrammarBackend needs the wrapped grammar backend's token-filter capability to enforce strict reasoning format; if enable_token_filter is on but grammar_backend.is_support_token_filter is False, it raises this ValueError at construction.

Source

Thrown at python/sglang/srt/constrained/reasoner_grammar_backend.py:274

        if not think_end_ids:
            raise ValueError(
                f"think_end_token '{reasoning_parser.detector.think_end_token}' "
                f"could not be encoded by the tokenizer."
            )
        self.think_end_ids = think_end_ids
        self._enable_strict_thinking = enable_strict_thinking
        self.think_excluded_token_ids = self._get_think_excluded_token_ids(
            reasoning_parser, tokenizer
        )
        self.max_think_tokens = envs.SGLANG_MAX_THINK_TOKENS.get()
        self.enable_token_filter = self.enable_strict_thinking and (
            self.think_excluded_token_ids is not None or self.max_think_tokens >= 0
        )
        if (
            self.enable_token_filter
            and not self.grammar_backend.is_support_token_filter
        ):
            raise ValueError(
                "Strict reasoning format requested but the grammar backend does not "
                "support token filtering. Use a grammar backend that supports token "
                "filtering (e.g., xgrammar) or disable strict reasoning mode."
            )
        self._token_filter_fn = (
            self.grammar_backend.set_token_filter if self.enable_token_filter else None
        )

    def _get_think_excluded_token_ids(
        self,
        reasoning_parser: ReasoningParser,
        tokenizer: Union[PreTrainedTokenizer, PreTrainedTokenizerFast],
    ) -> Optional[List[int]]:
        excluded_ids = []
        if (not self.enable_strict_thinking) or (
            not reasoning_parser.detector.think_excluded_tokens
        ):
            return None

View on GitHub (pinned to 0132848349)

Solutions

  1. Switch to --grammar-backend xgrammar, which supports token filtering
  2. Disable strict reasoning mode (drop think-excluded-token / max-think-token enforcement flags)
  3. If implementing a custom backend, add is_support_token_filter=True and a set_token_filter method

Example fix

# before
--grammar-backend none --max-think-tokens 1024
# after
--grammar-backend xgrammar --max-think-tokens 1024
Defensive patterns

Strategy: validation

Validate before calling

if strict_reasoning_enabled:
    assert grammar_backend.is_support_token_filter, 'backend must support token filtering'

Type guard

def supports_token_filter(b) -> bool:
    return bool(getattr(b, 'is_support_token_filter', False))

Prevention

When it happens

Trigger: Wrapping a backend without token filtering (e.g. 'none' or a backend lacking set_token_filter) while strict reasoning options (think_excluded tokens or max_think_tokens) request filtering.

Common situations: Using --grammar-backend llguidance/none with strict thinking flags; custom grammar backends that don't implement set_token_filter.

Related errors


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