go data_error ai_generated true

proto: cannot parse invalid wire-format data

ID: go/protobuf-unknown-field-error

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

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
google.golang.org/protobuf 1.30+ active

Root Cause

The received bytes are not a valid protobuf wire format, often because the client and server disagree on the message type, or because HTTP/JSON data was sent to a gRPC endpoint expecting binary protobuf.

generic

中文

收到的字节不是有效的 protobuf wire format,通常因为客户端与服务端消息类型不一致,或向期待二进制 protobuf 的 gRPC 端点发送了 HTTP/JSON 数据。

Workarounds

  1. 80% success
    md, _ := metadata.FromIncomingContext(ctx)
    log.Printf("content-type=%v len=%d", md.Get("content-type"), len(raw))
    if err := proto.Unmarshal(raw, msg); err != nil {
        return status.Errorf(codes.InvalidArgument, "unmarshal: %v", err)
    }
  2. 90% success
    // pin proto version in go.mod and CI check
    // go.mod: require github.com/acme/protos v1.4.2
    // CI: diff generated files against committed ones

Dead Ends

Common approaches that don't work:

  1. 90% fail

    The bytes are malformed, not truncated; increasing the size limit does not make invalid wire format valid.

  2. 85% fail

    The bytes are binary protobuf, not JSON; protojson will fail with a different parse error.