apache/beam · error · CannotProvideCoderException

Cannot provide coder for Create: The elements are not all…

Error message

Cannot provide coder for Create: The elements are not all of the same class.

What it means

Create's default coder inference walks the supplied elements and requires them to share a single runtime class; the first element fixes elementClazz (or Void is replaced), and any element of a different class causes CannotProvideCoderException. Beam throws this because a single coder cannot be inferred for heterogeneous element lists.

Solutions

  1. Make all elements the same runtime class, or declare the list as List<BaseType> and pass .withCoder(coderForBaseType).
  2. Provide an explicit coder via .withCoder(...) so inference is skipped entirely.
  3. Normalize elements to a common type before Create.of (map subclasses to a common supertype with a registered coder).
  4. For heterogeneous unions, define a schema/Avro union type and use withSchema/AvroCoder.

Example fix

// before
p.apply(Create.of(1, "two")); // mixed classes
// after
List<Object> vals = Arrays.asList(1, "two");
p.apply(Create.of(vals).withCoder(SerializableCoder.of(Object.class)));
Defensive patterns

Strategy: validation

Validate before calling

Set<Class<?>> classes = elems.stream().map(Object::getClass).collect(Collectors.toSet()); if (classes.size() > 1) { /* supply .withCoder for a common supertype */ }

Type guard

boolean homogeneousClasses(List<?> l) { return l.stream().map(Object::getClass).distinct().count() <= 1; }

Try / catch

try { p.apply(Create.of(elems)); } catch (CannotProvideCoderException e) { if (e.getMessage().contains("not all of the same class")) { /* add withCoder or homogenize elements */ } throw e; }

Prevention

When it happens

Trigger: Create.of(1, "two", 3L) or Create.of(subclassA, subclassB) where instances are different concrete classes; mixing null with elements where the first check yields Void then a mismatch occurs.

Common situations: Prototyping with mixed literal lists; lists declared as List<Object>; subclass hierarchies where elements are different concrete implementations of a common base/interface.

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/fbf66fbfd2572662. Report an issue: GitHub.

Appendix: source

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

      throws CannotProvideCoderException {
    checkArgument(
        !Iterables.isEmpty(elems),
        "Can not determine a default Coder for a 'Create' PTransform that "
            + "has no elements.  Either add elements, call Create.empty(Coder),"
            + " Create.empty(TypeDescriptor), or call 'withCoder(Coder)' or "
            + "'withType(TypeDescriptor)' on the PTransform.");
    // First try to deduce a coder using the types of the elements.
    Class<?> elementClazz = Void.class;
    for (T elem : elems) {
      if (elem == null) {
        continue;
      }
      Class<?> clazz = elem.getClass();
      if (elementClazz.equals(Void.class)) {
        elementClazz = clazz;
      } else if (!elementClazz.equals(clazz)) {
        // Elements are not the same type, require a user-specified coder.
        throw new CannotProvideCoderException(
            String.format(
                "Cannot provide coder for %s: The elements are not all of the same class.",
                Create.class.getSimpleName()));
      }
    }

    TypeDescriptor<T> typeDescriptor = (TypeDescriptor<T>) TypeDescriptor.of(elementClazz);
    if (elementClazz.getTypeParameters().length == 0) {
      try {
        Coder<T> coder =
            SchemaCoder.of(
                schemaRegistry.getSchema(typeDescriptor),
                typeDescriptor,
                schemaRegistry.getToRowFunction(typeDescriptor),
                schemaRegistry.getFromRowFunction(typeDescriptor));
        return coder;
      } catch (NoSuchSchemaException e) {
        // No schema.

View on GitHub (pinned to 12126d8942)