{"record":{"id":"a00271522ce8619c","repo":"huggingface/transformers","slug":"prefix-allowed-tokens-fn-returned-an-empty-list","errorCode":null,"errorMessage":"`prefix_allowed_tokens_fn` returned an empty list for batch ID {batch_id}.This means that the constraint is unsatisfiable. Please check your implementationof `prefix_allowed_tokens_fn` ","messagePattern":"`prefix_allowed_tokens_fn` returned an empty list for batch ID (.+?)\\.This means that the constraint is unsatisfiable\\. Please check your implementationof `prefix_allowed_tokens_fn` ","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"src/transformers/generation/logits_process.py","lineNumber":1545,"sourceCode":"    Alice and Bob Marley\n    ```\n    \"\"\"\n\n    def __init__(self, prefix_allowed_tokens_fn: Callable[[int, torch.Tensor], list[int]], num_beams: int):\n        self._prefix_allowed_tokens_fn = prefix_allowed_tokens_fn\n        self._num_beams = num_beams\n\n    @add_start_docstrings(LOGITS_PROCESSOR_INPUTS_DOCSTRING)\n    def __call__(self, input_ids: torch.LongTensor, scores: torch.FloatTensor) -> torch.FloatTensor:\n        mask = torch.full_like(scores, -math.inf)\n        batch_size = input_ids.shape[0] // self._num_beams\n\n        for batch_id in range(batch_size):\n            for beam_id in range(self._num_beams):\n                sent = input_ids[batch_id * self._num_beams + beam_id]\n                prefix_allowed_tokens = self._prefix_allowed_tokens_fn(batch_id, sent)\n                if len(prefix_allowed_tokens) == 0:\n                    raise ValueError(\n                        f\"`prefix_allowed_tokens_fn` returned an empty list for batch ID {batch_id}.\"\n                        f\"This means that the constraint is unsatisfiable. Please check your implementation\"\n                        f\"of `prefix_allowed_tokens_fn` \"\n                    )\n                mask[batch_id * self._num_beams + beam_id, prefix_allowed_tokens] = 0\n\n        scores_processed = scores + mask\n        return scores_processed\n\n\nclass ForcedBOSTokenLogitsProcessor(LogitsProcessor):\n    r\"\"\"\n    [`LogitsProcessor`] that enforces the specified token as the first generated token. Used with encoder-decoder\n    models.\n\n    Args:\n        bos_token_id (`int`):\n            The id of the token to force as the first generated token.","sourceCodeStart":1527,"sourceCodeEnd":1563,"githubUrl":"https://github.com/huggingface/transformers/blob/a597f974857b3d92939971296bc0deb93d33d780/src/transformers/generation/logits_process.py#L1527-L1563","documentation":"Error \"`prefix_allowed_tokens_fn` returned an empty list for batch ID {batch_id}.This means that the constraint is unsatisfiable. Please check your implementationof `prefix_allowed_tokens_fn` \" thrown in huggingface/transformers.","triggerScenarios":"Raised in PrefixConstrainedLogitsProcessor.__call__ when the user-supplied prefix_allowed_tokens_fn returns an empty list for a batch element.","commonSituations":"A custom prefix_allowed_tokens_fn whose constraint becomes unsatisfiable at some decoding step, e.g. a trie walk reaching a dead end.","solutions":["Fix `prefix_allowed_tokens_fn` so it always returns at least one allowed token id for every prefix.","Include `eos_token_id` in the allowed set once generation may finish."],"exampleFix":null,"handlingStrategy":null,"validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"a597f974857b3d92939971296bc0deb93d33d780","analyzedAt":"2026-08-14T18:24:08.354Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}