{"record":{"id":"d9da920e1e0a9030","repo":"apache/beam","slug":"failed-to-marshal-payload-as-proto","errorCode":null,"errorMessage":"failed to marshal payload as proto","messagePattern":"failed to marshal payload as proto","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"sdks/go/pkg/beam/core/runtime/xlangx/payload.go","lineNumber":74,"sourceCode":"\t\tSchema:  scm,\n\t\tPayload: buf.Bytes(),\n\t}\n\treturn ecp, nil\n}\n\n// EncodeStructPayload takes a native Go struct and returns a marshaled\n// ExternalConfigurationPayload proto, containing a Schema representation of\n// the original type and the original value encoded as a Row. This is intended\n// to be used as the expansion payload for an External transform.\nfunc EncodeStructPayload(pl any) ([]byte, error) {\n\tecp, err := CreateExternalConfigurationPayload(pl)\n\tif err != nil {\n\t\treturn []byte{}, err\n\t}\n\n\tplBytes, err := proto.Marshal(ecp)\n\tif err != nil {\n\t\terr = errors.Wrapf(err, \"failed to marshal payload as proto\")\n\t\treturn []byte{}, errors.WithContextf(err, \"encoding external payload %v\", pl)\n\t}\n\n\treturn plBytes, nil\n}\n\n// DecodeStructPayload takes a marshaled ExternalConfigurationPayload proto\n// and returns a native Go struct, with its type converted from the Schema\n// representation and its value decoded from the Row.\nfunc DecodeStructPayload(plBytes []byte) (any, error) {\n\t// Unmarshal payload proto.\n\tecp := &pipepb.ExternalConfigurationPayload{}\n\tif err := proto.Unmarshal(plBytes, ecp); err != nil {\n\t\terr = errors.WithContext(err, \"failed to unmarshal the payload proto\")\n\t\treturn nil, errors.WithContext(err, \"decoding external payload\")\n\t}\n\n\t// Convert Schema representation into payload type.","sourceCodeStart":56,"sourceCodeEnd":92,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/go/pkg/beam/core/runtime/xlangx/payload.go#L56-L92","documentation":"EncodeStructPayload serializes an ExpansionRequest/external payload struct to protobuf bytes for cross-language transform payloads. This error is returned when proto.Marshal fails on the constructed payload proto, wrapped with context naming the payload. proto.Marshal on a generated struct rarely fails, so this usually indicates a corrupted or unsupported message (e.g. containing an unmarshalable Any or invalid proto state).","triggerScenarios":"Calling xlangx.EncodeStructPayload / CrossLanguagePayload with a struct that cannot be marshaled by the protobuf runtime — e.g. a message containing an invalid google.protobuf.Any, unregistered dynamic message, or nil in a required oneof state.","commonSituations":"Passing a wrong type or a struct not generated by protoc-gen-go as the payload; mixing incompatible google.golang.org/protobuf versions (old github.com/golang/protobuf vs new APIv2); payloads built programmatically with reflection.","solutions":["Check the wrapped cause (errors.Unwrap / %v of the returned error) to see why Marshal failed and fix the offending field.","Ensure the payload struct is a protobuf-generated message compatible with the google.golang.org/protobuf runtime in use.","Pin/upgrade google.golang.org/protobuf and github.com/golang/protobuf to consistent versions.","If constructing payloads by hand, validate fields (non-nil oneof case, registered Any types) before calling EncodeStructPayload."],"exampleFix":"// before\npayload := myCustomGoStruct{Data: data}\nb, err := xlangx.EncodeStructPayload(payload)\n\n// after\npayload := &pipepb.ExpansionRequest{Components: components, Namespace: ns}\nb, err := xlangx.EncodeStructPayload(payload)","handlingStrategy":"try-catch","validationCode":"if _, ok := payload.(proto.Message); !ok {\n    return fmt.Errorf(\"payload %T is not a proto.Message\", payload)\n}\nif _, err := proto.Marshal(payload.(proto.Message)); err != nil {\n    return err // fail before the real call\n}","typeGuard":"func isProtoMessage(v any) bool { _, ok := v.(proto.Message); return ok }","tryCatchPattern":"b, err := xlangx.EncodeStructPayload(payload)\nif err != nil {\n    var cause error\n    for cause = err; errors.Unwrap(cause) != nil; cause = errors.Unwrap(cause) {\n    }\n    return fmt.Errorf(\"payload encoding failed for %T: %w\", payload, cause)\n}","preventionTips":["Only pass protobuf-generated structs as payloads.","Keep google.golang.org/protobuf and github.com/golang/protobuf versions consistent.","Log errors.Unwrap(err) to see the root Marshal cause."],"tags":["go","apache-beam","protobuf","serialization"],"backgroundTag":"json-marshal-failed","analyzedSha":"12126d8942aaf848030c478b4c6a28c6af861c66","analyzedAt":"2026-09-13T01:50:10.254Z","contentChangedAt":"2026-09-13T01:50:10.254Z","schemaVersion":2},"datasetVersion":"2026-09-14T16:17:12.679Z"}