{"record":{"id":"681dc2d1cf364a71","repo":"sgl-project/sglang","slug":"min-new-tokens-must-be-in-0-max-new-tokens-got","errorCode":null,"errorMessage":"min_new_tokens must be in [0, max_new_tokens], got {self.min_new_tokens}.","messagePattern":"min_new_tokens must be in \\[0, max_new_tokens\\], got (.+?)\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"python/sglang/srt/sampling/sampling_params.py","lineNumber":190,"sourceCode":"                f\"{self.frequency_penalty}.\"\n            )\n        if not -2.0 <= self.presence_penalty <= 2.0:\n            raise ValueError(\n                \"presence_penalty must be in [-2, 2], got \" f\"{self.presence_penalty}.\"\n            )\n        if not 0.0 < self.repetition_penalty <= 2.0:\n            raise ValueError(\n                \"repetition_penalty must be in (0, 2] (1.0 = no penalty), \"\n                f\"got {self.repetition_penalty}.\"\n            )\n        if not 0 <= self.min_new_tokens:\n            raise ValueError(\n                f\"min_new_tokens must be in [0, max_new_tokens], got \"\n                f\"{self.min_new_tokens}.\"\n            )\n        if self.max_new_tokens is not None:\n            if self.max_new_tokens < 0:\n                raise ValueError(\n                    f\"max_new_tokens must be at least 0, got {self.max_new_tokens}.\"\n                )\n            if not self.min_new_tokens <= self.max_new_tokens:\n                raise ValueError(\n                    f\"min_new_tokens must be in [0, max_new_tokens({self.max_new_tokens})], got \"\n                    f\"{self.min_new_tokens}.\"\n                )\n        if self.logit_bias is not None:\n            for token_id in self.logit_bias:\n                if not 0 <= int(token_id) < vocab_size:\n                    raise ValueError(\n                        f\"logit_bias must has keys in [0, {vocab_size - 1}], got \"\n                        f\"{token_id}.\"\n                    )\n\n        grammars = [\n            self.json_schema,\n            self.regex,","sourceCodeStart":172,"sourceCodeEnd":208,"githubUrl":"https://github.com/sgl-project/sglang/blob/0132848349585cfe6aae51c4941cbae872505f8a/python/sglang/srt/sampling/sampling_params.py#L172-L208","documentation":"SamplingParams.verify() requires min_new_tokens to be >= 0 (and later <= max_new_tokens when max_new_tokens is set). A negative min_new_tokens raises this ValueError during normalize()/verify(). min_new_tokens forces the model to generate at least N tokens by suppressing EOS until the count is reached.","triggerScenarios":"Passing SamplingParams(min_new_tokens=-1); also passing min_new_tokens greater than max_new_tokens triggers the companion check further down. verify() runs via normalize() per request.","commonSituations":"Computing min_new_tokens as target_length - current_length and going negative when the prompt already exceeds the target; config defaults of -1 used as 'unset'; off-by-one arithmetic in token budgeting.","solutions":["Clamp min_new_tokens to >= 0 (use 0 to disable)","Ensure min_new_tokens <= max_new_tokens when max_new_tokens is set","Compute min_new_tokens defensively: max(0, min(min_new, max_new or min_new))"],"exampleFix":"# before\nparams = SamplingParams(min_new_tokens=min_len, max_new_tokens=max_len)  # min_len may be negative\n# after\nparams = SamplingParams(min_new_tokens=max(0, min_len), max_new_tokens=max(max_len, 0))","handlingStrategy":"validation","validationCode":"min_new_tokens = max(0, min_new_tokens)\nif max_new_tokens is not None:\n    max_new_tokens = max(0, max_new_tokens)\n    min_new_tokens = min(min_new_tokens, max_new_tokens)\nparams = SamplingParams(min_new_tokens=min_new_tokens, max_new_tokens=max_new_tokens)","typeGuard":"def valid_min_new_tokens(mn, mx=None):\n    return isinstance(mn, int) and mn >= 0 and (mx is None or mn <= mx)","tryCatchPattern":"try:\n    llm.generate(prompts, SamplingParams(min_new_tokens=mn, max_new_tokens=mx))\nexcept ValueError as e:\n    if 'min_new_tokens' in str(e):\n        mn = 0\n    else:\n        raise","preventionTips":["Clamp computed min_new_tokens to >= 0 before building params","Keep the invariant 0 <= min_new_tokens <= max_new_tokens in token-budget math"],"tags":["sampling-params","min-new-tokens","validation","sglang"],"backgroundTag":"sampling-parameter-validation-failed","analyzedSha":"0132848349585cfe6aae51c4941cbae872505f8a","analyzedAt":"2026-08-28T05:10:05.995Z","schemaVersion":2},"datasetVersion":"2026-08-28T06:17:29.519Z"}