{"id":"ec53675de88cce77","repo":"redis/redis-py","slug":"protocol-must-be-an-integer-ec5367","errorCode":null,"errorMessage":"protocol must be an integer","messagePattern":"protocol must be an integer","errorType":"exception","errorClass":"ConnectionError","httpStatus":null,"severity":"error","filePath":"redis/connection.py","lineNumber":926,"sourceCode":"                self.retry.update_supported_errors(self.retry_on_error)\n        else:\n            self.retry = Retry(NoBackoff(), 0)\n        self.health_check_interval = health_check_interval\n        self.next_health_check = 0\n        self.redis_connect_func = redis_connect_func\n        self.encoder = Encoder(encoding, encoding_errors, decode_responses)\n        self.handshake_metadata = None\n        self._sock = None\n        self._socket_read_size = socket_read_size\n        self._connect_callbacks = []\n        self._buffer_cutoff = 6000\n        self._re_auth_token: Optional[TokenInterface] = None\n        try:\n            p = int(protocol)\n        except TypeError:\n            p = DEFAULT_RESP_VERSION\n        except ValueError:\n            raise ConnectionError(\"protocol must be an integer\")\n        else:\n            if p < 2 or p > 3:\n                raise ConnectionError(\"protocol must be either 2 or 3\")\n        self.protocol = p\n        self.legacy_responses = legacy_responses\n        if self.protocol == 3 and parser_class == _RESP2Parser:\n            # If the protocol is 3 but the parser is RESP2, change it to RESP3\n            # This is needed because the parser might be set before the protocol\n            # or might be provided as a kwarg to the constructor\n            # We need to react on discrepancy only for RESP2 and RESP3\n            # as hiredis supports both\n            parser_class = _RESP3Parser\n        self.set_parser(parser_class)\n\n        self._command_packer = self._construct_command_packer(command_packer)\n        self._should_reconnect = False\n\n        # HIMPORT client-side state. `himport_registry` is the shared client-level","sourceCodeStart":908,"sourceCodeEnd":944,"githubUrl":"https://github.com/redis/redis-py/blob/da03cdc7e8731092b13e395605c3c1fb2de25de1/redis/connection.py#L908-L944","documentation":"Raised as a ConnectionError when the 'protocol' argument cannot be parsed as an integer (int(protocol) raises ValueError, e.g. protocol='abc'). Note that None triggers a TypeError which silently falls back to the default RESP version (3), so this error specifically means a non-numeric string/object that int() rejects. The value must be convertible to int.","triggerScenarios":"Passing protocol='three', protocol='resp3', or any non-numeric string to redis.Redis(protocol=...) or the Connection constructor. Reading protocol from an env var that holds a word rather than a digit.","commonSituations":"Misconfigured REDIS_PROTOCOL environment variable containing a label instead of a number; passing a bool or object that int() rejects; typos in config files.","solutions":["Pass protocol as an integer literal: protocol=2 or protocol=3.","Sanitize env-var values: protocol=int(os.environ['REDIS_PROTOCOL']) after validating it is numeric.","Omit protocol to use the default (RESP3)."],"exampleFix":"// before\nr = redis.Redis(protocol='resp3')\n// after\nr = redis.Redis(protocol=3)","handlingStrategy":"validation","validationCode":"def parse_protocol(raw):\n    try:\n        return int(raw)\n    except (TypeError, ValueError):\n        raise ValueError(f'protocol must be an integer, got {raw!r}')\nprotocol = parse_protocol(os.environ.get('REDIS_PROTOCOL', 3))","typeGuard":null,"tryCatchPattern":"try:\n    r = redis.Redis(host=h, protocol=raw_protocol)\nexcept redis.exceptions.ConnectionError as e:\n    if 'protocol must be an integer' in str(e):\n        raw_protocol = 3\n        r = redis.Redis(host=h, protocol=raw_protocol)","preventionTips":["Coerce and validate protocol to int before passing it to the client.","Keep REDIS_PROTOCOL env var numeric."],"tags":["protocol","configuration","resp3"],"analyzedSha":"da03cdc7e8731092b13e395605c3c1fb2de25de1","analyzedAt":"2026-08-04T20:26:47.563Z","schemaVersion":2}