go data_error ai_generated true

proto: cannot parse invalid wire-format data

ID: go/proto-unmarshal-invalid-wire-type

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

Version Compatibility

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

Root Cause

The bytes handed to proto.Unmarshal do not conform to the protobuf wire format. Usually caused by passing JSON, plain text, or a truncated/concatenated buffer to a binary proto unmarshaler.

generic

中文

传给 proto.Unmarshal 的字节不符合 protobuf wire 格式。通常是因为把 JSON、纯文本或截断/拼接的缓冲区交给了二进制 proto 反序列化器。

Workarounds

  1. 90% success
    if err := proto.Unmarshal(data, msg); err != nil {
        // if data is JSON, use:
        err = protojson.Unmarshal(data, msg)
    }
  2. 85% success
    log.Printf("len=%d head=%x", len(data), data[:min(16,len(data))])

Dead Ends

Common approaches that don't work:

  1. 95% fail

    The buffer is deterministic; malformed bytes never become valid on retry. The error is not transient.

  2. 90% fail

    On wire-format errors the message is left in an undefined/partial state; downstream logic reads zero or garbage fields.