{"record":{"id":"dd3b191459432740","repo":"huggingface/transformers","slug":"top-p-has-to-be-a-float-0-and-1-but-is-top","errorCode":null,"errorMessage":"`top_p` has to be a float > 0 and < 1, but is {top_p}","messagePattern":"`top_p` has to be a float > 0 and < 1, but is (.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"src/transformers/generation/logits_process.py","lineNumber":518,"sourceCode":"    >>> 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 `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","sourceCodeStart":500,"sourceCodeEnd":536,"githubUrl":"https://github.com/huggingface/transformers/blob/a597f974857b3d92939971296bc0deb93d33d780/src/transformers/generation/logits_process.py#L500-L536","documentation":"Thrown by TopPLogitsWarper.__init__ when top_p is < 0 or > 1.0 after a float() coercion. Top-p (nucleus) sampling keeps the smallest set of tokens whose cumulative probability exceeds top_p, so the threshold must be a probability. Note the code coerces with float(top_p) first, so numeric strings and ints are accepted; the boundary values 0.0 and 1.0 also pass despite the message saying '> 0 and < 1' — 1.0 is effectively a no-op.","triggerScenarios":"TopPLogitsWarper(1.5); top_p=-0.1; model.generate(do_sample=True, top_p=1.2) via a typo'd generation config; hyperparameter sweeps stepping past 1.0.","commonSituations":"Generation-config JSON/YAML with top_p mistyped as 1.5; sweeps written as numpy floats > 1; confusing top_p with a percentage (passing 90 instead of 0.9).","solutions":["Clamp or correct the value to [0, 1]: top_p=0.9","If the value came as a percentage (e.g. 90), divide by 100 before use","Validate sweep/grid values: assert 0.0 <= top_p <= 1.0 before generate()"],"exampleFix":"# before\nout = model.generate(**inputs, do_sample=True, top_p=90)  # percent mistake -> ValueError\n\n# after\ntop_p = min(max(top_p_raw / 100.0, 0.0), 1.0) if top_p_raw > 1 else top_p_raw\nout = model.generate(**inputs, do_sample=True, top_p=top_p)","handlingStrategy":"validation","validationCode":"def valid_top_p(p):\n    try:\n        p = float(p)\n    except (TypeError, ValueError):\n        return False\n    return 0.0 <= p <= 1.0","typeGuard":"def is_valid_top_p(p) -> bool:\n    try:\n        return 0.0 <= float(p) <= 1.0\n    except (TypeError, ValueError):\n        return False","tryCatchPattern":"try:\n    proc = TopPLogitsWarper(float(top_p))\nexcept ValueError as e:\n    raise ValueError(f'top_p={top_p!r} must be in [0, 1]') from e","preventionTips":["Store top_p as a fraction (0.9), never a percentage (90)","Clamp sweep values to [0, 1] before generate()","Note 0.0 and 1.0 pass the check even though the message says otherwise"],"tags":["generation","top-p","nucleus-sampling","argument-validation"],"backgroundTag":null,"analyzedSha":"a597f974857b3d92939971296bc0deb93d33d780","analyzedAt":"2026-08-14T18:24:08.354Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}