{"record":{"id":"b50e3a65e8d5dcc1","repo":"sgl-project/sglang","slug":"max-new-tokens-must-be-at-least-0-got-self-max-n","errorCode":null,"errorMessage":"max_new_tokens must be at least 0, got {self.max_new_tokens}.","messagePattern":"max_new_tokens must be at least 0, got (.+?)\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"python/sglang/srt/sampling/sampling_params.py","lineNumber":194,"sourceCode":"                \"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,\n            self.ebnf,\n            self.structural_tag,\n        ]  # since mutually exclusive, only one can be set\n        if sum(x is not None for x in grammars) > 1:","sourceCodeStart":176,"sourceCodeEnd":212,"githubUrl":"https://github.com/sgl-project/sglang/blob/0132848349585cfe6aae51c4941cbae872505f8a/python/sglang/srt/sampling/sampling_params.py#L176-L212","documentation":"SamplingParams.verify() validates that max_new_tokens, when set, is non-negative. It is raised during normalize() (which calls verify) before the request is scheduled, because a negative generation budget is meaningless and would corrupt scheduling math.","triggerScenarios":"Passing SamplingParams(max_new_tokens=-1) (or a negative value) to a generate/launch request; often happens when max_new_tokens is computed as a subtraction like context_len - prompt_len that goes negative, or when a typo/CLI arg passes a negative number.","commonSituations":"Dynamic token-budget computation underflows (prompt longer than the cap), off-by-one or negated arithmetic in a wrapper, or passing -1 meaning 'unlimited' (vLLM-style) into SGLang, which expects None for unlimited.","solutions":["Set max_new_tokens to None if you mean 'no limit', or a non-negative integer.","Fix the arithmetic that produced a negative value (e.g. clamp max(0, budget) or skip the request when the prompt exceeds the cap).","Validate inputs before constructing SamplingParams."],"exampleFix":"# before\nsp = SamplingParams(max_new_tokens=context_len - len(prompt_ids))  # can be -1\n# after\nsp = SamplingParams(max_new_tokens=max(0, context_len - len(prompt_ids)))\n# or, for no limit:\nsp = SamplingParams(max_new_tokens=None)","handlingStrategy":"validation","validationCode":"if max_new_tokens is not None and max_new_tokens < 0:\n    max_new_tokens = None  # or clamp to 0 / raise your own error\nsp = SamplingParams(max_new_tokens=max_new_tokens)","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Never use -1 to mean unlimited; use None.","Clamp computed budgets with max(0, budget) and skip when the prompt overflows the context."],"tags":["sampling-params","validation","max-new-tokens","sglang"],"backgroundTag":"invalid-parameter-validation","analyzedSha":"0132848349585cfe6aae51c4941cbae872505f8a","analyzedAt":"2026-08-28T05:10:05.995Z","schemaVersion":2},"datasetVersion":"2026-08-28T06:17:29.519Z"}