2noise/ChatTTS · error · ValueError

presence_penalty must be in [-2, 2], got {self.presence_pena

Error message

presence_penalty must be in [-2, 2], got {self.presence_penalty}.

What it means

SamplingParams._verify_args enforces presence_penalty in the inclusive range [-2, 2], mirroring the OpenAI API. presence_penalty penalizes tokens based on whether they already appeared in the generated text.

Source

Thrown at ChatTTS/model/velocity/sampling_params.py:189

        else:
            self._verify_non_beam_search()
            # if self.temperature < _SAMPLING_EPS:
            #     # Zero temperature means greedy sampling.
            #     self.top_p = 1.0
            #     self.top_k = -1
            #     self.min_p = 0.0
            #     self._verify_greedy_sampling()

    def _verify_args(self) -> None:
        if self.n < 1:
            raise ValueError(f"n must be at least 1, got {self.n}.")
        if self.best_of < self.n:
            raise ValueError(
                f"best_of must be greater than or equal to n, "
                f"got n={self.n} and best_of={self.best_of}."
            )
        if not -2.0 <= self.presence_penalty <= 2.0:
            raise ValueError(
                "presence_penalty must be in [-2, 2], got " f"{self.presence_penalty}."
            )
        if not -2.0 <= self.frequency_penalty <= 2.0:
            raise ValueError(
                "frequency_penalty must be in [-2, 2], got "
                f"{self.frequency_penalty}."
            )
        if not 0.0 < self.repetition_penalty <= 2.0:
            raise ValueError(
                "repetition_penalty must be in (0, 2], got "
                f"{self.repetition_penalty}."
            )
        # if self.temperature < 0.0:
        #     raise ValueError(
        #         f"temperature must be non-negative, got {self.temperature}.")
        if not 0.0 < self.top_p <= 1.0:
            raise ValueError(f"top_p must be in (0, 1], got {self.top_p}.")
        if self.top_k < -1 or self.top_k == 0:

View on GitHub (pinned to 77b89ee281)

Solutions

  1. Clamp presence_penalty to [-2, 2] before constructing SamplingParams
  2. Validate user-supplied penalty values at your API boundary

Example fix

# before
params = SamplingParams(presence_penalty=2.5)

# after
params = SamplingParams(presence_penalty=min(max(penalty, -2.0), 2.0))
Defensive patterns

Strategy: validation

Validate before calling

def clamp_penalty(v: float) -> float:
    return min(max(float(v), -2.0), 2.0)

Type guard

def is_valid_penalty(v) -> bool:
    return isinstance(v, (int, float)) and -2.0 <= v <= 2.0

Prevention

When it happens

Trigger: Constructing SamplingParams with presence_penalty outside [-2.0, 2.0], e.g. 2.5, -3, or a large value passed through from user input.

Common situations: Exposing presence_penalty in a public API without bounds checking; parsing it from a request payload where a typo slips in; mixing up penalty scales from a different library.

Related errors


AI-assisted analysis of 2noise/ChatTTS@77b89ee281 (2026-08-26). Data as JSON: /api/errors/383b048cfa2da5d9. Report an issue: GitHub.