{"record":{"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":"validation","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/6a6b581b48225afa0b76912d1028c6035baee932/redis/connection.py#L911-L947","documentation":"Raised in AbstractConnection.__init__ (connection.py:928-929) after int(protocol) succeeds but the value is outside the supported range [2,3]. redis-py only speaks RESP2 and RESP3, so any other integer (1, 4, 0, -1) is rejected. This guards against silently negotiating an unsupported wire version.","triggerScenarios":"Passing protocol=1, protocol=4, protocol=0, or any integer not equal to 2 or 3 to redis.Redis/Connection.","commonSituations":"Typo (protocol=4); arithmetic/config drift producing an out-of-range number; assuming a newer RESP version exists before upgrading the library.","solutions":["Use protocol=2 or protocol=3 (3 is the current default).","Validate config at load time: if proto not in (2,3): raise/bound to 3.","Upgrade redis-py if you genuinely need a newer RESP version that may have shipped."],"exampleFix":"# before\nr = redis.Redis(host=h, port=p, protocol=4)\n# after\nr = redis.Redis(host=h, port=p, protocol=3)","handlingStrategy":"validation","validationCode":"PROTO = int(proto) if proto is not None else 3\nassert PROTO in (2, 3), f'protocol must be 2 or 3, got {PROTO}'\nr = redis.Redis(host=h, port=p, protocol=PROTO)","typeGuard":"def is_supported_resp(p) -> bool:\n    try:\n        return int(p) in (2, 3)\n    except (TypeError, ValueError):\n        return False","tryCatchPattern":"from redis.exceptions import ConnectionError\ntry:\n    r = redis.Redis(host=h, port=p, protocol=proto)\nexcept ConnectionError as e:\n    if 'either 2 or 3' in str(e):\n        r = redis.Redis(host=h, port=p, protocol=3)\n    else:\n        raise","preventionTips":["Bound protocol to {2,3} at the config layer.","Add a unit test asserting your config loader never emits out-of-range values."],"tags":["protocol","configuration","validation","resp"],"backgroundTag":null,"analyzedSha":"6a6b581b48225afa0b76912d1028c6035baee932","analyzedAt":"2026-08-10T12:52:44.840Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-21T04:17:39.646Z"}