huggingface/transformers · error · ValueError

`bad_words_ids` has to be a list of lists, but is {bad_words

Error message

`bad_words_ids` has to be a list of lists, but is {bad_words_ids}.

What it means

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

Source

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

            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)

            eos_token_id_list = eos_token_id.tolist()  # convert to python list before
            bad_words_ids = list(
                filter(lambda bad_token_seq: all(bad_token_seq != [i] for i in eos_token_id_list), bad_words_ids)
            )
        # Forbidding a sequence is equivalent to setting its bias to -inf
        sequence_bias = {tuple(sequence): float("-inf") for sequence in bad_words_ids}
        super().__init__(sequence_bias=sequence_bias)

    def _validate_arguments(self):
        bad_words_ids = self.bad_word_ids
        if not isinstance(bad_words_ids, list) or len(bad_words_ids) == 0:
            raise ValueError(f"`bad_words_ids` has to be a non-empty list, but is {bad_words_ids}.")
        if any(not isinstance(bad_word_ids, list) for bad_word_ids in bad_words_ids):
            raise ValueError(f"`bad_words_ids` has to be a list of lists, but is {bad_words_ids}.")
        if any(
            any((not isinstance(token_id, (int, np.integer)) or token_id < 0) for token_id in bad_word_ids)
            for bad_word_ids in bad_words_ids
        ):
            raise ValueError(
                f"Each list in `bad_words_ids` has to be a list of positive integers, but is {bad_words_ids}."
            )


class PrefixConstrainedLogitsProcessor(LogitsProcessor):
    r"""
    [`LogitsProcessor`] that enforces constrained generation and is useful for prefix-conditioned constrained
    generation. See [Autoregressive Entity Retrieval](https://huggingface.co/papers/2010.00904) for more information.

    Args:
        prefix_allowed_tokens_fn (`Callable[[int, torch.Tensor], list[int]]`):
            This function constraints the beam search to allowed tokens only at each step. This function takes 2
            arguments `inputs_ids` and the batch ID `batch_id`. It has to return a list with the allowed tokens for the

View on GitHub (pinned to a597f97485)

Solutions

  1. Wrap each bad word's token ids in its own list: `bad_words_ids=[[ids...], [ids...]]`.
  2. Tokenize each bad word separately and collect the id lists.

When it happens

Trigger: Raised in NoBadWordsLogitsProcessor.__init__ when bad_words_ids is a flat list of ints rather than a list of token-id lists.

Common situations: Passing bad_words_ids=[101, 102] instead of bad_words_ids=[[101, 102]] to generate().


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