{"record":{"id":"55ae46397c983fc7","repo":"sgl-project/sglang","slug":"top-p-must-be-in-0-1-got-self-top-p","errorCode":null,"errorMessage":"top_p must be in (0, 1], got {self.top_p}.","messagePattern":"top_p must be in \\(0, 1\\], got (.+?)\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"python/sglang/srt/sampling/sampling_params.py","lineNumber":162,"sourceCode":"        self.structural_tag = self.structural_tag or None\n\n        # Process some special cases\n        if 0 <= self.temperature < _SAMPLING_EPS:\n            # top_k = 1 means greedy sampling\n            self.temperature = 1.0\n            self.top_k = 1\n        if self.top_k == -1:\n            self.top_k = TOP_K_ALL  # whole vocabulary\n\n    def verify(self, vocab_size):\n        if self.beam_width is not None and self.beam_width < 1:\n            raise ValueError(f\"beam_width must be at least 1, got {self.beam_width}.\")\n        if not math.isfinite(self.temperature) or self.temperature < 0.0:\n            raise ValueError(\n                f\"temperature must be a non-negative finite number, got {self.temperature}.\"\n            )\n        if not 0.0 < self.top_p <= 1.0:\n            raise ValueError(f\"top_p must be in (0, 1], got {self.top_p}.\")\n        if not 0.0 <= self.min_p <= 1.0:\n            raise ValueError(f\"min_p must be in [0, 1], got {self.min_p}.\")\n        if self.top_k < 1 or self.top_k == -1:\n            raise ValueError(\n                f\"top_k must be -1 (disable) or at least 1, got {self.top_k}.\"\n            )\n        if not -2.0 <= self.frequency_penalty <= 2.0:\n            raise ValueError(\n                \"frequency_penalty must be in [-2, 2], got \"\n                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), \"","sourceCodeStart":144,"sourceCodeEnd":180,"githubUrl":"https://github.com/sgl-project/sglang/blob/0132848349585cfe6aae51c4941cbae872505f8a/python/sglang/srt/sampling/sampling_params.py#L144-L180","documentation":"SamplingParams.verify() requires top_p to lie in the exclusive-inclusive interval (0, 1] for nucleus sampling. Values of 0, negative numbers, or anything above 1 raise this ValueError during normalize()/verify(). top_p=0 would select an empty candidate set, hence it is explicitly disallowed even though 0 < 1 numerically.","triggerScenarios":"Passing SamplingParams(top_p=0) (e.g. intending greedy), top_p=1.5, or a negative value; verify() is called via normalize() when the request is prepared.","commonSituations":"Porting configs from other engines where top_p=0 disables nucleus sampling; sliders/UIs allowing 0; multiplying top_p by a factor that pushes it above 1.","solutions":["For greedy decoding use top_k=1 and/or temperature=0 instead of top_p=0","Set top_p=1.0 to disable nucleus filtering","If you need very aggressive truncation use a tiny positive value like top_p=0.01"],"exampleFix":"# before\nparams = SamplingParams(top_p=0)   # invalid\n# after\nparams = SamplingParams(top_p=1.0, temperature=0.0)  # greedy decoding","handlingStrategy":"validation","validationCode":"top_p = top_p if isinstance(top_p,(int,float)) and 0.0 < top_p <= 1.0 else 1.0\nparams = SamplingParams(top_p=top_p)","typeGuard":"def valid_top_p(p):\n    return isinstance(p,(int,float)) and 0.0 < p <= 1.0","tryCatchPattern":"try:\n    llm.generate(prompts, SamplingParams(top_p=p))\nexcept ValueError as e:\n    if 'top_p' in str(e):\n        p = 1.0\n    else:\n        raise","preventionTips":["Use top_p=1.0 (not 0) to disable nucleus sampling","Clamp UI slider values into (0, 1] before submission"],"tags":["sampling-params","top-p","nucleus-sampling","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"}