2noise/ChatTTS · error · ValueError

max_tokens must be at least 1, got {self.max_tokens}.

Error message

max_tokens must be at least 1, got {self.max_tokens}.

What it means

SamplingParams._verify_args enforces max_tokens >= 1. max_tokens is the maximum number of tokens to generate per completion; zero or negative lengths are rejected.

Source

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

            )
        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:
            raise ValueError(
                f"top_k must be -1 (disable), or at least 1, " f"got {self.top_k}."
            )
        if not 0.0 <= self.min_p <= 1.0:
            raise ValueError("min_p must be in [0, 1], got " f"{self.min_p}.")
        if self.max_tokens < 1:
            raise ValueError(f"max_tokens must be at least 1, got {self.max_tokens}.")
        if self.logprobs is not None and self.logprobs < 0:
            raise ValueError(f"logprobs must be non-negative, got {self.logprobs}.")
        if self.prompt_logprobs is not None and self.prompt_logprobs < 0:
            raise ValueError(
                f"prompt_logprobs must be non-negative, got " f"{self.prompt_logprobs}."
            )

    def _verify_beam_search(self) -> None:
        if self.best_of == 1:
            raise ValueError(
                "best_of must be greater than 1 when using beam "
                f"search. Got {self.best_of}."
            )
        if self.temperature > _SAMPLING_EPS:
            raise ValueError("temperature must be 0 when using beam search.")
        if self.top_p < 1.0 - _SAMPLING_EPS:
            raise ValueError("top_p must be 1 when using beam search.")
        if self.top_k != -1:

View on GitHub (pinned to 77b89ee281)

Solutions

  1. Set max_tokens to at least 1; size it as context_length - prompt_tokens, clamped to >= 1
  2. Reject prompts longer than the context window before computing max_tokens instead of letting it go to 0

Example fix

# before
max_tokens = context_len - len(prompt_ids)  # can be 0 or negative
params = SamplingParams(max_tokens=max_tokens)

# after
max_tokens = max(1, context_len - len(prompt_ids))
params = SamplingParams(max_tokens=max_tokens)
Defensive patterns

Strategy: validation

Validate before calling

def safe_max_tokens(context_len: int, prompt_ids: list) -> int:
    return max(1, context_len - len(prompt_ids))

Prevention

When it happens

Trigger: Constructing SamplingParams with max_tokens=0 or a negative value, e.g. computed as context_window - prompt_length when the prompt fills the whole window.

Common situations: Computing max_tokens from context-length arithmetic that can reach 0 for long prompts; passing max_tokens=0 meaning "no limit" (there is no unlimited setting — pick a real budget); config typos.

Related errors


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