{"record":{"id":"082f2a3e1bda1035","repo":"huggingface/transformers","slug":"passing-both-cache-implementation-used-to-initi","errorCode":null,"errorMessage":"Passing both `cache_implementation` (used to initialize certain caches) and `{cache_name}` (a Cache object) is unsupported. Please use only one of the two.","messagePattern":"Passing both `cache_implementation` \\(used to initialize certain caches\\) and `(.+?)` \\(a Cache object\\) is unsupported\\. Please use only one of the two\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"src/transformers/generation/utils.py","lineNumber":1950,"sourceCode":"        model_kwargs: dict,\n        generation_mode: GenerationMode,\n        batch_size: int,\n        max_cache_length: int,\n    ) -> bool:\n        \"\"\"\n        Prepares the cache for generation (if applicable), given `generate`'s parameterization. If a cache is\n        instantiated, writes it to `model_kwargs`, under the name expected by the model.\n        \"\"\"\n\n        # TODO @raushan, unify cache arg naming for all models\n        is_linear_attn_cache = \"mamba\" in self.__class__.__name__.lower()\n        cache_name = \"past_key_values\" if not is_linear_attn_cache else \"cache_params\"\n\n        # Quick escape route 1: if the user specifies a cache, we only need to check for conflicting `generate` arguments\n        user_defined_cache = model_kwargs.get(cache_name)\n        if user_defined_cache is not None:\n            if generation_config.cache_implementation is not None:\n                raise ValueError(\n                    f\"Passing both `cache_implementation` (used to initialize certain caches) and `{cache_name}` (a \"\n                    \"Cache object) is unsupported. Please use only one of the two.\"\n                )\n            if isinstance(user_defined_cache, tuple):\n                raise ValueError(\n                    \"Passing a tuple of `past_key_values` is not supported anymore. Please use a `Cache` instance.\"\n                )\n            return\n\n        # Quick escape route 2: if the user specifies no cache is to be used. (conflicting arguments are handled in\n        # `generation_config.validate()`)\n        if generation_config.use_cache is False:\n            return\n\n        # Quick escape route 3: model that supply it in `prepare_inputs_for_generation` (mamba, zamba, ...)\n        if not self._supports_default_dynamic_cache():\n            if generation_config.cache_implementation is not None:\n                logger.warning_once(","sourceCodeStart":1932,"sourceCodeEnd":1968,"githubUrl":"https://github.com/huggingface/transformers/blob/a597f974857b3d92939971296bc0deb93d33d780/src/transformers/generation/utils.py#L1932-L1968","documentation":"A generation cache can come either from `generation_config.cache_implementation` (a string like 'dynamic'/'static'/'quantized' that makes generate BUILD a cache) or from a user-supplied `Cache` object in `model_kwargs` (`past_key_values`, or `cache_params` for mamba-style models). Supplying both is ambiguous, so when `model_kwargs[cache_name]` is set and `cache_implementation` is not None, generate raises.","triggerScenarios":"`model.generate(**inputs, past_key_values=DynamicCache(), cache_implementation=\"dynamic\")`, or `model.generate(..., past_key_values=cache)` while `model.generation_config.cache_implementation = \"static\"` was set earlier.","commonSituations":"Serving code that pre-creates caches for throughput combined with a generation_config.json or wrapper that sets `cache_implementation`; incremental upgrades adding `cache_implementation` globally while callers still pass caches; mamba-family models where the key is `cache_params`.","solutions":["Pick one mechanism: either pass the `Cache` object and clear `cache_implementation` (`model.generate(..., past_key_values=cache, cache_implementation=None)`),","or drop the Cache object and keep `cache_implementation` so generate constructs the cache.","Check `model.generation_config.cache_implementation` — it may be set from a saved generation_config.json even if you never set it in the call.","Note mamba/linear-attention models use the kwarg name `cache_params`, not `past_key_values`."],"exampleFix":"# before\ncache = DynamicCache()\nout = model.generate(**inputs, past_key_values=cache, cache_implementation=\"dynamic\")  # ValueError: both\n\n# after\nout = model.generate(**inputs, past_key_values=cache, cache_implementation=None)","handlingStrategy":"validation","validationCode":"cache_name = \"cache_params\" if \"mamba\" in type(model).__name__.lower() else \"past_key_values\"\nif kwargs.get(cache_name) is not None and (kwargs.get(\"cache_implementation\") or model.generation_config.cache_implementation):\n    kwargs[\"cache_implementation\"] = None  # or drop kwargs[cache_name]","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Choose ONE cache mechanism per call: object OR implementation string.","Check model.generation_config.cache_implementation when passing a Cache object — saved configs can set it.","Remember mamba-family models use cache_params as the kwarg name."],"tags":["generation","cache","past-key-values","cache-implementation","conflicting-arguments"],"backgroundTag":null,"analyzedSha":"a597f974857b3d92939971296bc0deb93d33d780","analyzedAt":"2026-08-14T18:24:08.354Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}