huggingface/transformers · error · ValueError

`{arg_name}` has to be a positive integer, but is {arg_value

Error message

`{arg_name}` has to be a positive integer, but is {arg_value}

What it means

MinNewTokensLengthLogitsProcessor.__init__ validates prompt_length_to_skip and min_new_tokens: each must be an int and >= 0 (the message says 'positive integer' but the check allows 0 — a minor wording inconsistency in the library). Non-int or negative values are rejected.

Source

Thrown at src/transformers/generation/logits_process.py:215

    A number: one thousand
    ```
    """

    supports_continuous_batching = False

    def __init__(
        self,
        prompt_length_to_skip: int,
        min_new_tokens: int,
        eos_token_id: int | list[int] | torch.Tensor,
        device: str = "cpu",
    ):
        for arg_name, arg_value in [
            ("prompt_length_to_skip", prompt_length_to_skip),
            ("min_new_tokens", min_new_tokens),
        ]:
            if not isinstance(arg_value, int) or arg_value < 0:
                raise ValueError(f"`{arg_name}` has to be a positive integer, but is {arg_value}")

        if not isinstance(eos_token_id, torch.Tensor):
            if isinstance(eos_token_id, int):
                eos_token_id = [eos_token_id]
            eos_token_id = torch.tensor(eos_token_id, device=device)

        self.prompt_length_to_skip = prompt_length_to_skip
        self.min_new_tokens = min_new_tokens
        self.eos_token_id = eos_token_id

    @add_start_docstrings(LOGITS_PROCESSOR_INPUTS_DOCSTRING)
    def __call__(self, input_ids: torch.LongTensor, scores: torch.FloatTensor) -> torch.FloatTensor:
        new_tokens_length = input_ids.shape[-1] - self.prompt_length_to_skip
        scores_processed = scores.clone()
        vocab_tensor = torch.arange(scores.shape[-1], device=scores.device)
        eos_token_mask = torch.isin(vocab_tensor, self.eos_token_id)
        if new_tokens_length < self.min_new_tokens:
            scores_processed = torch.where(eos_token_mask, -math.inf, scores)

View on GitHub (pinned to a597f97485)

Solutions

  1. Coerce to int: int(min_new_tokens), int(prompt_length_to_skip)
  2. If prompt_length_to_skip is computed, assert it is >= 0 before constructing the processor
  3. Report/ignore the wording: 0 passes; only negative or non-int values fail

Example fix

# before
proc = MinNewTokensLengthLogitsProcessor(prompt_length_to_skip=len(ids)-1, min_new_tokens=5.0, eos_token_id=eos)

# after
proc = MinNewTokensLengthLogitsProcessor(prompt_length_to_skip=max(0, len(ids)), min_new_tokens=5, eos_token_id=eos)
Defensive patterns

Strategy: validation

Validate before calling

for name, v in [('prompt_length_to_skip', prompt_len), ('min_new_tokens', min_new_tokens)]:
    assert isinstance(v, int) and not isinstance(v, bool) and v >= 0, f'{name} must be a non-negative int, got {v!r}'

Type guard

def is_valid_nonneg_int(v) -> bool:
    return isinstance(v, int) and not isinstance(v, bool) and v >= 0

Prevention

When it happens

Trigger: Passing min_new_tokens=5.0 (float), prompt_length_to_skip=-1, or string values from config files. prompt_length_to_skip is normally len(prompt_ids) computed by callers; a computed length of -1 from a malformed input_ids slice triggers it.

Common situations: argparse/JSON configs without type=int; slicing bugs producing negative lengths; note the message/error mismatch: 0 is actually accepted, so users hunting a 'positive' bug may be misled.

Related errors


AI-assisted analysis of huggingface/transformers@a597f97485 (2026-08-14). Data as JSON: /api/errors/bb47ea0fc6890110. Report an issue: GitHub.