{"record":{"id":"116442522a2c071c","repo":"apache/beam","slug":"coder-type-v-must-be-identical-to-node-type-v","errorCode":null,"errorMessage":"coder type %v must be identical to node type %v","messagePattern":"coder type (.+?) must be identical to node type (.+?)","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"sdks/go/pkg/beam/pcollection.go","lineNumber":78,"sourceCode":"\treturn p.n.Type()\n}\n\n// Coder returns the coder for the collection. The Coder is of type 'A'.\nfunc (p PCollection) Coder() Coder {\n\tif !p.IsValid() {\n\t\tpanic(\"Invalid PCollection\")\n\t}\n\treturn Coder{p.n.Coder}\n}\n\n// SetCoder set the coder for the collection. The Coder must be of type 'A'.\nfunc (p PCollection) SetCoder(c Coder) error {\n\tif !p.IsValid() {\n\t\tpanic(\"Invalid PCollection\")\n\t}\n\n\tif !typex.IsEqual(p.n.Type(), c.coder.T) {\n\t\treturn errors.Errorf(\"coder type %v must be identical to node type %v\", c.coder.T, p.n)\n\t}\n\tp.n.Coder = c.coder\n\treturn nil\n}\n\n// WindowingStrategy returns the windowing strategy of the PCollection. It\n// describes how elements are assigned to windows and — for transforms that\n// honor it — the allowed lateness after which windows are closed.\n//\n// Transforms that use state and timers keyed by window, such as\n// GroupIntoBatches, consult this strategy to compute end-of-window\n// event-time timers and to bound partial-batch flushes by the pipeline's\n// allowed lateness.\nfunc (p PCollection) WindowingStrategy() *window.WindowingStrategy {\n\tif !p.IsValid() {\n\t\tpanic(\"Invalid PCollection\")\n\t}\n\treturn p.n.WindowingStrategy()","sourceCodeStart":60,"sourceCodeEnd":96,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/go/pkg/beam/pcollection.go#L60-L96","documentation":"PCollection.SetCoder validates that the coder's type exactly matches the PCollection's element type (typex.IsEqual) before assigning it. If they differ, the SDK returns this error rather than silently attaching a coder that would mis-encode elements. In Beam Go, coders are type-specific, so an 'identical' (not merely assignable) type match is required.","triggerScenarios":"Calling p.SetCoder(c) where c was created for a different element type than p's, e.g. attaching a coder for typex.WindowedValue or a different concrete type. Called indirectly by TryCombinePerKey, TryExternal, TryFlatten, TryCoGroupByKey, TryReshuffle, and ImpulseValue when coder/node types diverge.","commonSituations":"Reusing a coder defined for []byte with a PCollection of string, applying a custom coder registered for type A to a PCollection of type B after a refactor changed an element type, or setting a coder on a windowed/value-mismatched PCollection.","solutions":["Create the coder for the exact element type of the PCollection (check p.Type() before building the coder)","Use beam.NewCoder for the correct element type rather than reusing an existing coder","Update your custom coder registration to match the current element type after refactors","If types legitimately differ, insert a map/ParDo transform to convert elements to the coder's type first"],"exampleFix":"// before\ncod := beam.NewCoder(reflect.TypeOf(\"\"))\npcoll.SetCoder(cod) // pcoll is of type MyStruct\n// after\ncod := beam.NewCoder(reflect.TypeOf(MyStruct{}))\npcoll.SetCoder(cod)","handlingStrategy":"type-guard","validationCode":"if !typex.IsEqual(pcoll.Type(), coderType) {\n    return fmt.Errorf(\"coder type %v does not match pcollection type %v\", coderType, pcoll.Type())\n}","typeGuard":"func coderMatches(p beam.PCollection, c beam.Coder) bool {\n    return typex.IsEqual(p.Type(), c.Type())\n}","tryCatchPattern":"if err := pcoll.SetCoder(coder); err != nil {\n    return fmt.Errorf(\"setting coder: %w\", err)\n}","preventionTips":["Derive coders from the PCollection's own type (beam.NewCoder(reflect.TypeOf(element)))","Prefer letting the SDK infer coders instead of setting them manually","Re-check explicit coder calls after element-type refactors","Convert elements with a map step if types legitimately differ"],"tags":["go","apache-beam","coder","type-mismatch"],"backgroundTag":"type-mismatch","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"}