{"record":{"id":"4ae9130b44164b9f","repo":"aio-libs/aiohttp","slug":"data-argument-must-be-byte-ish-r-4ae913","errorCode":null,"errorMessage":"data argument must be byte-ish (%r)","messagePattern":"data argument must be byte-ish \\(%r\\)","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"aiohttp/web_ws.py","lineNumber":475,"sourceCode":"        \"\"\"Send a frame over the websocket.\"\"\"\n        if self._writer is None:\n            raise RuntimeError(\"Call .prepare() first\")\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 self._writer is None:\n            raise RuntimeError(\"Call .prepare() first\")\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 self._writer is None:\n            raise RuntimeError(\"Call .prepare() first\")\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 = json.dumps,\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":457,"sourceCodeEnd":493,"githubUrl":"https://github.com/aio-libs/aiohttp/blob/d041d4d0fd48c3f0832084d33be16cf1c4835f85/aiohttp/web_ws.py#L457-L493","documentation":"Raised by WebSocketResponse.send_bytes() when the `data` argument is not an instance of (bytes, bytearray, memoryview). This is a TypeError whose %r placeholder prints the actual type, e.g. \"data argument must be byte-ish (<class 'str'>)\". aiohttp validates before encoding because the underlying send_frame only accepts a concrete byte buffer and would produce a confusing AttributeError otherwise.","triggerScenarios":"Passing a str to send_bytes (use send_str instead), passing an int/None/dict/list, or passing a JSON encoder that yields str when send_json_bytes expects bytes (the dumps result is forwarded verbatim to send_bytes).","commonSituations":"Mixing up send_str vs send_bytes; using orjson.dumps (which returns bytes) with send_json (which calls send_str) or vice-versa; passing a numpy/array-like object instead of its .tobytes().","solutions":["If you have text, call send_str() instead of send_bytes().","Coerce before sending: `await ws.send_bytes(bytes(data))` for buffer-like objects, or `data.encode('utf-8')` for str.","For JSON produced by an encoder returning bytes (orjson), use send_json_bytes(...) not send_json(...).","Add an isinstance(data, (bytes, bytearray, memoryview)) assert at the call site during development."],"exampleFix":"// before\nawait ws.send_bytes({'k': 1})   # raises\nawait ws.send_bytes('hello')     # raises\n\n// after\nawait ws.send_bytes(json.dumps({'k': 1}).encode())\nawait ws.send_str('hello')\n# or, with orjson:\nawait ws.send_json_bytes({'k': 1}, dumps=orjson.dumps)","handlingStrategy":"type-guard","validationCode":"assert isinstance(data, (bytes, bytearray, memoryview)), f'must be byte-ish, got {type(data)}'\nawait ws.send_bytes(data)","typeGuard":"def is_byteish(data: object) -> bool:\n    return isinstance(data, (bytes, bytearray, memoryview))","tryCatchPattern":"try:\n    await ws.send_bytes(data)\nexcept TypeError as e:\n    if 'byte-ish' in str(e):\n        await ws.send_bytes(bytes(data))  # or send_str if it was text","preventionTips":["Use send_str for str payloads, send_bytes for byte payloads, send_json for objects.","If using orjson (bytes output) pair it with send_json_bytes, not send_json.","Add isinstance asserts in tests around the send call sites."],"tags":["websocket","typing","send","typeerror"],"backgroundTag":null,"analyzedSha":"d041d4d0fd48c3f0832084d33be16cf1c4835f85","analyzedAt":"2026-08-11T20:44:15.550Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}