sgl-project/sglang · error · ValueError

min_new_tokens must be in [0, max_new_tokens({self.max_new_t

Error message

min_new_tokens must be in [0, max_new_tokens({self.max_new_tokens})], got {self.min_new_tokens}.

What it means

verify() enforces the invariant 0 <= min_new_tokens <= max_new_tokens when max_new_tokens is set. A min greater than max makes the generation window empty/contradictory, so the request is rejected up front during normalize().

Source

Thrown at python/sglang/srt/sampling/sampling_params.py:201

        if not 0 <= self.min_new_tokens:
            raise ValueError(
                f"min_new_tokens must be in [0, max_new_tokens], got "
                f"{self.min_new_tokens}."
            )
        if self.max_new_tokens is not None:
            if self.max_new_tokens < 0:
                raise ValueError(
                    f"max_new_tokens must be at least 0, got {self.max_new_tokens}."
                )
            if not self.min_new_tokens <= self.max_new_tokens:
                raise ValueError(
                    f"min_new_tokens must be in [0, max_new_tokens({self.max_new_tokens})], got "
                    f"{self.min_new_tokens}."
                )
        if self.logit_bias is not None:
            for token_id in self.logit_bias:
                if not 0 <= int(token_id) < vocab_size:
                    raise ValueError(
                        f"logit_bias must has keys in [0, {vocab_size - 1}], got "
                        f"{token_id}."
                    )

        grammars = [
            self.json_schema,
            self.regex,
            self.ebnf,
            self.structural_tag,
        ]  # since mutually exclusive, only one can be set
        if sum(x is not None for x in grammars) > 1:
            raise ValueError(
                "Only one of json_schema, regex, ebnf, or structural_tag can be set."
            )

    def normalize(self, tokenizer):
        # Process stop strings
        if self.stop_strs is None:

View on GitHub (pinned to 0132848349)

Solutions

  1. Raise max_new_tokens to at least min_new_tokens.
  2. Lower min_new_tokens (or drop it to 0).

Example fix

# before
SamplingParams(min_new_tokens=100, max_new_tokens=50)
# after
SamplingParams(min_new_tokens=50, max_new_tokens=100)
Defensive patterns

Strategy: validation

Validate before calling

max_new_tokens = max_new_tokens if max_new_tokens is not None else float('inf')
assert 0 <= min_new_tokens <= max_new_tokens, (min_new_tokens, max_new_tokens)

Prevention

When it happens

Trigger: Setting SamplingParams(min_new_tokens=50, max_new_tokens=20), or raising min_new_tokens for 'don't emit short answers' while a low max_new_tokens is set globally (e.g. server default or per-request override).

Common situations: Copied min_new_tokens from one config into a request with a smaller max_new_tokens; global default max_new_tokens on the server clashing with per-request min; tuning min_p/min_new_tokens for constrained-grammar decoding.

Related errors


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