huggingface/transformers · error · ValueError

`temperature` (={temperature}) has to be a strictly positive

Error message

`temperature` (={temperature}) has to be a strictly positive float, otherwise your next token scores will be invalid.

What it means

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.

Source

Thrown at src/transformers/generation/logits_process.py:296

    >>> generate_kwargs["temperature"] = 0.0001
    >>> outputs = model.generate(**inputs, **generate_kwargs)
    >>> print(tokenizer.batch_decode(outputs, skip_special_tokens=True))
    ['Hugging Face Company is a company that has been around for over 20 years',
    'Hugging Face Company is a company that has been around for over 20 years']
    ```
    """

    supports_continuous_batching = True

    def __init__(self, temperature: float):
        if not isinstance(temperature, float) or not (temperature > 0):
            except_msg = (
                f"`temperature` (={temperature}) has to be a strictly positive float, otherwise your next token "
                "scores will be invalid."
            )
            if isinstance(temperature, float) and temperature == 0.0:
                except_msg += " If you're looking for greedy decoding strategies, set `do_sample=False`."
            raise ValueError(except_msg)

        self.temperature = temperature

    @add_start_docstrings(LOGITS_PROCESSOR_INPUTS_DOCSTRING)
    def __call__(self, input_ids: torch.LongTensor, scores: torch.FloatTensor) -> torch.FloatTensor:
        scores_processed = scores / self.temperature
        return scores_processed


class RepetitionPenaltyLogitsProcessor(LogitsProcessor):
    r"""
    [`LogitsProcessor`] that prevents the repetition of previous tokens through a penalty. This penalty is applied at
    most once per token. Note that, for decoder-only models like most LLMs, the considered tokens include the prompt
    by default.

    In the original [paper](https://huggingface.co/papers/1909.05858), the authors suggest the use of a penalty of around
    1.2 to achieve a good balance between truthful generation and lack of repetition. To penalize and reduce
    repetition, use `penalty` values above 1.0, where a higher value penalizes more strongly. To reward and encourage

View on GitHub (pinned to a597f97485)

Solutions

  1. If you want deterministic/greedy output, remove the temperature argument and set do_sample=False in generate() instead of temperature=0
  2. Pass a strictly positive float literal: TemperatureLogitsProcessor(1.0) (note the .0 — plain int 1 is rejected)
  3. Coerce config-loaded values explicitly: TemperatureLogitsProcessor(float(cfg['temperature'])) after checking it is > 0
  4. If temperature comes from user input or a sweep, validate 0.0 < temperature before constructing the processor

Example fix

// before
proc = TemperatureLogitsProcessor(0)  # greedy intent -> ValueError
out = model.generate(**inputs, do_sample=True, temperature=0)

// after
out = model.generate(**inputs, do_sample=False)  # greedy decoding, no temperature
# or, when sampling:
proc = TemperatureLogitsProcessor(0.7)
Defensive patterns

Strategy: validation

Validate before calling

def valid_temperature(t):
    return isinstance(t, float) and t > 0.0

# greedy intent -> do not build the processor at all:
# generate(**inputs, do_sample=False)

Type guard

def is_valid_temperature(t) -> bool:
    return type(t) is float and t > 0.0

Try / catch

try:
    proc = TemperatureLogitsProcessor(float(t))
except ValueError as e:
    raise ValueError(f'Invalid sampling config: {e}') from e

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of huggingface/transformers@a597f97485 (2026-08-14). Data as JSON: /api/errors/e3c6f9b49c29631d. Report an issue: GitHub.