{"record":{"id":"ed9d2363ee40e230","repo":"huggingface/transformers","slug":"one-of-the-two-arguments-is-not-a-cache-type-cac","errorCode":null,"errorMessage":"One of the two arguments is not a Cache: {type(caches[0]) = }, {type(caches[1]) = }","messagePattern":"One of the two arguments is not a Cache: (.+?), (.+?)","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"src/transformers/cache_utils.py","lineNumber":1992,"sourceCode":"        # For dp and ddp support, if only one argument is passed, it should be an iterable of DynamicCache ddp data\n        if len(caches) == 1:\n            self_attention_cache_data, cross_attention_cache_data = [], []\n            for combined_cache_data in caches[0]:\n                if len(combined_cache_data) == 6:  # two tuple of style (self_attn_k, self_attn_v, self_attn_sliding)\n                    self_attention_cache_data.append(combined_cache_data[:3])\n                    cross_attention_cache_data.append(combined_cache_data[3:])\n                # To support old DDP-style init, we handle the case where the tuple has no sliding window tensor\n                elif len(combined_cache_data) == 4:  # two tuple of style (self_attn_k, self_attn_v)\n                    self_attention_cache_data.append(combined_cache_data[:2])\n                    cross_attention_cache_data.append(combined_cache_data[2:])\n                else:\n                    raise ValueError(f\"Expected {len(combined_cache_data) = } to be 4 or 6.\\n{combined_cache_data = }\")\n            self.self_attention_cache = DynamicCache(self_attention_cache_data)\n            self.cross_attention_cache = DynamicCache(cross_attention_cache_data)\n        # Otherwise, we should get two arguments, a self-attention cache and a cross-attention cache\n        elif len(caches) == 2:\n            if not isinstance(caches[0], Cache) or not isinstance(caches[1], Cache):\n                raise TypeError(f\"One of the two arguments is not a Cache: {type(caches[0]) = }, {type(caches[1]) = }\")\n            self.self_attention_cache = caches[0]\n            self.cross_attention_cache = caches[1]\n        # Error case\n        else:\n            raise ValueError(f\"Expected 1 or 2 arguments, got {len(caches)}\")\n\n        self.is_updated = {}\n        for layer_idx in range(len(self.cross_attention_cache)):\n            self.is_updated[layer_idx] = bool(self.cross_attention_cache.get_seq_length(layer_idx) > 0)\n\n    def __iter__(self):\n        \"\"\"Returns tuples of style (self_attn_k, self_attn_v, self_attn_sliding, cross_attn_k, cross_attn_v, cross_attn_sliding)\"\"\"\n        for self_attention_layer, cross_attention_layer in zip(self.self_attention_cache, self.cross_attention_cache):\n            yield self_attention_layer + cross_attention_layer\n\n    def __repr__(self) -> str:\n        return (\n            f\"{self.__class__.__name__}(self_attention_cache={self.self_attention_cache}, cross_attention_cache=\"","sourceCodeStart":1974,"sourceCodeEnd":2010,"githubUrl":"https://github.com/huggingface/transformers/blob/a597f974857b3d92939971296bc0deb93d33d780/src/transformers/cache_utils.py#L1974-L2010","documentation":"EncoderDecoderCache.__init__ accepts either one DDP-style iterable or exactly two Cache objects (self-attention cache and cross-attention cache). This TypeError is raised when two positional arguments are passed but at least one of them is not an instance of transformers.cache_utils.Cache (e.g. raw tuples of key/value tensors, a list, or None). The constructor checks isinstance(caches[0], Cache) and isinstance(caches[1], Cache) and refuses anything else.","triggerScenarios":"Calling EncoderDecoderCache(past_key_values_tuple, cross_attn_tuple) with plain tuples/lists instead of Cache instances; passing (DynamicCache(), None); passing a dict of key_values; or passing a single cache plus a second unrelated object. Any call with 2 positional args where either fails isinstance(x, Cache).","commonSituations":"Migrating older code that built past_key_values as tuple-of-tuples and now wraps them in EncoderDecoderCache; using a custom cache class that does not subclass Cache; accidentally passing torch.Tensor key/value stacks; mixing up argument order with a DynamicCache and a StaticCache in a pipeline (valid types) vs a tuple (invalid).","solutions":["Wrap raw tensors in DynamicCache first: EncoderDecoderCache(DynamicCache(self_kv), DynamicCache(cross_kv))","If you have a single DDP-style iterable of per-layer tuples of length 4 or 6, pass it as the only argument instead of two arguments","Ensure any custom cache class subclasses transformers.cache_utils.Cache","Check that neither argument is None or an already-unpacked tuple before constructing"],"exampleFix":"// before\npast = EncoderDecoderCache(self_attn_kvs, cross_attn_kvs)  # raw tuples -> TypeError\n\n// after\nfrom transformers import DynamicCache, EncoderDecoderCache\npast = EncoderDecoderCache(DynamicCache(self_attn_kvs), DynamicCache(cross_attn_kvs))","handlingStrategy":"type-guard","validationCode":"from transformers.cache_utils import Cache\ndef is_cache_pair_ok(a, b) -> bool:\n    return isinstance(a, Cache) and isinstance(b, Cache)","typeGuard":"from transformers.cache_utils import Cache\n\ndef ensure_caches(*caches) -> tuple[Cache, Cache]:\n    if len(caches) == 2 and all(isinstance(c, Cache) for c in caches):\n        return caches[0], caches[1]\n    raise TypeError(f\"Need two Cache instances, got {[type(c) for c in caches]}\")","tryCatchPattern":"try:\n    enc_cache = EncoderDecoderCache(self_cache, cross_cache)\nexcept TypeError as e:\n    if \"not a Cache\" in str(e):\n        self_cache, cross_cache = DynamicCache(self_kv), DynamicCache(cross_kv)\n        enc_cache = EncoderDecoderCache(self_cache, cross_cache)\n    else:\n        raise","preventionTips":["Always construct inner caches with DynamicCache/StaticCache constructors, never raw tensors","Assert isinstance(x, Cache) before composing EncoderDecoderCache","Keep a helper that builds EncoderDecoderCache from tensors so wrapping is never forgotten"],"tags":["cache","typeerror","encoder-decoder","constructor","transformers"],"backgroundTag":null,"analyzedSha":"a597f974857b3d92939971296bc0deb93d33d780","analyzedAt":"2026-08-14T18:24:08.354Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}