{"id":"944430898c6d862f","repo":"redis/redis-py","slug":"protocol-must-be-an-integer","errorCode":null,"errorMessage":"protocol must be an integer","messagePattern":"protocol must be an integer","errorType":"validation","errorClass":"ConnectionError","httpStatus":null,"severity":"error","filePath":"redis/asyncio/connection.py","lineNumber":701,"sourceCode":"        self.health_check_interval = health_check_interval\n        self.next_health_check: float = -1\n        self.encoder = encoder_class(encoding, encoding_errors, decode_responses)\n        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","sourceCodeStart":683,"sourceCodeEnd":719,"githubUrl":"https://github.com/redis/redis-py/blob/da03cdc7e8731092b13e395605c3c1fb2de25de1/redis/asyncio/connection.py#L683-L719","documentation":"Raised as ConnectionError in Connection.__init__ when int(protocol) raises ValueError — i.e. protocol was a non-empty string that isn't a base-10 integer (e.g. protocol='3.0', protocol='resp3', protocol=True-ish strings). Note: a None or non-coercible type falls through to DEFAULT_RESP_VERSION (TypeError branch), so this specifically fires on string-but-not-int input.","triggerScenarios":"Passing protocol='3.0', protocol='three', protocol='2.5', or any string that fails int() parsing to Redis()/Connection().","commonSituations":"Reading protocol from an env var or YAML as a string and forgetting to cast; URL query parsing that yields 'protocol=3.0'.","solutions":["Pass an integer literal: protocol=3 or protocol=2.","Cast explicitly when reading from config: protocol=int(os.environ['REDIS_PROTOCOL']).","Omit the argument entirely to use the library default."],"exampleFix":"// before\nclient = Redis(url='...', protocol=os.environ['REDIS_PROTO'])  # '3' ok, '3.0' raises [95]\n// after\nclient = Redis(url='..., protocol=int(os.environ['REDIS_PROTO']))","handlingStrategy":"validation","validationCode":"try:\n    protocol = int(protocol)\nexcept (TypeError, ValueError):\n    raise ValueError(f'protocol must be an integer, got {protocol!r}')","typeGuard":null,"tryCatchPattern":"from redis.exceptions import ConnectionError\ntry:\n    client = Redis(protocol=protocol_str)\nexcept ConnectionError as e:\n    if 'protocol must be an integer' in str(e):\n        client = Redis(protocol=int(protocol_str))","preventionTips":["Cast protocol to int when reading from config/env.","Pass integer literals (protocol=3)."],"tags":["connection","protocol","config","validation"],"analyzedSha":"da03cdc7e8731092b13e395605c3c1fb2de25de1","analyzedAt":"2026-08-04T20:26:47.563Z","schemaVersion":2}