{"id":"170de481b66b5878","repo":"redis/redis-py","slug":"protocol-must-be-either-2-or-3","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/asyncio/connection.py","lineNumber":704,"sourceCode":"        self.redis_connect_func = redis_connect_func\n        self._reader: Optional[asyncio.StreamReader] = None\n        self._writer: Optional[asyncio.StreamWriter] = None\n        self._socket_read_size = socket_read_size\n        self._active_read_timeout = None\n        self._connect_callbacks: List[weakref.WeakMethod[ConnectCallbackT]] = []\n        self._buffer_cutoff = 6000\n        self._re_auth_token: Optional[TokenInterface] = None\n        self._should_reconnect = False\n\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 parser_class != _AsyncHiredisParser:\n            # The Python parsers are protocol-specific; hiredis supports both.\n            if self.protocol == 3 and parser_class == _AsyncRESP2Parser:\n                parser_class = _AsyncRESP3Parser\n            elif self.protocol == 2 and parser_class == _AsyncRESP3Parser:\n                parser_class = _AsyncRESP2Parser\n        self.set_parser(parser_class)\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()\n\n        AsyncMaintNotificationsAbstractConnection.__init__(\n            self,\n            maint_notifications_config,","sourceCodeStart":686,"sourceCodeEnd":722,"githubUrl":"https://github.com/redis/redis-py/blob/da03cdc7e8731092b13e395605c3c1fb2de25de1/redis/asyncio/connection.py#L686-L722","documentation":"Raised as ConnectionError in Connection.__init__ after successfully parsing protocol as an int, when the value is outside the supported range (p < 2 or p > 3). redis-py supports only RESP2 and RESP3, so any other integer (0, 1, 4, ...) is rejected.","triggerScenarios":"Passing protocol=1, protocol=4, protocol=0, or a negative int to Redis()/Connection(); a computed protocol value that lands outside 2..3.","commonSituations":"Typos in config (protocol=30), arithmetic that produces a bad value, or copy-paste from docs that used an unsupported version.","solutions":["Use protocol=2 (RESP2) or protocol=3 (RESP3) — the only supported values.","Omit the argument to accept the library default (currently RESP3).","Validate user-supplied config before passing it in (see validationCode)."],"exampleFix":"// before\nclient = Redis(url='...', protocol=30)  # raises [96]\n// after\nclient = Redis(url='...', protocol=3)","handlingStrategy":"validation","validationCode":"if protocol not in (2, 3):\n    raise ValueError(f'protocol must be 2 or 3, got {protocol}')","typeGuard":null,"tryCatchPattern":"from redis.exceptions import ConnectionError\ntry:\n    client = Redis(protocol=protocol)\nexcept ConnectionError as e:\n    if 'protocol must be either 2 or 3' in str(e):\n        client = Redis(protocol=3)","preventionTips":["Restrict protocol to {2, 3}.","Omit the arg to use the library default."],"tags":["connection","protocol","config","validation"],"analyzedSha":"da03cdc7e8731092b13e395605c3c1fb2de25de1","analyzedAt":"2026-08-04T20:26:47.563Z","schemaVersion":2}