sgl-project/sglang · error · TypeError

Cannot msgpack decode object of type {type(obj)} as {tp} wit

Error message

Cannot msgpack decode object of type {type(obj)} as {tp} with dec_hook. Use an explicit PickleWrapper field via wrap_as_pickle(...) and unwrap_from_pickle(...) for arbitrary payloads, or add a dedicated enc_hook/dec_hook branch for this transport type.

What it means

The mirror of the encode-side error: dec_hook only knows how to reconstruct a few types (torch.Tensor, np.ndarray) from their msgpack tuples. Hitting the raise means the packed stream tagged an ext/annotated type as something dec_hook cannot rebuild — arbitrary payloads must travel as PickleWrapper fields.

Source

Thrown at python/sglang/srt/utils/msgpack_utils.py:213

        "for this transport type."
    )


def dec_hook(tp: type, obj: object) -> object:
    if isinstance(obj, tp):
        return obj
    if tp is array:
        typecode, raw_data = obj
        res = array(typecode)
        res.frombytes(raw_data)
        return res
    if tp is torch.Tensor:
        shape, dtype, data, *device = obj
        return _restore_torch_tensor(shape, dtype, data, device[0] if device else "cpu")
    if tp is np.ndarray:
        shape, dtype, data = obj
        return np.frombuffer(data, dtype=np.dtype(dtype)).copy().reshape(shape)
    raise TypeError(
        f"Cannot msgpack decode object of type {type(obj)} as {tp} with "
        "dec_hook. Use an explicit PickleWrapper field via wrap_as_pickle(...) "
        "and unwrap_from_pickle(...) for arbitrary payloads, or add a "
        "dedicated enc_hook/dec_hook branch for this transport type."
    )


def ext_hook(code: int, data: memoryview) -> object:
    if code not in (
        _MSGPACK_EXT_ARRAY,
        _MSGPACK_EXT_TORCH_TENSOR,
        _MSGPACK_EXT_NP_ARRAY,
        _MSGPACK_EXT_SHM_POINTER_MM_DATA,
        _MSGPACK_EXT_CUDA_IPC_TENSOR_PROXY,
    ):
        return msgspec.msgpack.Ext(code, bytes(data))

    if code == _MSGPACK_EXT_ARRAY:

View on GitHub (pinned to 0132848349)

Solutions

  1. Ensure the sender used wrap_as_pickle(...) for that field so the receiver can unwrap_from_pickle(...) it.
  2. Align sender and receiver versions of sglang so both have the same enc/dec_hook branches.
  3. Add a matching dec_hook branch if you intentionally introduced a new transport type.
Defensive patterns

Strategy: fallback

Try / catch

try:
    obj = msgpack.unpackb(raw, ext_hook=ext_hook)
except TypeError:
    # sender older/newer than receiver: re-route or drop message
    logging.exception("wire format mismatch"); raise

Prevention

When it happens

Trigger: msgpack.unpackb(raw, ext_hook=ext_hook) where the payload contains a type annotation for a type dec_hook doesn't handle (not torch.Tensor/np.ndarray and not a PickleWrapper).

Common situations: Cross-version mismatch: sender added a new transport type branch that the receiver's dec_hook lacks; or a payload packed by unrelated code being decoded with these hooks.

Related errors


AI-assisted analysis of sgl-project/sglang@0132848349 (2026-08-28). Data as JSON: /api/errors/6f19069067c11955. Report an issue: GitHub.