{"record":{"id":"e3c6f9b49c29631d","repo":"huggingface/transformers","slug":"temperature-temperature-has-to-be-a-strictl","errorCode":null,"errorMessage":"`temperature` (={temperature}) has to be a strictly positive float, otherwise your next token scores will be invalid.","messagePattern":"`temperature` \\(=(.+?)\\) has to be a strictly positive float, otherwise your next token scores will be invalid\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"src/transformers/generation/logits_process.py","lineNumber":296,"sourceCode":"    >>> generate_kwargs[\"temperature\"] = 0.0001\n    >>> outputs = model.generate(**inputs, **generate_kwargs)\n    >>> print(tokenizer.batch_decode(outputs, skip_special_tokens=True))\n    ['Hugging Face Company is a company that has been around for over 20 years',\n    'Hugging Face Company is a company that has been around for over 20 years']\n    ```\n    \"\"\"\n\n    supports_continuous_batching = True\n\n    def __init__(self, temperature: float):\n        if not isinstance(temperature, float) or not (temperature > 0):\n            except_msg = (\n                f\"`temperature` (={temperature}) has to be a strictly positive float, otherwise your next token \"\n                \"scores will be invalid.\"\n            )\n            if isinstance(temperature, float) and temperature == 0.0:\n                except_msg += \" If you're looking for greedy decoding strategies, set `do_sample=False`.\"\n            raise ValueError(except_msg)\n\n        self.temperature = temperature\n\n    @add_start_docstrings(LOGITS_PROCESSOR_INPUTS_DOCSTRING)\n    def __call__(self, input_ids: torch.LongTensor, scores: torch.FloatTensor) -> torch.FloatTensor:\n        scores_processed = scores / self.temperature\n        return scores_processed\n\n\nclass RepetitionPenaltyLogitsProcessor(LogitsProcessor):\n    r\"\"\"\n    [`LogitsProcessor`] that prevents the repetition of previous tokens through a penalty. This penalty is applied at\n    most once per token. Note that, for decoder-only models like most LLMs, the considered tokens include the prompt\n    by default.\n\n    In the original [paper](https://huggingface.co/papers/1909.05858), the authors suggest the use of a penalty of around\n    1.2 to achieve a good balance between truthful generation and lack of repetition. To penalize and reduce\n    repetition, use `penalty` values above 1.0, where a higher value penalizes more strongly. To reward and encourage","sourceCodeStart":278,"sourceCodeEnd":314,"githubUrl":"https://github.com/huggingface/transformers/blob/a597f974857b3d92939971296bc0deb93d33d780/src/transformers/generation/logits_process.py#L278-L314","documentation":"Thrown by TemperatureLogitsProcessor.__init__ when the temperature argument is not a Python float or is not strictly greater than zero. Temperature divides the logits (scores / temperature), so a zero or negative value would produce invalid next-token scores (division by zero or flipped sign). Note the strict isinstance(temperature, float) check: passing an int (e.g. 1) is rejected even though 1 > 0.","triggerScenarios":"Constructing TemperatureLogitsProcessor(0.0) or with a negative value; passing an int like TemperatureLogitsProcessor(1) (fails the isinstance float check); passing temperature=0 to model.generate(do_sample=True) which builds this processor internally.","commonSituations":"Setting temperature=0 expecting greedy decoding (the error message explicitly tells you to use do_sample=False instead); loading temperature from YAML/JSON config where it deserializes as int (e.g. 1 instead of 1.0); copying a config from a library that allows 0 to disable temperature scaling.","solutions":["If you want deterministic/greedy output, remove the temperature argument and set do_sample=False in generate() instead of temperature=0","Pass a strictly positive float literal: TemperatureLogitsProcessor(1.0) (note the .0 — plain int 1 is rejected)","Coerce config-loaded values explicitly: TemperatureLogitsProcessor(float(cfg['temperature'])) after checking it is > 0","If temperature comes from user input or a sweep, validate 0.0 < temperature before constructing the processor"],"exampleFix":"// before\nproc = TemperatureLogitsProcessor(0)  # greedy intent -> ValueError\nout = model.generate(**inputs, do_sample=True, temperature=0)\n\n// after\nout = model.generate(**inputs, do_sample=False)  # greedy decoding, no temperature\n# or, when sampling:\nproc = TemperatureLogitsProcessor(0.7)","handlingStrategy":"validation","validationCode":"def valid_temperature(t):\n    return isinstance(t, float) and t > 0.0\n\n# greedy intent -> do not build the processor at all:\n# generate(**inputs, do_sample=False)","typeGuard":"def is_valid_temperature(t) -> bool:\n    return type(t) is float and t > 0.0","tryCatchPattern":"try:\n    proc = TemperatureLogitsProcessor(float(t))\nexcept ValueError as e:\n    raise ValueError(f'Invalid sampling config: {e}') from e","preventionTips":["Always write temperatures as float literals (0.7, 1.0) — ints are rejected","Use do_sample=False for greedy decoding, never temperature=0","Cast config-loaded values with float() after range-checking"],"tags":["generation","sampling","temperature","argument-validation"],"backgroundTag":null,"analyzedSha":"a597f974857b3d92939971296bc0deb93d33d780","analyzedAt":"2026-08-14T18:24:08.354Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}