{"id":"ab4236ef6f4a84fb","repo":"aio-libs/aiohttp","slug":"data-argument-must-be-byte-ish-r","errorCode":null,"errorMessage":"data argument must be byte-ish (%r)","messagePattern":"data argument must be byte-ish \\(%r\\)","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"aiohttp/client_ws.py","lineNumber":294,"sourceCode":"    async def pong(self, message: bytes = b\"\") -> None:\n        await self._writer.send_frame(message, WSMsgType.PONG)\n\n    async def send_frame(\n        self, message: bytes, opcode: WSMsgType, compress: int | None = None\n    ) -> None:\n        \"\"\"Send a frame over the websocket.\"\"\"\n        await self._writer.send_frame(message, opcode, compress)\n\n    async def send_str(self, data: str, compress: int | None = None) -> None:\n        if not isinstance(data, str):\n            raise TypeError(\"data argument must be str (%r)\" % type(data))\n        await self._writer.send_frame(\n            data.encode(\"utf-8\"), WSMsgType.TEXT, compress=compress\n        )\n\n    async def send_bytes(self, data: bytes, compress: int | None = None) -> None:\n        if not isinstance(data, (bytes, bytearray, memoryview)):\n            raise TypeError(\"data argument must be byte-ish (%r)\" % type(data))\n        await self._writer.send_frame(data, WSMsgType.BINARY, compress=compress)\n\n    async def send_json(\n        self,\n        data: Any,\n        compress: int | None = None,\n        *,\n        dumps: JSONEncoder = DEFAULT_JSON_ENCODER,\n    ) -> None:\n        await self.send_str(dumps(data), compress=compress)\n\n    async def send_json_bytes(\n        self,\n        data: Any,\n        compress: int | None = None,\n        *,\n        dumps: JSONBytesEncoder,\n    ) -> None:","sourceCodeStart":276,"sourceCodeEnd":312,"githubUrl":"https://github.com/aio-libs/aiohttp/blob/c0ef574e29109210e96e652771ae4e7b88615fa4/aiohttp/client_ws.py#L276-L312","documentation":"Raised as TypeError in ClientWebSocketResponse.send_bytes() when data is not a bytes-like object. Binary frames require raw bytes; the accepted types are bytes, bytearray, and memoryview.","triggerScenarios":"Fires at line 293-294 when isinstance(data, (bytes, bytearray, memoryview)) is False. Triggered by passing a str ('hello'), an int, a list of ints, or None to send_bytes.","commonSituations":"Passing a str to send_bytes (use send_str instead); passing JSON as a string instead of bytes; passing a numpy array or other buffer-like object without conversion.","solutions":["Pass bytes/bytearray/memoryview to send_bytes.","Encode str first: data.encode('utf-8') or just use send_str.","Convert buffer protocol objects: bytes(arr) or memoryview(arr).","For JSON use send_json_bytes with an orjson-style encoder."],"exampleFix":"# before\nawait ws.send_bytes('hello')      # str\nawait ws.send_bytes([1,2,3])      # list\n# after\nawait ws.send_bytes(b'hello')\nawait ws.send_bytes(bytes([1,2,3]))\n# or for text\nawait ws.send_str('hello')","handlingStrategy":"type-guard","validationCode":"if not isinstance(data, (bytes, bytearray, memoryview)):\n    raise TypeError(f'send_bytes requires bytes-like, got {type(data).__name__}')\nawait ws.send_bytes(data)","typeGuard":"def is_ws_binary(data) -> bool:\n    return isinstance(data, (bytes, bytearray, memoryview))","tryCatchPattern":null,"preventionTips":["Convert buffer-protocol objects (numpy arrays, array.array) to bytes first.","Encode str via send_str rather than passing encoded bytes to send_bytes unless you want a binary frame.","Annotate helper functions with the bytes-like type to catch errors at typecheck time."],"tags":["websocket","type-error","validation","client-ws"],"analyzedSha":"c0ef574e29109210e96e652771ae4e7b88615fa4","analyzedAt":"2026-08-04T19:51:05.467Z","schemaVersion":2}