huggingface/transformers · error · ValueError

`prompt_ignore_length` has to be a positive integer, but is

Error message

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

What it means

Thrown by RepetitionPenaltyLogitsProcessor.__init__ when prompt_ignore_length is not None and is either not a Python int or is negative. prompt_ignore_length slices the prompt tokens out of the penalty computation (input_ids[:, prompt_ignore_length:]), so it must be a non-negative int (0 is allowed despite the message saying 'positive'). Note bool passes the isinstance int check since bool subclasses int.

Source

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

    ...     penalty=1.1,
    ...     prompt_ignore_length=inputs["input_ids"].shape[-1]
    ... )
    >>> penalized_ids = model.generate(**inputs, logits_processor=[rep_pen_processor])
    >>> print(tokenizer.batch_decode(penalized_ids, skip_special_tokens=True)[0])
    I'm not going to be able to do that. I'm going to have to go through a lot of things, and
    ```
    """

    supports_continuous_batching = False

    def __init__(self, penalty: float, prompt_ignore_length: int | None = None):
        if not isinstance(penalty, float) or not (penalty > 0):
            raise ValueError(f"`penalty` has to be a strictly positive float, but is {penalty}")

        if prompt_ignore_length is not None and (
            not isinstance(prompt_ignore_length, int) or prompt_ignore_length < 0
        ):
            raise ValueError(f"`prompt_ignore_length` has to be a positive integer, but is {prompt_ignore_length}")

        self.penalty = penalty
        self.prompt_ignore_length = prompt_ignore_length
        self.logits_indices = None
        self.cu_seq_lens_q = None

    @add_start_docstrings(LOGITS_PROCESSOR_INPUTS_DOCSTRING)
    def __call__(self, input_ids: torch.LongTensor, scores: torch.FloatTensor) -> torch.FloatTensor:
        if self.prompt_ignore_length:
            input_ids = input_ids[:, self.prompt_ignore_length :]

        if scores.dim() == 3:
            if self.logits_indices is not None and self.cu_seq_lens_q is not None:
                last_positions = self.logits_indices
                last_scores = scores[0, last_positions, :]

                # Prepare token mask
                token_mask = torch.zeros_like(last_scores, dtype=torch.bool)

View on GitHub (pinned to a597f97485)

Solutions

  1. Pass a plain non-negative int: prompt_ignore_length=10
  2. Coerce numpy scalars: prompt_ignore_length=int(offset)
  3. For fractional prompts, compute the integer count yourself: int(len(prompt_ids) * 0.5)

Example fix

# before
proc = RepetitionPenaltyLogitsProcessor(1.2, prompt_ignore_length=5.0)  # float -> ValueError

# after
proc = RepetitionPenaltyLogitsProcessor(1.2, prompt_ignore_length=5)
# numpy case:
proc = RepetitionPenaltyLogitsProcessor(1.2, prompt_ignore_length=int(prompt_len_np))
Defensive patterns

Strategy: validation

Validate before calling

def valid_prompt_ignore_length(n):
    return n is None or (isinstance(n, int) and n >= 0)

Type guard

def is_valid_ignore_length(n) -> bool:
    return n is None or (type(n) is int and n >= 0)

Try / catch

try:
    proc = RepetitionPenaltyLogitsProcessor(1.2, prompt_ignore_length=int(n))
except ValueError as e:
    raise ValueError(f'prompt_ignore_length={n!r} must be a non-negative int') from e

Prevention

When it happens

Trigger: Passing prompt_ignore_length=2.0 (float), a negative value, or a numpy integer (isinstance np.int64, int is False on most builds); prompt_ignore_length=-1 intending 'ignore everything'.

Common situations: Computing the ignore length from tensor shapes (e.g. inputs['input_ids'].shape[-1] returns a Python int and is fine, but derived arithmetic with numpy scalars yields np.int64); passing a fraction like 0.5 to ignore half the prompt.

Related errors


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