{"record":{"id":"57a1042a02080126","repo":"sgl-project/sglang","slug":"temperature-must-be-a-non-negative-finite-number","errorCode":null,"errorMessage":"temperature must be a non-negative finite number, got {self.temperature}.","messagePattern":"temperature must be a non-negative finite number, got (.+?)\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"python/sglang/srt/sampling/sampling_params.py","lineNumber":158,"sourceCode":"        # An empty grammar constraint means \"unset\", not \"constrain to nothing\".\n        self.json_schema = self.json_schema or None\n        self.regex = self.regex or None\n        self.ebnf = self.ebnf or None\n        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}.\"","sourceCodeStart":140,"sourceCodeEnd":176,"githubUrl":"https://github.com/sgl-project/sglang/blob/0132848349585cfe6aae51c4941cbae872505f8a/python/sglang/srt/sampling/sampling_params.py#L140-L176","documentation":"SamplingParams.verify() requires temperature to be a finite, non-negative number. NaN, +inf/-inf, or any negative value raises this ValueError during normalize()/verify() before scheduling. Temperature is applied as a softmax divisor, so infinite or negative values are mathematically invalid.","triggerScenarios":"Passing SamplingParams(temperature=float('nan')), float('inf'), a negative number, or a string like 'nan' that gets cast; verify() runs via normalize() on every request.","commonSituations":"Computing temperature dynamically (e.g. log-scaled or decayed) and hitting NaN from a division by zero; JSON configs with \"NaN\"/\"Infinity\" literals; copying temperature=-1 from another framework meaning 'disabled'.","solutions":["Use temperature=0.0 for greedy decoding (or 1.0 for no scaling)","Fix the upstream computation producing NaN/inf (guard divisions, clamp values)","Clamp temperature before constructing params: max(0.0, min(t, 10.0))"],"exampleFix":"# before\nparams = SamplingParams(temperature=temperature)  # temperature may be NaN\n# after\nif not math.isfinite(temperature) or temperature < 0:\n    temperature = 0.0\nparams = SamplingParams(temperature=temperature)","handlingStrategy":"validation","validationCode":"import math\ntemperature = temperature if (isinstance(temperature,(int,float)) and math.isfinite(temperature) and temperature >= 0) else 0.0\nparams = SamplingParams(temperature=temperature)","typeGuard":"import math\ndef valid_temperature(t):\n    return isinstance(t,(int,float)) and math.isfinite(t) and t >= 0","tryCatchPattern":"try:\n    llm.generate(prompts, SamplingParams(temperature=t))\nexcept ValueError as e:\n    if 'temperature' in str(e):\n        llm.generate(prompts, SamplingParams(temperature=0.0))\n    else:\n        raise","preventionTips":["Guard divisions in dynamic temperature schedules to avoid NaN","Use temperature=0.0 for greedy decoding, never -1 or NaN as 'disabled'"],"tags":["sampling-params","temperature","nan","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"}