{"record":{"id":"57fc011e44c8c700","repo":"huggingface/transformers","slug":"min-tokens-to-keep-has-to-be-a-positive-integer","errorCode":null,"errorMessage":"`min_tokens_to_keep` has to be a positive integer, but is {min_tokens_to_keep}","messagePattern":"`min_tokens_to_keep` has to be a positive integer, but is (.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"src/transformers/generation/logits_process.py","lineNumber":520,"sourceCode":"    <BLANKLINE>\n    <BLANKLINE>\n\n    >>> # With `top_p` sampling, the output gets restricted to high-probability tokens.\n    >>> # Pro tip: In practice, LLMs use `top_p` in the 0.9-0.95 range.\n    >>> outputs = model.generate(**inputs, do_sample=True, top_p=0.1)\n    >>> print(tokenizer.batch_decode(outputs, skip_special_tokens=True)[0])\n    A sequence: 1, 2, 3, 4, 5, 6, 7, 8, 9\n    ```\n    \"\"\"\n\n    supports_continuous_batching = True\n\n    def __init__(self, top_p: float, filter_value: float = -float(\"Inf\"), min_tokens_to_keep: int = 1):\n        top_p = float(top_p)\n        if top_p < 0 or top_p > 1.0:\n            raise ValueError(f\"`top_p` has to be a float > 0 and < 1, but is {top_p}\")\n        if not isinstance(min_tokens_to_keep, int) or (min_tokens_to_keep < 1):\n            raise ValueError(f\"`min_tokens_to_keep` has to be a positive integer, but is {min_tokens_to_keep}\")\n\n        self.top_p = top_p\n        self.filter_value = filter_value\n        self.min_tokens_to_keep = min_tokens_to_keep\n\n    @add_start_docstrings(LOGITS_PROCESSOR_INPUTS_DOCSTRING)\n    def __call__(self, input_ids: torch.LongTensor, scores: torch.FloatTensor) -> torch.FloatTensor:\n        sorted_logits, sorted_indices = torch.sort(scores, descending=False)\n        cumulative_probs = sorted_logits.softmax(dim=-1).cumsum(dim=-1)\n\n        # Remove tokens with cumulative top_p above the threshold (token with 0 are kept)\n        sorted_indices_to_remove = cumulative_probs <= (1 - self.top_p)\n        # Keep at least min_tokens_to_keep\n        sorted_indices_to_remove[..., -self.min_tokens_to_keep :] = 0\n\n        # scatter sorted tensors to original indexing\n        indices_to_remove = sorted_indices_to_remove.scatter(1, sorted_indices, sorted_indices_to_remove)\n        scores_processed = scores.masked_fill(indices_to_remove, self.filter_value)","sourceCodeStart":502,"sourceCodeEnd":538,"githubUrl":"https://github.com/huggingface/transformers/blob/a597f974857b3d92939971296bc0deb93d33d780/src/transformers/generation/logits_process.py#L502-L538","documentation":"Thrown by TopPLogitsWarper.__init__ when min_tokens_to_keep is not a Python int or is < 1. This parameter guarantees at least that many tokens survive nucleus filtering, which requires a positive integer count. bool values pass isinstance (bool subclasses int) but True==1 is a legal value anyway.","triggerScenarios":"TopPLogitsWarper(0.9, min_tokens_to_keep=0); passing a float like 2.0; passing a numpy integer.","commonSituations":"Exposing min_tokens_to_keep in a user-facing API without validation; config values deserialized as strings or floats; deriving the value from a ratio instead of a count.","solutions":["Pass a positive int: min_tokens_to_keep=1 (the default) or higher","Coerce and floor computed values: max(1, int(round(ratio * vocab_size)))","Sanitize external input before building the warper"],"exampleFix":"# before\nproc = TopPLogitsWarper(0.9, min_tokens_to_keep=0)  # ValueError\n\n# after\nproc = TopPLogitsWarper(0.9, min_tokens_to_keep=1)","handlingStrategy":"validation","validationCode":"def valid_min_tokens(n):\n    return isinstance(n, int) and n >= 1","typeGuard":"def is_valid_min_tokens(n) -> bool:\n    return type(n) is int and n >= 1","tryCatchPattern":"try:\n    proc = TopPLogitsWarper(0.9, min_tokens_to_keep=int(max(1, n)))\nexcept ValueError as e:\n    raise ValueError(f'min_tokens_to_keep={n!r} must be >= 1') from e","preventionTips":["Treat min_tokens_to_keep as a count, not a ratio","Coerce with max(1, int(x)) when derived from user input","Validate shared config objects once before building processors"],"tags":["generation","top-p","argument-validation"],"backgroundTag":null,"analyzedSha":"a597f974857b3d92939971296bc0deb93d33d780","analyzedAt":"2026-08-14T18:24:08.354Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}