huggingface/transformers · error · ValueError

`min_length` has to be a non-negative integer, but is {min_l

Error message

`min_length` has to be a non-negative integer, but is {min_length}

What it means

MinLengthLogitsProcessor.__init__ validates min_length: it must be an int (not float/str/None) and >= 0. This mirrors generate(min_length=...); fractional or negative values, or values parsed as strings, are rejected.

Source

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

    >>> # setting `min_length` to a value smaller than the uncontrolled output length has no impact
    >>> gen_out = model.generate(**inputs, min_length=3)
    >>> print(tokenizer.batch_decode(gen_out, skip_special_tokens=True)[0])
    A number: one

    >>> # setting a larger `min_length` will force the model to generate beyond its natural ending point, which is not
    >>> # necessarily incorrect
    >>> gen_out = model.generate(**inputs, min_length=10)
    >>> print(tokenizer.batch_decode(gen_out, skip_special_tokens=True)[0])
    A number: one thousand, nine hundred and ninety-four
    ```
    """

    supports_continuous_batching: bool = False

    def __init__(self, min_length: int, eos_token_id: int | list[int] | torch.Tensor, device: str = "cpu"):
        if not isinstance(min_length, int) or min_length < 0:
            raise ValueError(f"`min_length` has to be a non-negative integer, but is {min_length}")

        if not isinstance(eos_token_id, torch.Tensor):
            if isinstance(eos_token_id, int):
                eos_token_id = [eos_token_id]
            eos_token_id = torch.tensor(eos_token_id, device=device)

        self.min_length = min_length
        self.eos_token_id = eos_token_id

    @add_start_docstrings(LOGITS_PROCESSOR_INPUTS_DOCSTRING)
    def __call__(self, input_ids: torch.LongTensor, scores: torch.FloatTensor) -> torch.FloatTensor:
        vocab_tensor = torch.arange(scores.shape[-1], device=scores.device)
        eos_token_mask = torch.isin(vocab_tensor, self.eos_token_id)
        scores_processed = scores.clone()
        if input_ids.shape[-1] < self.min_length:
            scores_processed = torch.where(eos_token_mask, -math.inf, scores)
        return scores_processed

View on GitHub (pinned to a597f97485)

Solutions

  1. Pass an int: int(min_length)
  2. Validate config before constructing: reject non-int or negative early
  3. Check for off-by-one intent: 'at least N total tokens' vs 'N new tokens' (min_new_tokens) confusion

Example fix

# before
proc = MinLengthLogitsProcessor(min_length=10.0, eos_token_id=eos)

# after
proc = MinLengthLogitsProcessor(min_length=int(10.0), eos_token_id=eos)
Defensive patterns

Strategy: validation

Validate before calling

assert isinstance(min_length, int) and not isinstance(min_length, bool) and min_length >= 0, \
    f'min_length must be a non-negative int, got {min_length!r}'

Type guard

def is_valid_min_length(v) -> bool:
    return isinstance(v, int) and not isinstance(v, bool) and v >= 0

Prevention

When it happens

Trigger: MinLengthLogitsProcessor(min_length=10.0) (float fails isinstance int), min_length=-1, or min_length='10' from CLI/JSON. Also GenerationConfig(min_length=0.5) flowing into processor construction.

Common situations: JSON/YAML configs where numbers parse as float or str; argparse without type=int; note that in Python isinstance(True, int) is True so booleans slip through — a separate latent quirk.

Related errors


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