{"id":"4a4a83ac04722223","repo":"aio-libs/aiohttp","slug":"received-message-msg-type-msg-data-r-is-not-ws-4a4a83","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/web_ws.py","lineNumber":701,"sourceCode":"        self: \"WebSocketResponse[_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: \"WebSocketResponse[Literal[True]]\",\n        *,\n        loads: JSONDecoder = ...,\n        timeout: float | None = None,\n    ) -> Any: ...\n\n    @overload\n    async def receive_json(\n        self: \"WebSocketResponse[Literal[False]]\",\n        *,\n        loads: Callable[[bytes], Any] = ...,\n        timeout: float | None = None,","sourceCodeStart":683,"sourceCodeEnd":719,"githubUrl":"https://github.com/aio-libs/aiohttp/blob/c0ef574e29109210e96e652771ae4e7b88615fa4/aiohttp/web_ws.py#L683-L719","documentation":"Raised by WebSocketResponse.receive_bytes() as a WSMessageTypeError (a TypeError subclass) when the received message is not a BINARY frame. receive_bytes() calls receive() then asserts msg.type is WSMsgType.BINARY; a TEXT frame or a control/close message fails the check. The actual type and data are included in the message.","triggerScenarios":"Calling `await ws.receive_bytes()` when the peer sent a TEXT frame, or when receive() returned a CLOSE/CLOSING/CLOSED/PING/PONG control message. The check at aiohttp/web_ws.py:700-703 raises.","commonSituations":"Peer sends text but the server assumes binary; a client sending JSON text when the server expected msgpack/protobuf binary; receive_bytes() called after the connection began closing so receive() returns a CLOSE sentinel.","solutions":["Use the generic receive() and branch on msg.type to handle TEXT and control frames.","Verify `msg.type == WSMsgType.BINARY` before interpreting data as bytes.","Catch WSMessageTypeError (from aiohttp.client_exceptions) to recover gracefully.","Agree on a single frame-type convention with the peer."],"exampleFix":"# before\nasync for _ in ws:\n    data = await ws.receive_bytes()  # raises on TEXT/CLOSE\n\n# after\nasync for msg in ws:\n    if msg.type == WSMsgType.BINARY:\n        handle_binary(msg.data)\n    elif msg.type == WSMsgType.TEXT:\n        handle_text(msg.data)","handlingStrategy":"try-catch","validationCode":"from aiohttp import WSMsgType\n\nasync def receive_binary(ws):\n    msg = await ws.receive()\n    if msg.type is WSMsgType.BINARY:\n        return msg.data\n    return None","typeGuard":"from aiohttp import WSMsgType\n\ndef is_binary(msg) -> bool:\n    return msg.type is WSMsgType.BINARY","tryCatchPattern":"from aiohttp.client_exceptions import WSMessageTypeError\n\ntry:\n    data = await ws.receive_bytes()\nexcept WSMessageTypeError:\n    msg = await ws.receive()  # handle non-binary frame","preventionTips":["Branch on msg.type from generic receive() for mixed protocols.","Catch WSMessageTypeError to recover from unexpected frame types.","Confirm the peer's frame convention before assuming binary."],"tags":["websocket","server","types","receive"],"analyzedSha":"c0ef574e29109210e96e652771ae4e7b88615fa4","analyzedAt":"2026-08-04T19:51:05.467Z","schemaVersion":2}