{"record":{"id":"2eb71420a03f79f6","repo":"sgl-project/sglang","slug":"cannot-msgpack-encode-object-of-type-type-obj-w","errorCode":null,"errorMessage":"Cannot msgpack encode object of type {type(obj)} with enc_hook. Use an explicit PickleWrapper field via wrap_as_pickle(...) for arbitrary payloads, or add a dedicated enc_hook/dec_hook branch for this transport type.","messagePattern":"Cannot msgpack encode object of type (.+?) with enc_hook\\. Use an explicit PickleWrapper field via wrap_as_pickle\\(\\.\\.\\.\\) for arbitrary payloads, or add a dedicated enc_hook/dec_hook branch for this transport type\\.","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"python/sglang/srt/utils/msgpack_utils.py","lineNumber":191,"sourceCode":"            raw_data,\n        )\n    if isinstance(obj, np.floating):\n        return float(obj)\n    if isinstance(obj, np.integer):\n        return int(obj)\n    if isinstance(obj, np.bool_):\n        return bool(obj)\n    if isinstance(obj, CudaIpcTensorTransportProxy):\n        return _pack_ext(\n            _MSGPACK_EXT_CUDA_IPC_TENSOR_PROXY,\n            _encode_cuda_ipc_tensor_proxy(obj),\n        )\n    if _is_shm_pointer_mm_data(obj):\n        return _pack_ext(\n            _MSGPACK_EXT_SHM_POINTER_MM_DATA,\n            _encode_shm_pointer_mm_data(obj),\n        )\n    raise TypeError(\n        f\"Cannot msgpack encode object of type {type(obj)} with enc_hook. \"\n        \"Use an explicit PickleWrapper field via wrap_as_pickle(...) for \"\n        \"arbitrary payloads, or add a dedicated enc_hook/dec_hook branch \"\n        \"for this transport type.\"\n    )\n\n\ndef dec_hook(tp: type, obj: object) -> object:\n    if isinstance(obj, tp):\n        return obj\n    if tp is array:\n        typecode, raw_data = obj\n        res = array(typecode)\n        res.frombytes(raw_data)\n        return res\n    if tp is torch.Tensor:\n        shape, dtype, data, *device = obj\n        return _restore_torch_tensor(shape, dtype, data, device[0] if device else \"cpu\")","sourceCodeStart":173,"sourceCodeEnd":209,"githubUrl":"https://github.com/sgl-project/sglang/blob/0132848349585cfe6aae51c4941cbae872505f8a/python/sglang/srt/utils/msgpack_utils.py#L173-L209","documentation":"msgpack_utils' enc_hook serializes a fixed set of types (torch.Tensor, np.ndarray, shared-memory pointers, CUDA IPC proxies). Any other non-msgpack-native object reaches the final raise: arbitrary payloads must be explicitly wrapped in PickleWrapper via wrap_as_pickle(...), or a dedicated enc_hook branch must be added.","triggerScenarios":"Calling msgpack pack/encode on a structure containing an object of an unsupported type — e.g., a dataclass, custom class, or torch.dtype — without first passing it through wrap_as_pickle().","commonSituations":"Adding new fields to IPC/ZMQ message types (scheduler↔detokenizer, tokenizer manager) that carry non-primitive objects; upgrading a payload that previously held plain dicts to hold rich Python objects.","solutions":["Wrap the arbitrary field with wrap_as_pickle(obj) when constructing the message, and unwrap_from_pickle(...) on receipt.","Prefer converting to primitives (lists/dicts/bytes) before packing if the payload crosses trust boundaries.","If the type is common and performance-sensitive, add an explicit enc_hook/dec_hook + ext-code branch in msgpack_utils."],"exampleFix":"# before\nmsg = {\"req\": some_custom_object}\npacked = msgpack.packb(msg, default=enc_hook)\n\n# after\nmsg = {\"req\": wrap_as_pickle(some_custom_object)}\npacked = msgpack.packb(msg, default=enc_hook)\n# on the receiver:\nobj = unwrap_from_pickle(msgpack.unpackb(packed, ext_hook=ext_hook)[\"req\"])","handlingStrategy":"type-guard","validationCode":null,"typeGuard":"def is_msgpack_safe(obj) -> bool:\n    import torch\n    return obj is None or isinstance(obj, (bool, int, float, str, bytes, list, tuple, dict, torch.Tensor))","tryCatchPattern":"try:\n    packed = msgpack.packb(msg, default=enc_hook)\nexcept TypeError as e:\n    raise ValueError(f\"payload has unserializable field: {e}\") from e","preventionTips":["Wrap every non-primitive field with wrap_as_pickle at construction time.","Keep wire messages primitive; document the schema."],"tags":["serialization","msgpack","sglang"],"backgroundTag":"unsupported-serialization-type","analyzedSha":"0132848349585cfe6aae51c4941cbae872505f8a","analyzedAt":"2026-08-28T05:10:05.995Z","schemaVersion":2},"datasetVersion":"2026-08-28T06:17:29.519Z"}