redis/redis-py · error · ValueError

Cannot create cache key.

Error message

Cannot create cache key.

What it means

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.

Source

Thrown at redis/connection.py:1764

        # next read_response does not try to cache their reply under a stale key.
        self._current_command_cache_key = None
        self._conn.send_packed_command(command)

    def send_command(self, *args, **kwargs):
        self._process_pending_invalidations()

        with self._cache_lock:
            # Command is write command or not allowed
            # to be cached.
            if not self._cache.is_cachable(
                CacheKey(command=args[0], redis_keys=(), redis_args=())
            ):
                self._current_command_cache_key = None
                self._conn.send_command(*args, **kwargs)
                return

        if kwargs.get("keys") is None:
            raise ValueError("Cannot create cache key.")

        # Creates cache key.
        self._current_command_cache_key = CacheKey(
            command=args[0], redis_keys=tuple(kwargs.get("keys")), redis_args=args
        )

        with self._cache_lock:
            # We have to trigger invalidation processing in case if
            # it was cached by another connection to avoid
            # queueing invalidations in stale connections.
            if self._cache.get(self._current_command_cache_key):
                entry = self._cache.get(self._current_command_cache_key)

                with self._pool_lock:
                    while entry.connection_ref.can_read():
                        try:
                            entry.connection_ref.read_response(
                                push_request=True,

View on GitHub (pinned to da03cdc7e8)

Solutions

  1. Pass the keys= kwarg when invoking cachable read commands through a CSC-enabled client.
  2. If this surfaces from a standard redis-py command, report it as a bug with the command name.
  3. Disable client-side caching for the workload if key tracking cannot be supplied.

Example fix

// before
r.execute_command('GET', 'mykey')  # missing keys for CSC
// after
r.execute_command('GET', 'mykey', keys=['mykey'])
Defensive patterns

Strategy: validation

Validate before calling

# when invoking cachable commands through a CSC client, always pass keys=
r.execute_command('GET', 'mykey', keys=['mykey'])

Try / catch

try:
    r.execute_command('GET', 'mykey')
except ValueError as e:
    if 'Cannot create cache key' in str(e):
        r.execute_command('GET', 'mykey', keys=['mykey'])

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of redis/redis-py@da03cdc7e8 (2026-08-04). Data as JSON: /data/errors/4f2d6b101093029a.json. Report an issue: GitHub.