{"record":{"id":"bb6bb310a659ab0c","repo":"aio-libs/aiohttp","slug":"value-argument-must-be-byte-ish-not-type-value","errorCode":null,"errorMessage":"value argument must be byte-ish, not {type(value)!r}","messagePattern":"value argument must be byte-ish, not (.+?)","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"aiohttp/payload.py","lineNumber":358,"sourceCode":"class BytesPayload(Payload):\n    _value: bytes\n    # _consumed = False (inherited) - Bytes are immutable and can be reused\n    _autoclose = True  # No file handle, just bytes in memory\n\n    def __init__(\n        self, value: bytes | bytearray | memoryview, *args: Any, **kwargs: Any\n    ) -> None:\n        if \"content_type\" not in kwargs:\n            kwargs[\"content_type\"] = \"application/octet-stream\"\n\n        super().__init__(value, *args, **kwargs)\n\n        if isinstance(value, memoryview):\n            self._size = value.nbytes\n        elif isinstance(value, (bytes, bytearray)):\n            self._size = len(value)\n        else:\n            raise TypeError(f\"value argument must be byte-ish, not {type(value)!r}\")\n\n        if self._size > TOO_LARGE_BYTES_BODY:\n            warnings.warn(\n                \"Sending a large body directly with raw bytes might\"\n                \" lock the event loop. You should probably pass an \"\n                \"io.BytesIO object instead\",\n                ResourceWarning,\n                source=self,\n            )\n\n    def decode(self, encoding: str = \"utf-8\", errors: str = \"strict\") -> str:\n        return self._value.decode(encoding, errors)\n\n    async def as_bytes(self, encoding: str = \"utf-8\", errors: str = \"strict\") -> bytes:\n        \"\"\"\n        Return bytes representation of the value.\n\n        This method returns the raw bytes content of the payload.","sourceCodeStart":340,"sourceCodeEnd":376,"githubUrl":"https://github.com/aio-libs/aiohttp/blob/d041d4d0fd48c3f0832084d33be16cf1c4835f85/aiohttp/payload.py#L340-L376","documentation":"Raised by BytesPayload.__init__ when the value argument is not bytes, bytearray, or memoryview. BytesPayload is the factory-selected Payload class for raw binary data, so any non-byte-ish object (most commonly a str) is rejected at construction rather than failing later during serialization. aiohttp will not implicitly encode text for you here.","triggerScenarios":"Passing a str to a request body that resolves to BytesPayload, e.g. session.post(url, data='plain text') in some code paths, or constructing BytesPayload('hello') directly; passing an int, list, or None as the value.","commonSituations":"Sending JSON or form text without encoding; assuming aiohttp auto-encodes str the same way the high-level client does for the data= argument (the low-level BytesPayload does not); feeding a value returned by json.dumps without encode().","solutions":["Encode the string to bytes before passing: payload = BytesPayload(text.encode('utf-8')).","For JSON, use session.post(url, json=obj) or JsonPayload(obj) instead of a raw bytes payload.","For text payloads, use TextPayload or aiohttp's TextIOPayload which handle encoding via content-type charset."],"exampleFix":"# before\npayload = BytesPayload('{\"k\": 1}')\n\n# after\nfrom aiohttp.payload import JsonPayload\npayload = JsonPayload({'k': 1})","handlingStrategy":"type-guard","validationCode":"if isinstance(value, str):\n    value = value.encode('utf-8')\npayload = BytesPayload(value)","typeGuard":"def is_byteish(v) -> bool:\n    return isinstance(v, (bytes, bytearray, memoryview))","tryCatchPattern":"try:\n    payload = BytesPayload(value)\nexcept TypeError:\n    payload = BytesPayload(str(value).encode('utf-8'))","preventionTips":["Encode str to bytes before building a raw payload.","Prefer JsonPayload/TextPayload for structured or text data over BytesPayload.","Add a unit test that feeds the expected types into payload construction."],"tags":["payload","type-error","bytes","serialization"],"backgroundTag":null,"analyzedSha":"d041d4d0fd48c3f0832084d33be16cf1c4835f85","analyzedAt":"2026-08-11T20:44:15.550Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}