{"record":{"id":"9055674310a7309b","repo":"aio-libs/aiohttp","slug":"received-message-msg-type-msg-data-r-is-not-ws-905567","errorCode":null,"errorMessage":"Received message {msg.type}:{msg.data!r} is not WSMsgType.BINARY","messagePattern":"Received message (.+?):(.+?) is not WSMsgType\\.BINARY","errorType":"exception","errorClass":"WSMessageTypeError","httpStatus":null,"severity":"error","filePath":"aiohttp/client_ws.py","lineNumber":489,"sourceCode":"        self: \"ClientWebSocketResponse[_DecodeText]\", *, timeout: float | None = None\n    ) -> str | bytes: ...\n\n    async def receive_str(self, *, timeout: float | None = None) -> str | bytes:\n        \"\"\"Receive TEXT message.\n\n        Returns str when decode_text=True (default), bytes when decode_text=False.\n        \"\"\"\n        msg = await self.receive(timeout)\n        if msg.type is not WSMsgType.TEXT:\n            raise WSMessageTypeError(\n                f\"Received message {msg.type}:{msg.data!r} is not WSMsgType.TEXT\"\n            )\n        return msg.data\n\n    async def receive_bytes(self, *, timeout: float | None = None) -> bytes:\n        msg = await self.receive(timeout)\n        if msg.type is not WSMsgType.BINARY:\n            raise WSMessageTypeError(\n                f\"Received message {msg.type}:{msg.data!r} is not WSMsgType.BINARY\"\n            )\n        return msg.data\n\n    @overload\n    async def receive_json(\n        self: \"ClientWebSocketResponse[Literal[True]]\",\n        *,\n        loads: JSONDecoder = ...,\n        timeout: float | None = None,\n    ) -> Any: ...\n\n    @overload\n    async def receive_json(\n        self: \"ClientWebSocketResponse[Literal[False]]\",\n        *,\n        loads: Callable[[bytes], Any] = ...,\n        timeout: float | None = None,","sourceCodeStart":471,"sourceCodeEnd":507,"githubUrl":"https://github.com/aio-libs/aiohttp/blob/d041d4d0fd48c3f0832084d33be16cf1c4835f85/aiohttp/client_ws.py#L471-L507","documentation":"Raised by receive_bytes() when the next message is not WSMsgType.BINARY. It is the binary counterpart of error 82: receive_bytes() assumes the next frame is BINARY and rejects TEXT, CLOSE, PING, PONG, or ERROR frames with WSMessageTypeError.","triggerScenarios":"Server sends a TEXT frame (e.g. an error string) while client loops on receive_bytes(); a CLOSE frame arrives mid-stream; PING/PONG control frames are queued ahead of the next binary payload.","commonSituations":"Mixed-protocol servers that interleave text status messages with binary data; client assuming pure-binary when the server sends a textual handshake/error; teardown ordering.","solutions":["Drive a ws.receive() loop and branch on msg.type before decoding.","If text frames are legitimate, call receive_str() for those or fall back to receive().","Break the loop cleanly on CLOSE/CLOSED instead of calling receive_bytes() again.","Catch WSMessageTypeError to survive unexpected text frames during binary streaming."],"exampleFix":"# before\nasync for _ in range(10):\n    payload = await ws.receive_bytes()\n# after\nwhile True:\n    msg = await ws.receive()\n    if msg.type is WSMsgType.BINARY:\n        payload = msg.data\n    elif msg.type is WSMsgType.TEXT:\n        log.warning('unexpected text: %r', msg.data)\n    elif msg.type in (WSMsgType.CLOSED, WSMsgType.CLOSE):\n        break","handlingStrategy":"validation","validationCode":"async def receive_binary(ws, timeout=None):\n    msg = await ws.receive(timeout)\n    if msg.type is aiohttp.WSMsgType.BINARY:\n        return msg.data\n    if msg.type in (aiohttp.WSMsgType.CLOSED, aiohttp.WSMsgType.CLOSE):\n        raise ConnectionResetError('websocket closed')\n    return None","typeGuard":"from aiohttp import WSMsgType\n\ndef is_binary(msg) -> bool:\n    return msg.type is WSMsgType.BINARY","tryCatchPattern":"from aiohttp import WSMessageTypeError\ntry:\n    payload = await ws.receive_bytes()\nexcept WSMessageTypeError as exc:\n    log.warning('non-binary frame ignored: %s', exc)\n    payload = b''","preventionTips":["Use a receive() loop and branch on msg.type for mixed-protocol servers.","Confirm the server's contract: pure-binary vs mixed before assuming receive_bytes() always works.","Stop calling receive_bytes() once a CLOSE frame has been observed."],"tags":["websocket","client","message-type","receive-bytes"],"backgroundTag":null,"analyzedSha":"d041d4d0fd48c3f0832084d33be16cf1c4835f85","analyzedAt":"2026-08-11T20:44:15.550Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}