redis/redis-py · error · ConnectionError

Error setting client name

Error message

Error setting client name

What it means

Raised in on_connect (connection.py:1199-1207) when client_name is set and the CLIENT SETNAME reply is not 'OK'. The library sets the client name right after connect/auth; if the server rejects SETNAME (name too long, contains invalid chars, or non-OK for any reason) the connection is aborted. This protects against silently running unnamed connections.

Solutions

  1. Shorten client_name and remove spaces/newlines (use hyphens or underscores).
  2. Confirm the server's client-name rules (CLIENT SETNAME docs) for your Redis version.
  3. Omit client_name if it is not required.
  4. Use ASCII alphanumeric names with separators.

Example fix

# before
r = redis.Redis(host=h, port=p, client_name='my service (prod) #1')
# after
r = redis.Redis(host=h, port=p, client_name='my-service-prod-1')
Defensive patterns

Strategy: validation

Validate before calling

import re
def valid_client_name(name: str) -> bool:
    # Redis CLIENT SETNAME: no spaces/newlines; reasonable length
    return bool(name) and len(name) <= 255 and re.fullmatch(r'[^\s\r\n]+', name)

name = client_name if valid_client_name(client_name) else None
r = redis.Redis(host=h, port=p, client_name=name)

Type guard

import re
def is_valid_client_name(name) -> bool:
    return isinstance(name, str) and len(name) <= 255 and re.fullmatch(r'[^\s\r\n]+', name)

Try / catch

from redis.exceptions import ConnectionError
import re
try:
    r = redis.Redis(host=h, port=p, client_name=name)
    r.ping()
except ConnectionError as e:
    if 'client name' in str(e):
        safe = re.sub(r'\s+', '-', name)[:64]
        r = redis.Redis(host=h, port=p, client_name=safe)
    else:
        raise

Prevention

When it happens

Trigger: Passing client_name with characters Redis disallows, or longer than the server's limit (CONFIG set max name length, historically 255 and must not contain spaces/newlines in older versions), when connecting.

Common situations: Embedding spaces or newlines in client_name; generating very long dynamic names (e.g. a full hostname + uuid); connecting to an old Redis with stricter name rules.

Related errors


AI-assisted analysis of redis/redis-py@6a6b581b48 (2026-08-10). Data as JSON: /api/errors/ea4d5972a3a378a6. Report an issue: GitHub.

Appendix: source

Thrown at redis/connection.py:1207

                and self.handshake_metadata.get("proto") != self.protocol
            ):
                raise ConnectionError("Invalid RESP version")

        # Activate maintenance notifications for this connection
        # if enabled in the configuration
        # This is a no-op if maintenance notifications are not enabled
        self.activate_maint_notifications_handling_if_enabled(check_health=check_health)

        # if a client_name is given, set it
        if self.client_name:
            self.send_command(
                "CLIENT",
                "SETNAME",
                self.client_name,
                check_health=check_health,
            )
            if str_if_bytes(self.read_response()) != "OK":
                raise ConnectionError("Error setting client name")

        # Set the library name and version from driver_info
        try:
            if self.driver_info and self.driver_info.formatted_name:
                self.send_command(
                    "CLIENT",
                    "SETINFO",
                    "LIB-NAME",
                    self.driver_info.formatted_name,
                    check_health=check_health,
                )
                self.read_response()
        except ResponseError:
            pass

        try:
            if self.driver_info and self.driver_info.lib_version:
                self.send_command(

View on GitHub (pinned to 6a6b581b48)