apache/beam · error · CannotProvideCoderException
Cannot provide coder for elements of Create: For their commo
Error message
Cannot provide coder for elements of Create: For their common class, no coder could be provided. Based on their values, they do not all default to the same Coder.
What it means
Create.of(...) infers a Coder for its elements by calling inferCoderFromObject for each element; if the inferred coders differ between elements, no single coder can encode the PCollection and the pipeline construction fails with CannotProvideCoderException. This happens because Beam cannot guess a consistent encoding when a common class has no registered coder and the element values imply different coders (e.g. mixed generic/Polymorphic values).
Source
Thrown at sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/Create.java:996
// If that fails, try to deduce a coder using the elements themselves
return (Coder<T>) inferCoderFromObjects(coderRegistry, schemaRegistry, elems);
}
/**
* Attempts to infer the {@link Coder} of the elements ensuring that the returned coder is
* equivalent for all elements.
*/
private static Coder<?> inferCoderFromObjects(
CoderRegistry coderRegistry, SchemaRegistry schemaRegistry, Iterable<?> elems)
throws CannotProvideCoderException {
Optional<Coder<?>> coder = Optional.absent();
for (Object elem : elems) {
Coder<?> c = inferCoderFromObject(coderRegistry, schemaRegistry, elem);
if (!coder.isPresent()) {
coder = (Optional) Optional.of(c);
} else if (!Objects.equals(c, coder.get())) {
throw new CannotProvideCoderException(
"Cannot provide coder for elements of "
+ Create.class.getSimpleName()
+ ":"
+ " For their common class, no coder could be provided."
+ " Based on their values, they do not all default to the same Coder.");
}
}
if (coder.isPresent()) {
return coder.get();
}
throw new CannotProvideCoderException(
"Cannot provide coder for elements of "
+ Create.class.getSimpleName()
+ ":"
+ " For their common class, no coder could be provided."
+ " Based on their values, no coder could be inferred.");
}View on GitHub (pinned to 12126d8942)
Solutions
- Supply the coder explicitly: Create.of(elems).withCoder(MyCoder.of())
- Annotate the element class with @DefaultCoder(MyCoder.class) or register a coder via CoderRegistry.registerCoderForClass
- Ensure all elements in Create.of infer the same coder (same concrete type, not raw generics/Object)
- If elements are of an Avro/POJO/Proto type, make sure the schema/coder provider (e.g. AvroCoder via @DefaultSchema) is on the classpath and registered
Example fix
// before PCollection<Object> p = pipeline.apply(Create.of(new Foo(), new Bar())); // after PCollection<Foo> p = pipeline.apply(Create.of(foo1, foo2).withCoder(FooCoder.of()));
Defensive patterns
Strategy: validation
Validate before calling
// Verify every element infers the same coder before Create.of
List<Coder<?>> coders = elems.stream()
.map(e -> pipeline.getCoderRegistry().getCoder(e.getClass()))
.distinct()
.collect(Collectors.toList());
if (coders.size() != 1) {
// supply an explicit coder via .withCoder(...)
} Try / catch
try {
pipeline.apply(Create.of(elems));
} catch (CannotProvideCoderException e) {
pipeline.apply(Create.of(elems).withCoder(MyCoder.of()));
} Prevention
- Annotate custom element classes with @DefaultCoder
- Never use raw generic or Object element types with Create.of
- Register coders for shared domain classes in a common pipeline setup
When it happens
Trigger: Calling Create.of(e1, e2, ...) where inferCoderFromObject returns different Coder instances for different elements, or where no coder can be resolved for the shared runtime class of the elements.
Common situations: Creating a PCollection from heterogeneous values typed only as Object, raw generic types, or classes without a registered default coder (no DefaultCoder annotation, no Coder registered in the CoderRegistry).
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
- Cannot provide coder for elements of Create: For their commo
- Unable to infer a coder for ValueState and no Coder was spec
- Unable to infer a coder for CombiningState and no Coder was
- Unable to infer a coder for CombiningWithContextState and no
- Unable to infer a coder for BagState and no Coder was specif
AI-assisted analysis of apache/beam@12126d8942 (2026-09-13).
Data as JSON: /api/errors/a89e53de760053a0.
Report an issue: GitHub.