{"record":{"id":"3a5575da2fb15001","repo":"huggingface/transformers","slug":"expected-1-or-2-arguments-got-len-caches","errorCode":null,"errorMessage":"Expected 1 or 2 arguments, got {len(caches)}","messagePattern":"Expected 1 or 2 arguments, got (.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"src/transformers/cache_utils.py","lineNumber":1997,"sourceCode":"                    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=\"\n            f\"{self.cross_attention_cache})\"\n        )\n\n    def __len__(self):\n        \"\"\"","sourceCodeStart":1979,"sourceCodeEnd":2015,"githubUrl":"https://github.com/huggingface/transformers/blob/a597f974857b3d92939971296bc0deb93d33d780/src/transformers/cache_utils.py#L1979-L2015","documentation":"EncoderDecoderCache.__init__ accepts either exactly one argument (a DDP-style iterable of per-layer cache data tuples) or exactly two arguments (a self-attention Cache and a cross-attention Cache). This ValueError is raised for any other argument count: zero or three-or-more positional arguments.","triggerScenarios":"Calling EncoderDecoderCache() with no args; passing three caches, e.g. EncoderDecoderCache(a, b, c); unpacking a list incorrectly such as EncoderDecoderCache(*three_items); or passing the same cache twice plus a third config object.","commonSituations":"Building the cache in a loop where the number of collected caches varies; refactoring from a 2-tuple to a 3-tuple of caches; splatting a list whose length is not 1 or 2.","solutions":["Pass exactly two Cache instances: EncoderDecoderCache(self_attention_cache, cross_attention_cache)","Or pass exactly one iterable of per-layer tuples (each of length 4 or 6) for DDP-style init","If splatting a list, check its length is 1 or 2 before the call"],"exampleFix":"// before\ncaches = [self_cache, cross_cache, extra]\nenc_dec = EncoderDecoderCache(*caches)\n\n// after\nenc_dec = EncoderDecoderCache(caches[0], caches[1])","handlingStrategy":"validation","validationCode":"assert 1 <= len(caches) <= 2, f\"EncoderDecoderCache takes 1 or 2 args, got {len(caches)}\"","typeGuard":null,"tryCatchPattern":"try:\n    enc = EncoderDecoderCache(*caches)\nexcept ValueError as e:\n    if \"Expected 1 or 2 arguments\" in str(e):\n        raise ValueError(f\"Bad cache list length {len(caches)}; check how caches were collected\") from e\n    raise","preventionTips":["When splatting, validate len(caches) in {1, 2} before the call","Prefer explicit two-argument form EncoderDecoderCache(self_cache, cross_cache) in application code"],"tags":["cache","valueerror","encoder-decoder","arguments","transformers"],"backgroundTag":null,"analyzedSha":"a597f974857b3d92939971296bc0deb93d33d780","analyzedAt":"2026-08-14T18:24:08.354Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}