go
data_error
ai_generated
true
proto: cannot parse invalid wire-format data
ID: go/protobuf-cannot-unmarshal-wire-type
80%Fix Rate
88%Confidence
0Evidence
2024-03-22First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 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
-
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()) -
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:
-
95% fail
Wire-format mismatch is a schema problem, not a size problem; retrying with more memory won't help.
-
85% fail
If the wire bytes are actually protobuf, JSON parsing fails; if they're JSON, the sender is misconfigured.