go data_error ai_generated true

proto: cannot parse invalid wire-format data

ID: go/protobuf-cannot-unmarshal-wire-type

Also available as: JSON · Markdown · 中文
80%Fix Rate
88%Confidence
0Evidence
2024-03-22First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
google.golang.org/protobuf v1.28+ active

Root Cause

The byte slice passed to proto.Unmarshal is not a valid protobuf encoding for the target message — often because the client and server use different .proto definitions or the payload is not protobuf at all.

generic

中文

传给 proto.Unmarshal 的字节切片不是目标消息的有效 protobuf 编码——通常是客户端与服务端使用了不同的 .proto 定义,或者数据根本不是 protobuf。

Workarounds

  1. 90% success
    Verify the sender and receiver share the exact same .proto file. Regenerate stubs and compare descriptor hashes:
    import "google.golang.org/protobuf/reflect/protoreflect"
    desc := (&pb.MyMsg{}).ProtoReflect().Descriptor()
    log.Printf("msg=%s pkg=%s", desc.FullName(), desc.ParentFile().Package())
  2. 80% success
    Wrap Unmarshal with a length check and hex dump on failure to identify the payload:
    if err := proto.Unmarshal(data, msg); err != nil {
      log.Printf("unmarshal failed: %v, first16=%x", err, data[:min(16,len(data))])
      return err
    }

Dead Ends

Common approaches that don't work:

  1. 95% fail

    Wire-format mismatch is a schema problem, not a size problem; retrying with more memory won't help.

  2. 85% fail

    If the wire bytes are actually protobuf, JSON parsing fails; if they're JSON, the sender is misconfigured.