{"id":"4f2d6b101093029a","repo":"redis/redis-py","slug":"cannot-create-cache-key","errorCode":null,"errorMessage":"Cannot create cache key.","messagePattern":"Cannot create cache key\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"redis/connection.py","lineNumber":1764,"sourceCode":"        # next read_response does not try to cache their reply under a stale key.\n        self._current_command_cache_key = None\n        self._conn.send_packed_command(command)\n\n    def send_command(self, *args, **kwargs):\n        self._process_pending_invalidations()\n\n        with self._cache_lock:\n            # Command is write command or not allowed\n            # to be cached.\n            if not self._cache.is_cachable(\n                CacheKey(command=args[0], redis_keys=(), redis_args=())\n            ):\n                self._current_command_cache_key = None\n                self._conn.send_command(*args, **kwargs)\n                return\n\n        if kwargs.get(\"keys\") is None:\n            raise ValueError(\"Cannot create cache key.\")\n\n        # Creates cache key.\n        self._current_command_cache_key = CacheKey(\n            command=args[0], redis_keys=tuple(kwargs.get(\"keys\")), redis_args=args\n        )\n\n        with self._cache_lock:\n            # We have to trigger invalidation processing in case if\n            # it was cached by another connection to avoid\n            # queueing invalidations in stale connections.\n            if self._cache.get(self._current_command_cache_key):\n                entry = self._cache.get(self._current_command_cache_key)\n\n                with self._pool_lock:\n                    while entry.connection_ref.can_read():\n                        try:\n                            entry.connection_ref.read_response(\n                                push_request=True,","sourceCodeStart":1746,"sourceCodeEnd":1782,"githubUrl":"https://github.com/redis/redis-py/blob/da03cdc7e8731092b13e395605c3c1fb2de25de1/redis/connection.py#L1746-L1782","documentation":"Raised as a ValueError in CacheProxyConnection.send_command when a command is deemed cacheable but the caller did not supply the 'keys' kwarg needed to build a CacheKey. The cache layer needs the keys touched by a command to construct invalidation tracking, so a cacheable command without keys cannot proceed.","triggerScenarios":"Executing a read-only/cachable command through a CSC-enabled client where the cache's is_cachable returns True but the command was issued without keys=... in kwargs. Typically an internal/library-level call path that bypasses key passing, or a custom command invocation that omits keys.","commonSituations":"Custom use of the cache connection API without forwarding keys; an internal redis-py path that does not populate keys for a newly cachable command; misconfiguration of which commands are cachable.","solutions":["Pass the keys= kwarg when invoking cachable read commands through a CSC-enabled client.","If this surfaces from a standard redis-py command, report it as a bug with the command name.","Disable client-side caching for the workload if key tracking cannot be supplied."],"exampleFix":"// before\nr.execute_command('GET', 'mykey')  # missing keys for CSC\n// after\nr.execute_command('GET', 'mykey', keys=['mykey'])","handlingStrategy":"validation","validationCode":"# when invoking cachable commands through a CSC client, always pass keys=\nr.execute_command('GET', 'mykey', keys=['mykey'])","typeGuard":null,"tryCatchPattern":"try:\n    r.execute_command('GET', 'mykey')\nexcept ValueError as e:\n    if 'Cannot create cache key' in str(e):\n        r.execute_command('GET', 'mykey', keys=['mykey'])","preventionTips":["Always pass the keys= kwarg for cachable commands on CSC-enabled clients.","If the error comes from a built-in command, file a bug with the command name.","Disable CSC if key tracking cannot be provided."],"tags":["client-side-caching","cache-key","configuration"],"analyzedSha":"da03cdc7e8731092b13e395605c3c1fb2de25de1","analyzedAt":"2026-08-04T20:26:47.563Z","schemaVersion":2}