{"record":{"id":"bfdbf1ce7ac911f0","repo":"huggingface/transformers","slug":"top-k-has-to-be-a-strictly-positive-integer-but","errorCode":null,"errorMessage":"`top_k` has to be a strictly positive integer, but is {top_k}","messagePattern":"`top_k` 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":583,"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: A, B, C, D, E — S — O, P — R\n\n    >>> # With `top_k` sampling, the output gets restricted the k most likely tokens.\n    >>> # Pro tip: In practice, LLMs use `top_k` in the 5-50 range.\n    >>> outputs = model.generate(**inputs, do_sample=True, top_k=2)\n    >>> print(tokenizer.batch_decode(outputs, skip_special_tokens=True)[0])\n    A sequence: A, B, C, D, E, F, G, H, I\n    ```\n    \"\"\"\n\n    supports_continuous_batching = True\n\n    def __init__(self, top_k: int, filter_value: float = -float(\"Inf\"), min_tokens_to_keep: int = 1):\n        if not isinstance(top_k, int) or top_k <= 0:\n            raise ValueError(f\"`top_k` has to be a strictly positive integer, but is {top_k}\")\n\n        self.top_k = max(top_k, min_tokens_to_keep)\n        self.filter_value = filter_value\n        self.min_tokens_to_keep = min_tokens_to_keep  # used for CB processor initialization\n\n    @add_start_docstrings(LOGITS_PROCESSOR_INPUTS_DOCSTRING)\n    def __call__(self, input_ids: torch.LongTensor, scores: torch.FloatTensor) -> torch.FloatTensor:\n        top_k = min(self.top_k, scores.size(-1))  # Safety check\n        # Remove all tokens with a probability less than the last token of the top-k\n        indices_to_remove = scores < torch.topk(scores, top_k)[0][..., -1, None]\n        scores_processed = scores.masked_fill(indices_to_remove, self.filter_value)\n        return scores_processed\n\n\nclass TopHLogitsWarper(LogitsProcessor):\n    \"\"\"\n    [`LogitsProcessor`] that implements Top-H sampling, a decoding method which adaptively selects a subset of\n    high-probability tokens based on entropy and cumulative probability constraints.","sourceCodeStart":565,"sourceCodeEnd":601,"githubUrl":"https://github.com/huggingface/transformers/blob/a597f974857b3d92939971296bc0deb93d33d780/src/transformers/generation/logits_process.py#L565-L601","documentation":"Thrown by TopKLogitsWarper.__init__ when top_k is not a Python int or is <= 0. Top-k sampling keeps only the k highest logits, so k must be a positive integer count. Note self.top_k = max(top_k, min_tokens_to_keep) is stored, and the __call__ clamps against vocab size at runtime, so only the constructor check can fail.","triggerScenarios":"TopKLogitsWarper(0); top_k=-5; top_k=50.0 (float); model.generate(do_sample=True, top_k=0) intending 'disabled' — this library requires omitting top_k or setting it to the vocab size instead.","commonSituations":"Configs where top_k: 0 conventionally means 'off' (as in some other inference stacks) — here it raises; passing top_k as float from a config; numpy ints from sweep grids.","solutions":["To disable top-k filtering, remove top_k from the generate call / generation config entirely","Otherwise pass a positive int: top_k=50","Coerce external values: int(top_k) after checking top_k >= 1"],"exampleFix":"# before\nout = model.generate(**inputs, do_sample=True, top_k=0)  # 'disable' convention -> ValueError\n\n# after\nout = model.generate(**inputs, do_sample=True)  # top_k omitted = disabled\n# or a large k to approximate disabled:\nout = model.generate(**inputs, do_sample=True, top_k=model.config.vocab_size)","handlingStrategy":"validation","validationCode":"def valid_top_k(k):\n    return isinstance(k, int) and k > 0","typeGuard":"def is_valid_top_k(k) -> bool:\n    return type(k) is int and k > 0","tryCatchPattern":"try:\n    proc = TopKLogitsWarper(int(k))\nexcept ValueError as e:\n    raise ValueError(f'top_k={k!r} must be a positive int; omit it to disable') from e","preventionTips":["top_k=0 does not mean 'disabled' here — omit the parameter instead","Pass ints, not floats or numpy scalars","Common LLM range is 5–50"],"tags":["generation","top-k","sampling","argument-validation"],"backgroundTag":null,"analyzedSha":"a597f974857b3d92939971296bc0deb93d33d780","analyzedAt":"2026-08-14T18:24:08.354Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}