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, no coder could be inferred.

What it means

Create.of(...) failed to infer any Coder at all: every call to inferCoderFromObject returned a coder but none was kept, or no coder could be inferred for the element values, so after the loop coder is empty and CannotProvideCoderException is thrown. Beam requires a deterministic way to serialize PCollection elements and refuses to guess.

Source

Thrown at sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/Create.java:1008

    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.");
  }

  /**
   * Attempt to infer the type for some very common Apache Beam parameterized types.
   *
   * <p>TODO: Instead, build a TypeDescriptor so that the {@link CoderRegistry} is invoked for the
   * type instead of hard coding the coders for common types.
   */
  private static Coder<?> inferCoderFromObject(
      CoderRegistry coderRegistry, SchemaRegistry schemaRegistry, Object o)
      throws CannotProvideCoderException {

    if (o == null) {

View on GitHub (pinned to 12126d8942)

Solutions

  1. Provide the coder explicitly with .withCoder(...) on the Create transform
  2. Annotate the element class with @DefaultCoder(AvroCoder.class) (or your coder)
  3. Register the coder: pipeline.getCoderRegistry().registerCoderForClass(MyType.class, MyTypeCoder.of())
  4. Verify the element type is one Beam can infer (primitive, Serializable, Pojo/Row/Proto with schema providers enabled)

Example fix

// before
pipeline.apply(Create.of(new MyRecord()))
// after
pipeline.apply(Create.of(new MyRecord()).withCoder(AvroCoder.of(MyRecord.class)))
Defensive patterns

Strategy: validation

Validate before calling

try {
  pipeline.getCoderRegistry().getCoder(MyRecord.class);
} catch (CannotProvideCoderException e) {
  pipeline.getCoderRegistry().registerCoderForClass(MyRecord.class, AvroCoder.of(MyRecord.class));
}

Try / catch

try {
  pipeline.apply(Create.of(elems));
} catch (CannotProvideCoderException e) {
  pipeline.apply(Create.of(elems).withCoder(AvroCoder.of(MyRecord.class)));
}

Prevention

When it happens

Trigger: Calling Create.of(single element or all elements) where inferCoderFromObject throws/cannot resolve a coder for the element's class — e.g. a plain class with no DefaultCoder, no coder registered in CoderRegistry, and not a known simple/Serializable type.

Common situations: Custom domain classes used with Create.of without @DefaultCoder; classes added in a refactor that lost their coder registration; running under a test harness where CoderRegistry customizations were not applied.

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


AI-assisted analysis of apache/beam@12126d8942 (2026-09-13). Data as JSON: /api/errors/2cbb548d72ef02d5. Report an issue: GitHub.