huggingface/transformers · error · ValueError

`eos_token_id` has to be a list of positive integers, but is

Error message

`eos_token_id` has to be a list of positive integers, but is {eos_token_id}

What it means

Error "`eos_token_id` has to be a list of positive integers, but is {eos_token_id}" thrown in huggingface/transformers.

Source

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

    >>> # `forced_eos_token_id` ensures the generation ends with a EOS token
    >>> outputs = model.generate(**inputs, max_new_tokens=10, forced_eos_token_id=tokenizer.eos_token_id)
    >>> print(tokenizer.batch_decode(outputs)[0])
    A sequence: 1, 2, 3, 4, 5, 6, 7,<|endoftext|>
    ```
    """

    def __init__(self, max_length: int, eos_token_id: int | list[int] | torch.Tensor, device: str = "cpu"):
        self.max_length = max_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.eos_token_id = eos_token_id

        if torch.is_floating_point(eos_token_id) or (eos_token_id < 0).any():
            raise ValueError(f"`eos_token_id` has to be a list of positive integers, but is {eos_token_id}")

    @add_start_docstrings(LOGITS_PROCESSOR_INPUTS_DOCSTRING)
    def __call__(self, input_ids: torch.LongTensor, scores: torch.FloatTensor) -> torch.FloatTensor:
        cur_len = input_ids.shape[-1]
        scores_processed = scores
        if cur_len == self.max_length - 1:
            scores_processed = torch.full_like(scores, -math.inf)
            scores_processed[:, self.eos_token_id] = 0
        return scores_processed


class InfNanRemoveLogitsProcessor(LogitsProcessor):
    r"""
    [`LogitsProcessor`] that removes all `nan` and `inf` values to avoid the generation method to fail. Note that using
    the logits processor should only be used if necessary since it can slow down the generation method.

    This logits processor has no `generate` example, as there shouldn't be a correct combination of flags that warrants
    its use.

View on GitHub (pinned to a597f97485)

Solutions

  1. Pass `eos_token_id` as a list of positive integers, e.g. `[tokenizer.eos_token_id]`.
  2. Use `generation_config.eos_token_id` which is normalized to a list automatically.

When it happens

Trigger: Raised in ForcedEOSTokenLogitsProcessor.__init__ when eos_token_id is not a positive integer or list of positive integers.

Common situations: Passing eos_token_id=None, a negative id, or an unparsed string id when forcing EOS at max_length.


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