{"id":"d4e51ea90cb9fee2","repo":"redis/redis-py","slug":"error-setting-client-name","errorCode":null,"errorMessage":"Error setting client name","messagePattern":"Error setting client name","errorType":"exception","errorClass":"ConnectionError","httpStatus":null,"severity":"error","filePath":"redis/asyncio/connection.py","lineNumber":1025,"sourceCode":"            #     raise ConnectionError(\"Invalid RESP version\")\n\n        # Activate maintenance notifications for this connection\n        # if enabled in the configuration\n        # This is a no-op if maintenance notifications are not enabled\n        await self.activate_maint_notifications_handling_if_enabled(\n            check_health=check_health\n        )\n\n        # if a client_name is given, set it\n        if self.client_name:\n            await self.send_command(\n                \"CLIENT\",\n                \"SETNAME\",\n                self.client_name,\n                check_health=check_health,\n            )\n            if str_if_bytes(await self.read_response()) != \"OK\":\n                raise ConnectionError(\"Error setting client name\")\n\n        # Set the library name and version from driver_info, pipeline for lower startup latency\n        lib_name_sent = False\n        lib_version_sent = False\n\n        if self.driver_info and self.driver_info.formatted_name:\n            await self.send_command(\n                \"CLIENT\",\n                \"SETINFO\",\n                \"LIB-NAME\",\n                self.driver_info.formatted_name,\n                check_health=check_health,\n            )\n            lib_name_sent = True\n\n        if self.driver_info and self.driver_info.lib_version:\n            await self.send_command(\n                \"CLIENT\",","sourceCodeStart":1007,"sourceCodeEnd":1043,"githubUrl":"https://github.com/redis/redis-py/blob/da03cdc7e8731092b13e395605c3c1fb2de25de1/redis/asyncio/connection.py#L1007-L1043","documentation":"Raised as a ConnectionError during the connection handshake (on_connect) when the CLIENT SETNAME command is sent (because self.client_name is set) but the server returns a value that is not the string 'OK'. This means the server did not acknowledge the name assignment, usually because the name violates server naming rules (e.g. contains spaces/newlines) or the server is a proxy/compatibility layer that does not implement CLIENT SETNAME correctly.","triggerScenarios":"Constructing redis.asyncio.Redis(client_name='...') (or connection_pool with client_name) and opening a connection whose on_connect() issues 'CLIENT SETNAME <name>'; the read_response() for that command returns something other than b'OK'/'OK'. Only fires when self.client_name is truthy at line 1017.","commonSituations":"Client names containing spaces, newlines, or non-printable characters (Redis disallows these); connecting to a Redis-compatible proxy (KeyDB, Dragonfly, Valkey, Twemproxy, a load balancer) that rejects or mishandles CLIENT SETNAME; older server versions where the name was too long or already in use.","solutions":["Sanitize the client_name to contain only printable characters with no spaces or newlines.","Verify the target actually speaks the Redis protocol and supports CLIENT SETNAME (point a raw redis-cli at it and run 'CLIENT SETNAME test').","Temporarily drop the client_name argument to confirm it is the cause; re-add a clean value once the connection succeeds.","Upgrade or reconfigure the proxy/load balancer to pass CLIENT commands through unchanged."],"exampleFix":"// before\nr = redis.asyncio.Redis(host=h, port=p, client_name=\"my worker #3\")\n\n// after\nr = redis.asyncio.Redis(host=h, port=p, client_name=\"my-worker-3\")","handlingStrategy":"validation","validationCode":"import re\nNAME_RE = re.compile(r\"^[\\x21-\\x7e]+$\")  # printable, no spaces/newlines\ndef valid_client_name(name: str | None) -> bool:\n    return name is None or (NAME_RE.match(name) is not None and len(name) <= 200)","typeGuard":"def is_valid_client_name(name) -> bool:\n    return name is None or (isinstance(name, str) and len(name) <= 200 and \" \" not in name and \"\\n\" not in name)","tryCatchPattern":"from redis.exceptions import ConnectionError\ntry:\n    await r.ping()\nexcept ConnectionError as e:\n    if \"setting client name\" in str(e):\n        r = redis.asyncio.Redis(..., client_name=None)  # retry without name\n    else:\n        raise","preventionTips":["Keep client names short, printable, no whitespace.","Validate names against server naming rules before connecting.","Confirm the endpoint supports CLIENT SETNAME (not a stripped proxy)."],"tags":["connection","handshake","client-name","async"],"analyzedSha":"da03cdc7e8731092b13e395605c3c1fb2de25de1","analyzedAt":"2026-08-04T20:26:47.563Z","schemaVersion":2}