apache/beam · error · CannotProvideCoderException
PTransform.getOutputCoder called.
Error message
PTransform.getOutputCoder called.
What it means
PTransform.getDefaultOutputCoder() is a deprecated hook that was meant to be overridden to supply an output coder. The base implementation throws CannotProvideCoderException because the generic PTransform cannot infer a coder. If your transform neither overrides this nor sets a coder on its output PCollection, pipeline construction fails.
Solutions
- In expand(), explicitly set a coder: PCollection<O> out = input.apply(...); out.setCoder(MyCoder.of()); return out;
- Override getDefaultOutputCoder(input) (the modern one-argument variant) to return a Coder for your output
- Ensure the output type is a concrete, coder-inferrable class (avoid raw/generic types)
Example fix
// before
return input.apply("t", new MyTransform());
// after
PCollection<O> out = input.apply("t", new MyTransform());
out.setCoder(MyCoder.of());
return out; Defensive patterns
Strategy: validation
Validate before calling
// Verify a coder is available before expanding
Coder<?> c = null;
try {
java.lang.reflect.Method m = PTransform.class.getDeclaredMethod("getDefaultOutputCoder", PCollection.class);
m.setAccessible(true);
c = (Coder<?>) m.invoke(transform, input);
} catch (CannotProvideCoderException | ReflectiveOperationException ignored) { }
if (c == null) { /* set coder explicitly on output */ } Try / catch
try {
PCollection<O> out = input.apply(transform);
} catch (CannotProvideCoderException | IllegalStateException e) {
PCollection<O> out = input.apply(transform).setCoder(MyCoder.of());
} Prevention
- Always call setCoder() on output PCollections in custom transforms
- Avoid raw/generic output types that defeat coder inference
- Migrate away from the deprecated getDefaultOutputCoder() override
When it happens
Trigger: Applying a custom PTransform that returns a PCollection without ever calling output.setCoder(...) and without overriding getDefaultOutputCoder, so Beam falls back to the deprecated base method.
Common situations: Custom transforms producing PCollection<Row>/POJOs where type erasure prevents inference; older tutorials still using the deprecated override; transforms whose output type is a raw or generic type.
Understand the failure class
Background: Schema validation failed / invalid input schema: payload rejected because its shape doesn't match the expected schema — this error's family across 28 libraries.
Related errors
- ApproximateUnique.PerKey requires its input to use KvCoder
- AvroCoder for GenericRecord requires a schema
- cannot encode a null BitSet
- cannot encode a null byte[]
- cannot encode a null Integer
AI-assisted analysis of apache/beam@12126d8942 (2026-09-13).
Data as JSON: /api/errors/594b98ef34e412bf.
Report an issue: GitHub.
Appendix: source
Thrown at sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/PTransform.java:331
private void readObject(ObjectInputStream oos) {
// We don't really want to be serializing this object, but we
// often have serializable anonymous DoFns nested within a
// PTransform.
}
/**
* Returns the default {@code Coder} to use for the output of this single-output {@code
* PTransform}.
*
* <p>By default, always throws
*
* @throws CannotProvideCoderException if no coder can be inferred
* @deprecated Instead, the PTransform should explicitly call {@link PCollection#setCoder} on the
* returned PCollection.
*/
@Deprecated
protected Coder<?> getDefaultOutputCoder() throws CannotProvideCoderException {
throw new CannotProvideCoderException("PTransform.getOutputCoder called.");
}
/**
* Returns the default {@code Coder} to use for the output of this single-output {@code
* PTransform} when applied to the given input.
*
* <p>By default, always throws.
*
* @throws CannotProvideCoderException if none can be inferred.
* @deprecated Instead, the PTransform should explicitly call {@link PCollection#setCoder} on the
* returned PCollection.
*/
@Deprecated
protected Coder<?> getDefaultOutputCoder(@SuppressWarnings("unused") InputT input)
throws CannotProvideCoderException {
return getDefaultOutputCoder();
}
View on GitHub (pinned to 12126d8942)