{"record":{"id":"7eb7f42c42d3cba6","repo":"huggingface/transformers","slug":"min-p-has-to-be-a-float-in-the-0-1-interval","errorCode":null,"errorMessage":"`min_p` has to be a float in the [0, 1] interval, but is {min_p}","messagePattern":"`min_p` has to be a float in the \\[0, 1\\] interval, but is (.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"src/transformers/generation/logits_process.py","lineNumber":753,"sourceCode":"\n    >>> # With sampling, the output is unexpected -- sometimes too unexpected.\n    >>> outputs = model.generate(**inputs, do_sample=True)\n    >>> print(tokenizer.batch_decode(outputs, skip_special_tokens=True)[0])\n    A sequence: 1, 2, 3 | < 4 (left-hand pointer) ;\n    <BLANKLINE>\n    <BLANKLINE>\n\n    >>> # With `min_p` sampling, the output gets restricted to high-probability tokens.\n    >>> # Pro tip: In practice, LLMs use `min_p` in the 0.01-0.2 range.\n    >>> outputs = model.generate(**inputs, do_sample=True, min_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    def __init__(self, min_p: float, filter_value: float = -float(\"Inf\"), min_tokens_to_keep: int = 1):\n        if not (0 <= min_p <= 1.0):\n            raise ValueError(f\"`min_p` has to be a float in the [0, 1] interval, but is {min_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.min_p = min_p\n        self.filter_value = filter_value\n        self.min_tokens_to_keep = min_tokens_to_keep\n\n    def __call__(self, input_ids: torch.LongTensor, scores: torch.FloatTensor) -> torch.FloatTensor:\n        # Convert logits to probabilities\n        probs = torch.softmax(scores, dim=-1)\n        # Get the probability of the top token for each sequence in the batch\n        top_probs = probs.amax(dim=-1, keepdim=True)\n        # Calculate the actual min_p threshold by scaling min_p with the top token's probability\n        scaled_min_p = self.min_p * top_probs\n        # Create a mask for tokens that have a probability less than the scaled min_p\n        tokens_to_remove = probs < scaled_min_p\n\n        # Keep at least min_tokens_to_keep tokens (clip k to vocab size if needed, avoids index out of range)","sourceCodeStart":735,"sourceCodeEnd":771,"githubUrl":"https://github.com/huggingface/transformers/blob/a597f974857b3d92939971296bc0deb93d33d780/src/transformers/generation/logits_process.py#L735-L771","documentation":"Thrown by MinPLogitsProcessor.__init__ when min_p is outside [0, 1]. Min-p sampling keeps only tokens whose probability is at least min_p times the top token's probability, so min_p is a ratio and both endpoints 0 and 1 are legal (0 disables, 1 keeps only the argmax).","triggerScenarios":"MinPLogitsProcessor(1.2); min_p=-0.05; model.generate(do_sample=True, min_p=15) from a mistyped config (practical range is 0.01–0.2).","commonSituations":"Min-p is a newer parameter; users port values from papers or other engines using different scales; percentage confusion (10 instead of 0.1); generation-config typos.","solutions":["Use a ratio in [0, 1]: min_p=0.1 (docs suggest 0.01–0.2 in practice)","Convert percentages: min_p = pct / 100.0, clamped to [0, 1]","Validate 0 <= min_p <= 1 in config-loading code before generate()"],"exampleFix":"# before\nout = model.generate(**inputs, do_sample=True, min_p=10)  # percent mistake -> ValueError\n\n# after\nout = model.generate(**inputs, do_sample=True, min_p=0.1)","handlingStrategy":"validation","validationCode":"def valid_min_p(p):\n    return isinstance(p, (int, float)) and 0.0 <= float(p) <= 1.0","typeGuard":"def is_valid_min_p(p) -> bool:\n    return isinstance(p, (int, float)) and 0.0 <= p <= 1.0","tryCatchPattern":"try:\n    proc = MinPLogitsProcessor(float(p))\nexcept ValueError as e:\n    raise ValueError(f'min_p={p!r} must be in [0, 1] (typical 0.01-0.2)') from e","preventionTips":["min_p is a ratio of the top token's probability — practical range 0.01-0.2","Never pass percentages; divide by 100 first","0 disables min-p filtering"],"tags":["generation","min-p","sampling","argument-validation"],"backgroundTag":null,"analyzedSha":"a597f974857b3d92939971296bc0deb93d33d780","analyzedAt":"2026-08-14T18:24:08.354Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}