{"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":"validation","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/c0ef574e29109210e96e652771ae4e7b88615fa4/aiohttp/payload.py#L340-L376","documentation":"Raised by BytesPayload.__init__ when the `value` argument is not an instance of bytes, bytearray, or memoryview. BytesPayload is strictly for binary buffer types; any other type (str, int, list, etc.) is rejected because it cannot be written as raw octets.","triggerScenarios":"Constructing `BytesPayload(value)` directly, or indirectly by passing a non-byte object to an API that routes to BytesPayload (e.g. `data=<str>` where the registry selects BytesPayload, or explicit instantiation with a str).","commonSituations":"Passing a Python str instead of bytes; passing an int/None/list; a function that sometimes returns str and sometimes bytes; encoding a str without `.encode()`.","solutions":["Encode strings first: `BytesPayload(my_str.encode('utf-8'))`, or use StringPayload for str.","Ensure the value is bytes/bytearray/memoryview before constructing the payload.","For JSON data use JsonPayload; for arbitrary objects convert to bytes explicitly."],"exampleFix":"// before\nBytesPayload('hello')  # str -> TypeError\n// after\nBytesPayload(b'hello')\n# or\nBytesPayload('hello'.encode('utf-8'))\n","handlingStrategy":"type-guard","validationCode":"if not isinstance(value, (bytes, bytearray, memoryview)):\n    raise TypeError('BytesPayload requires bytes/bytearray/memoryview')","typeGuard":"def is_byteish(value) -> bool:\n    return isinstance(value, (bytes, bytearray, memoryview))","tryCatchPattern":"try:\n    payload = BytesPayload(value)\nexcept TypeError:\n    payload = BytesPayload(str(value).encode('utf-8'))","preventionTips":["Always .encode() strings before constructing BytesPayload.","Use StringPayload for str, JsonPayload for JSON-serializable objects.","Assert the type at function boundaries when accepting raw buffers."],"tags":["payload","bytes","type-error","encoding"],"analyzedSha":"c0ef574e29109210e96e652771ae4e7b88615fa4","analyzedAt":"2026-08-04T19:51:05.467Z","schemaVersion":2}