{"record":{"id":"d717194b31f376eb","repo":"aio-libs/aiohttp","slug":"1007","errorCode":"1007","errorMessage":"Invalid UTF-8 text message","messagePattern":"Invalid UTF-8 text message","errorType":"exception","errorClass":"WebSocketError","httpStatus":null,"severity":"error","filePath":"aiohttp/_websocket/reader_c.py","lineNumber":281,"sourceCode":"                        \"Compressed message has too many deflate members\",\n                    ) from exc\n                if self._max_msg_size and len(payload_merged) > self._max_msg_size:\n                    raise WebSocketError(\n                        WSCloseCode.MESSAGE_TOO_BIG,\n                        f\"Decompressed message exceeds size limit {self._max_msg_size}\",\n                    )\n            elif type(assembled_payload) is bytes:\n                payload_merged = assembled_payload\n            else:\n                payload_merged = bytes(assembled_payload)\n\n            size = len(payload_merged)\n            if opcode == OP_CODE_TEXT:\n                if self._decode_text:\n                    try:\n                        text = payload_merged.decode(\"utf-8\")\n                    except UnicodeDecodeError as exc:\n                        raise WebSocketError(\n                            WSCloseCode.INVALID_TEXT, \"Invalid UTF-8 text message\"\n                        ) from exc\n\n                    # XXX: The Text and Binary messages here can be a performance\n                    # bottleneck, so we use tuple.__new__ to improve performance.\n                    # This is not type safe, but many tests should fail in\n                    # test_client_ws_functional.py if this is wrong.\n                    msg = TUPLE_NEW(WSMessageText, (text, size, \"\", WS_MSG_TYPE_TEXT))\n                else:\n                    # Return raw bytes for TEXT messages when decode_text=False\n                    msg = TUPLE_NEW(\n                        WSMessageTextBytes, (payload_merged, size, \"\", WS_MSG_TYPE_TEXT)\n                    )\n            else:\n                msg = TUPLE_NEW(\n                    WSMessageBinary, (payload_merged, size, \"\", WS_MSG_TYPE_BINARY)\n                )\n","sourceCodeStart":263,"sourceCodeEnd":299,"githubUrl":"https://github.com/aio-libs/aiohttp/blob/d041d4d0fd48c3f0832084d33be16cf1c4835f85/aiohttp/_websocket/reader_c.py#L263-L299","documentation":"A TEXT (opcode 0x01) frame's assembled payload is not valid UTF-8. RFC 6455 §6.1 requires the connection to fail with code 1007 when a text payload is non-UTF-8; aiohttp catches `UnicodeDecodeError` and closes with 1007.","triggerScenarios":"Peer sends a TEXT frame containing bytes that are not valid UTF-8 (e.g. raw Latin-1, a multibyte sequence split incorrectly across fragments after assembly, or binary mislabeled as text).","commonSituations":"Peer encodes strings in a non-UTF-8 encoding; a proxy corrupts/reassembles fragments; binary data accidentally sent as a TEXT frame.","solutions":["Fix the peer to send UTF-8-encoded text, or use BINARY frames for non-text data.","Handle `WSMsgType.ERROR` and reconnect — the connection is already failed with 1007."],"exampleFix":null,"handlingStrategy":"type-guard","validationCode":null,"typeGuard":"def is_error(msg) -> bool:\n    return msg.type == aiohttp.WSMsgType.ERROR","tryCatchPattern":"msg = await ws.receive()\nif msg.type == aiohttp.WSMsgType.ERROR:\n    exc = msg.data            # a WebSocketError\n    close_code = exc.code     # 1002/1007/1009/...\n    log.warning(\"ws protocol error %s: %s\", close_code, exc)","preventionTips":["If you generate text frames, always encode with `.encode('utf-8')`.","Use BINARY (`send_bytes`) for anything that is not definitely UTF-8 text."],"tags":["websocket","protocol","encoding","utf-8","rfc6455","close-code-1007"],"backgroundTag":null,"analyzedSha":"d041d4d0fd48c3f0832084d33be16cf1c4835f85","analyzedAt":"2026-08-11T20:44:15.550Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}