{"id":"673647ddf8f57c52","repo":"redis/redis-py","slug":"protocol-must-be-either-2-or-3-673647","errorCode":null,"errorMessage":"protocol must be either 2 or 3","messagePattern":"protocol must be either 2 or 3","errorType":"exception","errorClass":"ConnectionError","httpStatus":null,"severity":"error","filePath":"redis/connection.py","lineNumber":929,"sourceCode":"        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\n        # registry (empty if unconfigured) and persists across reconnects.\n        self.himport_registry = himport_registry\n        self._reset_himport_state()","sourceCodeStart":911,"sourceCodeEnd":947,"githubUrl":"https://github.com/redis/redis-py/blob/da03cdc7e8731092b13e395605c3c1fb2de25de1/redis/connection.py#L911-L947","documentation":"Raised as a ConnectionError when the protocol integer is outside the supported range [2, 3]. The library only speaks RESP2 and RESP3, so any other integer (0, 1, 4, ...) is rejected at construction time. Unlike a non-numeric value, this passes int() but fails the bounds check.","triggerScenarios":"Passing protocol=4, protocol=1, protocol=0, or a negative number to redis.Redis(protocol=...). Computing a protocol value dynamically that lands outside 2-3.","commonSituations":"Forward-looking config set to a not-yet-supported version; off-by-one in code that derives the protocol number; copying examples that assume a wider range.","solutions":["Use protocol=2 (RESP2) or protocol=3 (RESP3), the only supported values.","Omit the argument entirely to use the default RESP3.","Fix any calculation that produces an out-of-range protocol number."],"exampleFix":"// before\nr = redis.Redis(protocol=4)\n// after\nr = redis.Redis(protocol=3)","handlingStrategy":"validation","validationCode":"def validate_protocol(p):\n    p = int(p)\n    if p not in (2, 3):\n        raise ValueError('protocol must be 2 or 3')\n    return p\nprotocol = validate_protocol(raw_protocol)","typeGuard":null,"tryCatchPattern":"try:\n    r = redis.Redis(host=h, protocol=raw_protocol)\nexcept redis.exceptions.ConnectionError as e:\n    if 'either 2 or 3' in str(e):\n        raw_protocol = 3\n        r = redis.Redis(host=h, protocol=raw_protocol)","preventionTips":["Restrict protocol to the literal values 2 or 3 in config.","Default to omitting protocol to use RESP3."],"tags":["protocol","configuration","resp3"],"analyzedSha":"da03cdc7e8731092b13e395605c3c1fb2de25de1","analyzedAt":"2026-08-04T20:26:47.563Z","schemaVersion":2}