{"record":{"id":"e450685da5b04220","repo":"huggingface/transformers","slug":"min-length-has-to-be-a-non-negative-integer-but","errorCode":null,"errorMessage":"`min_length` has to be a non-negative integer, but is {min_length}","messagePattern":"`min_length` has to be a non-negative integer, but is (.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"src/transformers/generation/logits_process.py","lineNumber":144,"sourceCode":"\n    >>> # setting `min_length` to a value smaller than the uncontrolled output length has no impact\n    >>> gen_out = model.generate(**inputs, min_length=3)\n    >>> print(tokenizer.batch_decode(gen_out, skip_special_tokens=True)[0])\n    A number: one\n\n    >>> # setting a larger `min_length` will force the model to generate beyond its natural ending point, which is not\n    >>> # necessarily incorrect\n    >>> gen_out = model.generate(**inputs, min_length=10)\n    >>> print(tokenizer.batch_decode(gen_out, skip_special_tokens=True)[0])\n    A number: one thousand, nine hundred and ninety-four\n    ```\n    \"\"\"\n\n    supports_continuous_batching: bool = False\n\n    def __init__(self, min_length: int, eos_token_id: int | list[int] | torch.Tensor, device: str = \"cpu\"):\n        if not isinstance(min_length, int) or min_length < 0:\n            raise ValueError(f\"`min_length` has to be a non-negative integer, but is {min_length}\")\n\n        if not isinstance(eos_token_id, torch.Tensor):\n            if isinstance(eos_token_id, int):\n                eos_token_id = [eos_token_id]\n            eos_token_id = torch.tensor(eos_token_id, device=device)\n\n        self.min_length = min_length\n        self.eos_token_id = eos_token_id\n\n    @add_start_docstrings(LOGITS_PROCESSOR_INPUTS_DOCSTRING)\n    def __call__(self, input_ids: torch.LongTensor, scores: torch.FloatTensor) -> torch.FloatTensor:\n        vocab_tensor = torch.arange(scores.shape[-1], device=scores.device)\n        eos_token_mask = torch.isin(vocab_tensor, self.eos_token_id)\n        scores_processed = scores.clone()\n        if input_ids.shape[-1] < self.min_length:\n            scores_processed = torch.where(eos_token_mask, -math.inf, scores)\n        return scores_processed\n","sourceCodeStart":126,"sourceCodeEnd":162,"githubUrl":"https://github.com/huggingface/transformers/blob/a597f974857b3d92939971296bc0deb93d33d780/src/transformers/generation/logits_process.py#L126-L162","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Pass an int: int(min_length)","Validate config before constructing: reject non-int or negative early","Check for off-by-one intent: 'at least N total tokens' vs 'N new tokens' (min_new_tokens) confusion"],"exampleFix":"# before\nproc = MinLengthLogitsProcessor(min_length=10.0, eos_token_id=eos)\n\n# after\nproc = MinLengthLogitsProcessor(min_length=int(10.0), eos_token_id=eos)","handlingStrategy":"validation","validationCode":"assert isinstance(min_length, int) and not isinstance(min_length, bool) and min_length >= 0, \\\n    f'min_length must be a non-negative int, got {min_length!r}'","typeGuard":"def is_valid_min_length(v) -> bool:\n    return isinstance(v, int) and not isinstance(v, bool) and v >= 0","tryCatchPattern":null,"preventionTips":["Use type=int in argparse","int() config values after JSON load","Remember min_length counts total tokens; use min_new_tokens for new-token semantics"],"tags":["validation","logits-processor","types","generation-config"],"backgroundTag":null,"analyzedSha":"a597f974857b3d92939971296bc0deb93d33d780","analyzedAt":"2026-08-14T18:24:08.354Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}