{"record":{"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/d041d4d0fd48c3f0832084d33be16cf1c4835f85/aiohttp/web_ws.py#L683-L719","documentation":"Raised by WebSocketResponse.receive_bytes() as a WSMessageTypeError (a TypeError subclass) when the message returned by receive() is not WSMsgType.BINARY. receive_bytes() expects a binary frame; receiving a TEXT frame, or any control/closing frame, trips the guard. The %r formatting shows the offending frame type and data.","triggerScenarios":"Peer sends a TEXT frame when the server calls receive_bytes(); a CLOSE/CLOSED frame arrives during a receive_bytes loop; the client serializes with json.dumps (str) but the server expects bytes.","commonSituations":"Mismatched framing between client (text JSON) and server (expecting binary); control frames interrupting a binary read loop; reusing receive_bytes when the protocol actually sends text.","solutions":["Use receive() and dispatch on msg.type so TEXT, BINARY, CLOSE, and ERROR are handled distinctly.","Catch WSMessageTypeError and either switch to text handling or close with a type-mismatch code.","Align the client to send BINARY frames (e.g. orjson.dumps which returns bytes)."],"exampleFix":"// before\npayload = await ws.receive_bytes()  # raises on text\n\n// after\nfrom aiohttp import WSMessageTypeError\ntry:\n    payload = await ws.receive_bytes()\nexcept WSMessageTypeError:\n    msg = await ws.receive()\n    if msg.type == WSMsgType.TEXT:\n        payload = msg.data.encode('utf-8')\n    else:\n        raise\n# or dispatch up front:\nmsg = await ws.receive()\nif msg.type == WSMsgType.BINARY: ...\nelif msg.type == WSMsgType.TEXT: ...","handlingStrategy":"try-catch","validationCode":"from aiohttp import WSMessageTypeError\ntry:\n    payload = await ws.receive_bytes()\nexcept WSMessageTypeError:\n    msg = await ws.receive()\n    if msg.type == WSMsgType.BINARY:\n        payload = msg.data\n    else:\n        raise","typeGuard":"def is_binary_msg(msg) -> bool:\n    return msg.type is WSMsgType.BINARY","tryCatchPattern":"try:\n    payload = await ws.receive_bytes()\nexcept WSMessageTypeError as e:\n    await ws.close(code=WSCloseCode.UNSUPPORTED_DATA, message=b'expected binary')\n    raise","preventionTips":["Use receive() with type dispatch for protocols that mix frame types.","Ensure clients serialize to bytes (e.g. orjson.dumps) when the server reads binary.","Catch WSMessageTypeError specifically rather than broad TypeError."],"tags":["websocket","receive","typing","protocol","typeerror"],"backgroundTag":null,"analyzedSha":"d041d4d0fd48c3f0832084d33be16cf1c4835f85","analyzedAt":"2026-08-11T20:44:15.550Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}