{"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/c0ef574e29109210e96e652771ae4e7b88615fa4/aiohttp/client_ws.py#L471-L507","documentation":"Raised by ClientWebSocketResponse.receive_bytes() when the next message is not a BINARY frame. receive_bytes promises bytes, so a TEXT frame, a CLOSE/CLOSED control frame, or an ERROR frame makes the contract impossible to keep and aiohttp raises WSMessageTypeError (TypeError subclass). The offending type and data are included in the message for debugging.","triggerScenarios":"Calling `await ws.receive_bytes()` when the peer sent a TEXT frame, or when the socket returned a CLOSE/ERROR/CLOSED control message.","commonSituations":"Server normally sends text but client assumed binary. Peer closed the connection mid-exchange. A protocol version mismatch where the server picks text framing. Using receive_bytes() after the connection has already started draining control frames.","solutions":["Switch to `msg = await ws.receive()` and branch on `msg.type == WSMsgType.BINARY`.","Verify the server actually sends BINARY frames; the message shows the actual frame type received.","Catch WSMessageTypeError around receive_bytes() to recover gracefully and treat it as a protocol error / connection end."],"exampleFix":"// before\ndata = await ws.receive_bytes()\n// after\nfrom aiohttp import WSMsgType\nmsg = await ws.receive()\nif msg.type == WSMsgType.BINARY:\n    data = msg.data\nelse:\n    raise RuntimeError(f\"expected binary, got {msg.type}\")","handlingStrategy":"try-catch","validationCode":"from aiohttp import WSMsgType\nasync def safe_receive_bytes(ws):\n    msg = await ws.receive()\n    if msg.type is not WSMsgType.BINARY:\n        raise WSMessageTypeError(f\"expected BINARY, got {msg.type}\")\n    return msg.data","typeGuard":"from aiohttp import WSMsgType\nfrom aiohttp.http_websocket import WSMessage\n\ndef is_binary_message(msg: WSMessage) -> bool:\n    return msg.type is WSMsgType.BINARY","tryCatchPattern":"from aiohttp import WSMessageTypeError\ntry:\n    data = await ws.receive_bytes()\nexcept WSMessageTypeError as e:\n    if ws.closed:\n        return None\n    raise","preventionTips":["Use ws.receive() and check WSMsgType.BINARY when the peer might send text or control frames.","Stop calling receive_bytes() after observing CLOSED/ERROR.","Negotiate subprotocol/encoding with the server up front so both sides agree on binary vs text."],"tags":["websocket","client","message-type","receive"],"analyzedSha":"c0ef574e29109210e96e652771ae4e7b88615fa4","analyzedAt":"2026-08-04T19:51:05.467Z","schemaVersion":2}