{"record":{"id":"76514235f45b8093","repo":"huggingface/transformers","slug":"prompt-ignore-length-has-to-be-a-positive-intege","errorCode":null,"errorMessage":"`prompt_ignore_length` has to be a positive integer, but is {prompt_ignore_length}","messagePattern":"`prompt_ignore_length` has to be a positive integer, but is (.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"src/transformers/generation/logits_process.py","lineNumber":365,"sourceCode":"    ...     penalty=1.1,\n    ...     prompt_ignore_length=inputs[\"input_ids\"].shape[-1]\n    ... )\n    >>> penalized_ids = model.generate(**inputs, logits_processor=[rep_pen_processor])\n    >>> print(tokenizer.batch_decode(penalized_ids, skip_special_tokens=True)[0])\n    I'm not going to be able to do that. I'm going to have to go through a lot of things, and\n    ```\n    \"\"\"\n\n    supports_continuous_batching = False\n\n    def __init__(self, penalty: float, prompt_ignore_length: int | None = None):\n        if not isinstance(penalty, float) or not (penalty > 0):\n            raise ValueError(f\"`penalty` has to be a strictly positive float, but is {penalty}\")\n\n        if prompt_ignore_length is not None and (\n            not isinstance(prompt_ignore_length, int) or prompt_ignore_length < 0\n        ):\n            raise ValueError(f\"`prompt_ignore_length` has to be a positive integer, but is {prompt_ignore_length}\")\n\n        self.penalty = penalty\n        self.prompt_ignore_length = prompt_ignore_length\n        self.logits_indices = None\n        self.cu_seq_lens_q = None\n\n    @add_start_docstrings(LOGITS_PROCESSOR_INPUTS_DOCSTRING)\n    def __call__(self, input_ids: torch.LongTensor, scores: torch.FloatTensor) -> torch.FloatTensor:\n        if self.prompt_ignore_length:\n            input_ids = input_ids[:, self.prompt_ignore_length :]\n\n        if scores.dim() == 3:\n            if self.logits_indices is not None and self.cu_seq_lens_q is not None:\n                last_positions = self.logits_indices\n                last_scores = scores[0, last_positions, :]\n\n                # Prepare token mask\n                token_mask = torch.zeros_like(last_scores, dtype=torch.bool)","sourceCodeStart":347,"sourceCodeEnd":383,"githubUrl":"https://github.com/huggingface/transformers/blob/a597f974857b3d92939971296bc0deb93d33d780/src/transformers/generation/logits_process.py#L347-L383","documentation":"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.","triggerScenarios":"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'.","commonSituations":"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.","solutions":["Pass a plain non-negative int: prompt_ignore_length=10","Coerce numpy scalars: prompt_ignore_length=int(offset)","For fractional prompts, compute the integer count yourself: int(len(prompt_ids) * 0.5)"],"exampleFix":"# before\nproc = RepetitionPenaltyLogitsProcessor(1.2, prompt_ignore_length=5.0)  # float -> ValueError\n\n# after\nproc = RepetitionPenaltyLogitsProcessor(1.2, prompt_ignore_length=5)\n# numpy case:\nproc = RepetitionPenaltyLogitsProcessor(1.2, prompt_ignore_length=int(prompt_len_np))","handlingStrategy":"validation","validationCode":"def valid_prompt_ignore_length(n):\n    return n is None or (isinstance(n, int) and n >= 0)","typeGuard":"def is_valid_ignore_length(n) -> bool:\n    return n is None or (type(n) is int and n >= 0)","tryCatchPattern":"try:\n    proc = RepetitionPenaltyLogitsProcessor(1.2, prompt_ignore_length=int(n))\nexcept ValueError as e:\n    raise ValueError(f'prompt_ignore_length={n!r} must be a non-negative int') from e","preventionTips":["0 is allowed; negatives and floats are not","Wrap shape-derived values with int() before passing","Remember bool passes the int check — avoid passing flags here"],"tags":["generation","repetition-penalty","argument-validation"],"backgroundTag":null,"analyzedSha":"a597f974857b3d92939971296bc0deb93d33d780","analyzedAt":"2026-08-14T18:24:08.354Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}