{"record":{"id":"c5113b672c3431f8","repo":"huggingface/transformers","slug":"self-class-name-cannot-use-beam-search-w","errorCode":null,"errorMessage":"{self.__class__.__name__} cannot use beam search with a cache currently, as the cache cannot be reordered","messagePattern":"(.+?) cannot use beam search with a cache currently, as the cache cannot be reordered","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"src/transformers/generation/utils.py","lineNumber":3487,"sourceCode":"                decoder_prompt_len=decoder_prompt_len,\n                length_penalty=length_penalty,\n                early_stopping=early_stopping,\n            )\n\n            # g. Prepare remaining data for the next iteration, including computing the stopping condition for\n            # beam search as a whole (as opposed to individual beams, i.e. `stopping_criteria`)\n\n            # pluck the cache from the beam indices that will be used in the next iteration\n            # NOTE: we need to check if `self._reorder_cache` exists for special models like RAG, RecurrentGemma etc.\n            if any(cache_key in model_kwargs for cache_key in ALL_CACHE_NAMES):\n                cache_key = next(cache_key for cache_key in ALL_CACHE_NAMES if cache_key in model_kwargs)\n                beam_idx = self._flatten_beam_dim(running_beam_indices[..., cur_len - decoder_prompt_len])\n                if hasattr(self, \"_reorder_cache\"):\n                    model_kwargs[cache_key] = self._reorder_cache(model_kwargs[cache_key], beam_idx)\n                elif hasattr(model_kwargs[cache_key], \"reorder_cache\"):\n                    model_kwargs[cache_key].reorder_cache(beam_idx)\n                else:\n                    raise ValueError(\n                        f\"{self.__class__.__name__} cannot use beam search with a cache currently, as the cache cannot be reordered\"\n                    )\n\n            cur_len = cur_len + 1\n            is_early_stop_heuristic_unsatisfied = self._check_early_stop_heuristic(\n                is_early_stop_heuristic_unsatisfied=is_early_stop_heuristic_unsatisfied,\n                running_beam_scores=running_beam_scores,\n                beam_scores=beam_scores,\n                is_sent_finished=is_sent_finished,\n                cur_len=cur_len,\n                max_length=max_length,\n                decoder_prompt_len=decoder_prompt_len,\n                early_stopping=early_stopping,\n                length_penalty=length_penalty,\n            )\n            this_peer_finished = not self._beam_search_has_unfinished_sequences(\n                is_early_stop_heuristic_unsatisfied,\n                is_sent_finished,","sourceCodeStart":3469,"sourceCodeEnd":3505,"githubUrl":"https://github.com/huggingface/transformers/blob/a597f974857b3d92939971296bc0deb93d33d780/src/transformers/generation/utils.py#L3469-L3505","documentation":"Error \"{self.__class__.__name__} cannot use beam search with a cache currently, as the cache cannot be reordered\" thrown in huggingface/transformers.","triggerScenarios":"Raised in beam search generation when the model class uses a cache that cannot be reordered for beam hypotheses.","commonSituations":"Beam search (num_beams>1) on models with non-reorderable caches such as certain linear-attention or Mamba-style models.","solutions":["Use greedy or sampling decoding instead of beam search for this model.","Use a dynamic cache implementation that supports reordering."],"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"}