{"record":{"id":"02a7385bfbf201d9","repo":"huggingface/transformers","slug":"min-tokens-to-keep-has-to-be-a-strictly-positive","errorCode":null,"errorMessage":"`min_tokens_to_keep` has to be a strictly positive integer, but is {min_tokens_to_keep}","messagePattern":"`min_tokens_to_keep` has to be a strictly positive integer, but is (.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"src/transformers/generation/logits_process.py","lineNumber":915,"sourceCode":"    <BLANKLINE>\n\n    >>> # With epsilon sampling, the output gets restricted to high-probability tokens. Note that this is similar to\n    >>> # Top P sampling, which restricts tokens based on their cumulative probability.\n    >>> # Pro tip: The paper recommends using `epsilon_cutoff` values between 3e-4 and 9e-4\n    >>> outputs = model.generate(**inputs, do_sample=True, epsilon_cutoff=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    def __init__(self, epsilon: float, filter_value: float = -float(\"Inf\"), min_tokens_to_keep: int = 1):\n        epsilon = float(epsilon)\n        if epsilon <= 0 or epsilon >= 1:\n            raise ValueError(f\"`epsilon_cutoff` has to be a float > 0 and < 1, but is {epsilon}\")\n\n        min_tokens_to_keep = int(min_tokens_to_keep)\n        if min_tokens_to_keep < 1:\n            raise ValueError(\n                f\"`min_tokens_to_keep` has to be a strictly positive integer, but is {min_tokens_to_keep}\"\n            )\n\n        self.epsilon = epsilon\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        # Determine which indices to remove\n        probabilities = scores.softmax(dim=-1)\n        indices_to_remove = probabilities < self.epsilon\n\n        # Keep the words with the 'min_tokens_to_keep'-highest probabilities\n        top_k = min(self.min_tokens_to_keep, scores.size(-1))  # Safety check\n        indices_to_remove = indices_to_remove & (scores < torch.topk(scores, top_k)[0][..., -1, None])\n\n        scores_processed = scores.masked_fill(indices_to_remove, self.filter_value)","sourceCodeStart":897,"sourceCodeEnd":933,"githubUrl":"https://github.com/huggingface/transformers/blob/a597f974857b3d92939971296bc0deb93d33d780/src/transformers/generation/logits_process.py#L897-L933","documentation":"Thrown by EpsilonLogitsWarper.__init__ when min_tokens_to_keep is < 1 after an int() coercion. Unlike sibling processors, this one coerces first (min_tokens_to_keep = int(...)), so a float like 2.5 silently becomes 2 — only values that truncate to 0 or less (0, 0.5, -1) raise. The message says 'strictly positive integer'.","triggerScenarios":"EpsilonLogitsWarper(3e-4, min_tokens_to_keep=0); min_tokens_to_keep=0.5; negative values.","commonSituations":"Config fields typed as floats where 0 means 'auto'; note also that fractional values are silently truncated rather than rejected, which can mask bugs.","solutions":["Pass a positive int: min_tokens_to_keep=1 (default)","Round deliberately before passing: max(1, round(x)) to avoid silent truncation surprises","Validate external input: reject anything < 1"],"exampleFix":"# before\nproc = EpsilonLogitsWarper(9e-4, min_tokens_to_keep=0)  # ValueError\n\n# after\nproc = EpsilonLogitsWarper(9e-4, min_tokens_to_keep=1)","handlingStrategy":"validation","validationCode":"def valid_min_tokens(n):\n    return float(n) >= 1  # note: constructor silently truncates via int()","typeGuard":"def is_valid_min_tokens(n) -> bool:\n    try:\n        return int(n) >= 1\n    except (TypeError, ValueError):\n        return False","tryCatchPattern":"try:\n    proc = EpsilonLogitsWarper(9e-4, min_tokens_to_keep=max(1, int(round(n))))\nexcept ValueError as e:\n    raise ValueError(f'min_tokens_to_keep={n!r} must be >= 1') from e","preventionTips":["Fractional values are silently truncated — round deliberately","Reject 0 and negatives in your config schema"],"tags":["generation","epsilon-sampling","argument-validation"],"backgroundTag":null,"analyzedSha":"a597f974857b3d92939971296bc0deb93d33d780","analyzedAt":"2026-08-14T18:24:08.354Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}