go data_error ai_generated true

proto: cannot parse invalid wire-format data

ID: go/protobuf-unmarshal-invalid-wire

Also available as: JSON · Markdown · 中文
80%Fix Rate
87%Confidence
0Evidence
2024-05-15First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
google.golang.org/protobuf 1.x active — — —

Root Cause

The bytes passed to proto.Unmarshal do not conform to protobuf wire format — often because the data is JSON, empty, truncated, or a different proto message type entirely.

generic

中文

传给 proto.Unmarshal 的字节不符合 protobuf wire format——通常因为数据是 JSON、空、被截断,或完全是另一种 proto 消息类型。

Workarounds

  1. 85% success
    var msg pb.User
    if json.Valid(data) {
      err = protojson.Unmarshal(data, &msg)
    } else {
      err = proto.Unmarshal(data, &msg)
    }
    if err != nil { return fmt.Errorf("decode: %w", err) }
  2. 80% success
    if len(data) > 0 {
      log.Printf("first bytes: %x", data[:min(len(data),16)])
    }
    // JSON starts with '{' = 0x7b; protobuf field tag usually 0x0a etc.

Dead Ends

Common approaches that don't work:

  1. 95% fail

    Wire format is deterministic; identical bytes yield identical failure.

  2. 70% fail

    If bytes are binary protobuf, JSON parser fails; if JSON, the original was wrong type.