{"record":{"id":"5c194263cb8fd0b4","repo":"sgl-project/sglang","slug":"frequency-penalty-must-be-in-2-2-got-self-fr","errorCode":null,"errorMessage":"frequency_penalty must be in [-2, 2], got {self.frequency_penalty}.","messagePattern":"frequency_penalty must be in \\[-2, 2\\], got (.+?)\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"python/sglang/srt/sampling/sampling_params.py","lineNumber":170,"sourceCode":"            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), \"\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:","sourceCodeStart":152,"sourceCodeEnd":188,"githubUrl":"https://github.com/sgl-project/sglang/blob/0132848349585cfe6aae51c4941cbae872505f8a/python/sglang/srt/sampling/sampling_params.py#L152-L188","documentation":"SamplingParams.verify() requires frequency_penalty to lie in [-2, 2] (OpenAI-compatible range). Out-of-range values raise this ValueError during normalize()/verify(). frequency_penalty penalizes tokens proportionally to how often they have appeared.","triggerScenarios":"Passing SamplingParams(frequency_penalty=3.0), a large negative value, or a percent-scaled number; verify() runs via normalize() per request.","commonSituations":"Proxying OpenAI-API requests with unchecked penalty values; tuning scripts sweeping penalties beyond 2; confusing the 0-1 scale with the -2..2 scale.","solutions":["Clamp frequency_penalty to [-2, 2] before building params","Validate incoming API requests and reject/clamp penalty fields","Re-check tuned values from experiments that exceeded the range"],"exampleFix":"# before\nparams = SamplingParams(frequency_penalty=penalty)\n# after\nparams = SamplingParams(frequency_penalty=max(-2.0, min(2.0, penalty)))","handlingStrategy":"validation","validationCode":"frequency_penalty = max(-2.0, min(2.0, frequency_penalty))\nparams = SamplingParams(frequency_penalty=frequency_penalty)","typeGuard":"def valid_frequency_penalty(p):\n    return isinstance(p,(int,float)) and -2.0 <= p <= 2.0","tryCatchPattern":"try:\n    llm.generate(prompts, SamplingParams(frequency_penalty=p))\nexcept ValueError as e:\n    if 'frequency_penalty' in str(e):\n        p = 0.0\n    else:\n        raise","preventionTips":["Clamp OpenAI-style penalty fields to [-2, 2] at the API gateway","Use 0.0 to disable penalties"],"tags":["sampling-params","frequency-penalty","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"}