{"id":"ea4d5972a3a378a6","repo":"redis/redis-py","slug":"error-setting-client-name-ea4d59","errorCode":null,"errorMessage":"Error setting client name","messagePattern":"Error setting client name","errorType":"exception","errorClass":"ConnectionError","httpStatus":null,"severity":"error","filePath":"redis/connection.py","lineNumber":1207,"sourceCode":"                and self.handshake_metadata.get(\"proto\") != self.protocol\n            ):\n                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        self.activate_maint_notifications_handling_if_enabled(check_health=check_health)\n\n        # if a client_name is given, set it\n        if self.client_name:\n            self.send_command(\n                \"CLIENT\",\n                \"SETNAME\",\n                self.client_name,\n                check_health=check_health,\n            )\n            if str_if_bytes(self.read_response()) != \"OK\":\n                raise ConnectionError(\"Error setting client name\")\n\n        # Set the library name and version from driver_info\n        try:\n            if self.driver_info and self.driver_info.formatted_name:\n                self.send_command(\n                    \"CLIENT\",\n                    \"SETINFO\",\n                    \"LIB-NAME\",\n                    self.driver_info.formatted_name,\n                    check_health=check_health,\n                )\n                self.read_response()\n        except ResponseError:\n            pass\n\n        try:\n            if self.driver_info and self.driver_info.lib_version:\n                self.send_command(","sourceCodeStart":1189,"sourceCodeEnd":1225,"githubUrl":"https://github.com/redis/redis-py/blob/da03cdc7e8731092b13e395605c3c1fb2de25de1/redis/connection.py#L1189-L1225","documentation":"Raised as a ConnectionError when CLIENT SETNAME is sent during connect (because client_name was configured) but the server's response is not 'OK'. This indicates the server rejected the name, typically because it is invalid (too long, contains spaces) or the server does not support the command.","triggerScenarios":"Constructing redis.Redis(client_name='some name') where the name exceeds the server limit or contains disallowed characters; connecting to an ancient Redis that lacks CLIENT SETNAME. The check fires in on_connect after setting the name.","commonSituations":"Long/invalid client_name strings; names containing spaces or newlines; server version too old; dynamic name generation that produces illegal characters.","solutions":["Use a short, alphanumeric client_name with no spaces.","Upgrade the Redis server to one supporting CLIENT SETNAME.","Omit client_name if naming is not required."],"exampleFix":"// before\nr = redis.Redis(host=h, client_name='my app name with spaces')\n// after\nr = redis.Redis(host=h, client_name='my-app')","handlingStrategy":"validation","validationCode":"def validate_client_name(name):\n    if name is None:\n        return name\n    if len(name) > 100 or any(c.isspace() for c in name):\n        raise ValueError('client_name must be short and contain no spaces')\n    return name\nclient_name = validate_client_name(raw_name)","typeGuard":null,"tryCatchPattern":"try:\n    r = redis.Redis(host=h, client_name=raw_name)\n    r.ping()\nexcept redis.exceptions.ConnectionError as e:\n    if 'setting client name' in str(e):\n        r = redis.Redis(host=h)  # drop client_name\n        r.ping()","preventionTips":["Keep client_name short and free of spaces/newlines.","Validate generated names before passing them to the client."],"tags":["connection","configuration","handshake"],"analyzedSha":"da03cdc7e8731092b13e395605c3c1fb2de25de1","analyzedAt":"2026-08-04T20:26:47.563Z","schemaVersion":2}