{"record":{"id":"3ce07b75e39d9205","repo":"pypa/pip","slug":"data-must-have-bytes-type","errorCode":null,"errorMessage":"data must have bytes type","messagePattern":"data must have bytes type","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"src/pip/_vendor/msgpack/fallback.py","lineNumber":848,"sourceCode":"            self._buffer = BytesIO()\n            return ret\n\n    def pack_map_header(self, n):\n        if n >= 2**32:\n            raise ValueError\n        self._pack_map_header(n)\n        if self._autoreset:\n            ret = self._buffer.getvalue()\n            self._buffer = BytesIO()\n            return ret\n\n    def pack_ext_type(self, typecode, data):\n        if not isinstance(typecode, int):\n            raise TypeError(\"typecode must have int type.\")\n        if not 0 <= typecode <= 127:\n            raise ValueError(\"typecode should be 0-127\")\n        if not isinstance(data, bytes):\n            raise TypeError(\"data must have bytes type\")\n        L = len(data)\n        if L > 0xFFFFFFFF:\n            raise ValueError(\"Too large data\")\n        if L == 1:\n            self._buffer.write(b\"\\xd4\")\n        elif L == 2:\n            self._buffer.write(b\"\\xd5\")\n        elif L == 4:\n            self._buffer.write(b\"\\xd6\")\n        elif L == 8:\n            self._buffer.write(b\"\\xd7\")\n        elif L == 16:\n            self._buffer.write(b\"\\xd8\")\n        elif L <= 0xFF:\n            self._buffer.write(b\"\\xc7\" + struct.pack(\"B\", L))\n        elif L <= 0xFFFF:\n            self._buffer.write(b\"\\xc8\" + struct.pack(\">H\", L))\n        else:","sourceCodeStart":830,"sourceCodeEnd":866,"githubUrl":"https://github.com/pypa/pip/blob/f399c3718970b1b0e2478dac5296eb62679a9b86/src/pip/_vendor/msgpack/fallback.py#L830-L866","documentation":"Raised by msgpack's pure-Python Packer.pack_ext_type when the data argument is not a bytes instance (bytearray or memoryview are also rejected here — it checks isinstance(data, bytes) strictly). The ext type payload must be raw bytes for the binary wire format. Note ExtType.__new__ in ext.py:12 enforces the same constraint.","triggerScenarios":"Calling packer.pack_ext_type(1, 'string data') or packer.pack_ext_type(1, bytearray(b'x')) or packer.pack_ext_type(1, [1,2,3]). Any non-bytes data type triggers it.","commonSituations":"Passing a string instead of bytes; using bytearray (which pack_ext_type rejects, though _pack accepts bytearray for bin types); list/int payload that hasn't been pre-serialized.","solutions":["Convert data to bytes before calling pack_ext_type: data = bytes(data) or data = data.encode('utf-8').","If using bytearray, convert with bytes(bytearray_obj).","If using memoryview, call .tobytes() first.","Pre-serialize structured data to bytes before wrapping in an ext type."],"exampleFix":"# before\npacker.pack_ext_type(1, 'hello')  # str — raises\n\n# after\npacker.pack_ext_type(1, b'hello')  # bytes","handlingStrategy":"type-guard","validationCode":"def ensure_bytes(data) -> bytes:\n    if isinstance(data, str):\n        return data.encode('utf-8')\n    if isinstance(data, (bytearray, memoryview)):\n        return bytes(data)\n    if not isinstance(data, bytes):\n        raise TypeError(f'data must be bytes, got {type(data).__name__}')\n    return data","typeGuard":"def is_bytes_data(data) -> bool:\n    return isinstance(data, bytes)","tryCatchPattern":"try:\n    packer.pack_ext_type(code, data)\nexcept TypeError as e:\n    if 'must have bytes type' in str(e):\n        data = bytes(data) if not isinstance(data, str) else data.encode()\n        packer.pack_ext_type(code, data)\n    else:\n        raise","preventionTips":["Always encode strings to bytes before using as ext data.","Convert bytearray/memoryview to bytes for ext types.","Pre-serialize structured payloads to bytes."],"tags":["msgpack","ext-type","type-error","bytes"],"backgroundTag":null,"analyzedSha":"f399c3718970b1b0e2478dac5296eb62679a9b86","analyzedAt":"2026-08-08T23:01:42.227Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}