{"record":{"id":"50ff23f5a14a45d5","repo":"apache/beam","slug":"unencodable-type-v-try-to-wrap-the-type-as-a-field-in-a","errorCode":null,"errorMessage":"unencodable type '%v', try to wrap the type as a field in a struct, see https://github.com/apache/beam/issues/23101 for details","messagePattern":"unencodable type '(.+?)', try to wrap the type as a field in a struct, see https://github\\.com/apache/beam/issues/23101 for details","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"sdks/go/pkg/beam/core/runtime/graphx/serialize.go","lineNumber":508,"sourceCode":"\t\t\treturn nil, errors.WithContextf(wrapped, \"encoding channel %v\", t)\n\t\t}\n\t\tdir, err := encodeChanDir(t.ChanDir())\n\t\tif err != nil {\n\t\t\twrapped := errors.Wrap(err, \"bad channel direction\")\n\t\t\treturn nil, errors.WithContextf(wrapped, \"encoding channel %v\", t)\n\t\t}\n\t\treturn &v1pb.Type{Kind: v1pb.Type_CHAN, Element: elm, ChanDir: dir}, nil\n\n\tcase reflect.Ptr:\n\t\telm, err := encodeType(t.Elem())\n\t\tif err != nil {\n\t\t\twrapped := errors.Wrap(err, \"bad base type\")\n\t\t\treturn nil, errors.WithContextf(wrapped, \"encoding pointer %v\", t)\n\t\t}\n\t\treturn &v1pb.Type{Kind: v1pb.Type_PTR, Element: elm}, nil\n\n\tcase reflect.Map, reflect.Array:\n\t\treturn nil, errors.Errorf(\"unencodable type '%v', try to wrap the type as a field in a struct, see https://github.com/apache/beam/issues/23101 for details\", t.Kind())\n\n\tdefault:\n\t\treturn nil, errors.Errorf(\"unencodable type '%v'\", t.Kind())\n\t}\n}\n\nfunc tryEncodeSpecial(t reflect.Type) (v1pb.Type_Special, bool) {\n\tswitch t {\n\tcase reflectx.Error:\n\t\treturn v1pb.Type_ERROR, true\n\tcase reflectx.Context:\n\t\treturn v1pb.Type_CONTEXT, true\n\tcase reflectx.Type:\n\t\treturn v1pb.Type_TYPE, true\n\n\tcase typex.EventTimeType:\n\t\treturn v1pb.Type_EVENTTIME, true\n\tcase typex.WindowType:","sourceCodeStart":490,"sourceCodeEnd":526,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/go/pkg/beam/core/runtime/graphx/serialize.go#L490-L526","documentation":"encodeType explicitly rejects reflect.Map and reflect.Array kinds, returning 'unencodable type %v' with a link to beam issue 23101. The graphx protobuf type schema cannot represent raw maps/arrays, so the SDK fails fast at graph-marshalling time rather than producing a corrupt model graph.","triggerScenarios":"Any pipeline where a top-level Map or Array reflect.Type is serialized into the job graph — e.g. a PCollection of map[string]int as the direct coder type, or an exported DoFn field/parameter of map or array type.","commonSituations":"Developers using Go maps as PCollection element types (a very natural pattern) hit this at pipeline construction/submit; also occurs after upgrades when code paths changed to emit map-typed coders.","solutions":["Wrap the map or array as a field in a struct and use the struct as the element type, per issue 23101","Use beam.KV or a custom struct instead of a raw map as the PCollection type","Register a beam.CustomCoder for the map type","Split the map into slices of key/value pairs that are encodable"],"exampleFix":"// before\nfunc emit(wordcount map[string]int) { ... }\n// after\ntype WordCount struct { Counts map[string]int }\nfunc emit(wc WordCount) { ... }","handlingStrategy":"validation","validationCode":"if t.Kind() == reflect.Map || t.Kind() == reflect.Array {\n\treturn fmt.Errorf(\"type %v must be wrapped in a struct before use in a Beam pipeline\", t)\n}","typeGuard":"func mapOrArray(t reflect.Type) bool { return t.Kind() == reflect.Map || t.Kind() == reflect.Array }","tryCatchPattern":"if err != nil {\n\tvar encErr = err\n\tlog.Printf(\"unencodable type: %v\", errors.WithContextf(err, \"in pipeline\"))\n\t// replace element type with a struct wrapper and resubmit\n}","preventionTips":["Use structs instead of maps/arrays as PCollection element types","Prefer beam.KV for key/value pairs","Keep the reference to beam issue 23101 for the recommended workaround","Add a compile-time type check in pipeline construction code"],"tags":["go","serialization","apache-beam"],"backgroundTag":"unsupported-operation","analyzedSha":"12126d8942aaf848030c478b4c6a28c6af861c66","analyzedAt":"2026-09-13T01:50:10.254Z","contentChangedAt":"2026-09-13T01:50:10.254Z","schemaVersion":2},"datasetVersion":"2026-09-20T03:17:13.778Z"}