apache/beam · error
coder type %v must be identical to node type %v
Error message
coder type %v must be identical to node type %v
What it means
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.
Source
Thrown at sdks/go/pkg/beam/pcollection.go:78
return p.n.Type()
}
// Coder returns the coder for the collection. The Coder is of type 'A'.
func (p PCollection) Coder() Coder {
if !p.IsValid() {
panic("Invalid PCollection")
}
return Coder{p.n.Coder}
}
// SetCoder set the coder for the collection. The Coder must be of type 'A'.
func (p PCollection) SetCoder(c Coder) error {
if !p.IsValid() {
panic("Invalid PCollection")
}
if !typex.IsEqual(p.n.Type(), c.coder.T) {
return errors.Errorf("coder type %v must be identical to node type %v", c.coder.T, p.n)
}
p.n.Coder = c.coder
return nil
}
// WindowingStrategy returns the windowing strategy of the PCollection. It
// describes how elements are assigned to windows and — for transforms that
// honor it — the allowed lateness after which windows are closed.
//
// Transforms that use state and timers keyed by window, such as
// GroupIntoBatches, consult this strategy to compute end-of-window
// event-time timers and to bound partial-batch flushes by the pipeline's
// allowed lateness.
func (p PCollection) WindowingStrategy() *window.WindowingStrategy {
if !p.IsValid() {
panic("Invalid PCollection")
}
return p.n.WindowingStrategy()View on GitHub (pinned to 12126d8942)
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
Example fix
// before
cod := beam.NewCoder(reflect.TypeOf(""))
pcoll.SetCoder(cod) // pcoll is of type MyStruct
// after
cod := beam.NewCoder(reflect.TypeOf(MyStruct{}))
pcoll.SetCoder(cod) Defensive patterns
Strategy: type-guard
Validate before calling
if !typex.IsEqual(pcoll.Type(), coderType) {
return fmt.Errorf("coder type %v does not match pcollection type %v", coderType, pcoll.Type())
} Type guard
func coderMatches(p beam.PCollection, c beam.Coder) bool {
return typex.IsEqual(p.Type(), c.Type())
} Try / catch
if err := pcoll.SetCoder(coder); err != nil {
return fmt.Errorf("setting coder: %w", err)
} Prevention
- 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
When it happens
Trigger: 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.
Common situations: 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.
Understand the failure class
Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.
Related errors
- not a float type: %v
- not a signed integer type: %v
- not a unsigned integer type: %v
- received unknown value type: want []byte, got %T
- GroupByKey requires its input to use KvCoder
AI-assisted analysis of apache/beam@12126d8942 (2026-09-13).
Data as JSON: /api/errors/116442522a2c071c.
Report an issue: GitHub.