apache/beam · error · CannotProvideCoderException

Class %s does not have a @DefaultCoder annotation.

Error message

Class %s does not have a @DefaultCoder annotation.

What it means

Thrown by DefaultCoder.coderFor when it cannot find a @DefaultCoder annotation on the class (including its AutoValue superclass after walking up one level). The Beam coder registry needs @DefaultCoder to know which CoderProvider supplies a default coder for the type. Without the annotation, Beam refuses to guess a coder and raises CannotProvideCoderException.

Source

Thrown at sdks/java/core/src/main/java/org/apache/beam/sdk/coders/DefaultCoder.java:102

       */
      @SuppressFBWarnings("DCN_NULLPOINTER_EXCEPTION") // TODO(#35312)
      @Override
      public <T> Coder<T> coderFor(
          TypeDescriptor<T> typeDescriptor, List<? extends Coder<?>> componentCoders)
          throws CannotProvideCoderException {

        Class<?> clazz = typeDescriptor.getRawType();
        DefaultCoder defaultAnnotation = clazz.getAnnotation(DefaultCoder.class);
        if (defaultAnnotation == null) {
          // check if the superclass has DefaultCoder annotation if the class is generated using
          // AutoValue
          if (clazz.getName().contains("AutoValue_")) {
            clazz = clazz.getSuperclass();
            defaultAnnotation = clazz.getAnnotation(DefaultCoder.class);
          }
        }
        if (defaultAnnotation == null) {
          throw new CannotProvideCoderException(
              String.format("Class %s does not have a @DefaultCoder annotation.", clazz.getName()));
        }

        @SuppressWarnings("rawtypes") // this is deliberate, as the user will pass FooCoder.class
        Class<? extends Coder> defaultAnnotationValue = defaultAnnotation.value();
        if (defaultAnnotationValue == null) {
          throw new CannotProvideCoderException(
              String.format(
                  "Class %s has a @DefaultCoder annotation with a null value.", clazz.getName()));
        }

        LOG.debug(
            "DefaultCoder annotation found for {} with value {}", clazz, defaultAnnotationValue);

        Method coderProviderMethod;
        try {
          coderProviderMethod = defaultAnnotationValue.getMethod("getCoderProvider");
        } catch (NoSuchMethodException e) {

View on GitHub (pinned to 12126d8942)

Solutions

  1. Annotate the class with @DefaultCoder(YourCoder.class) (on the AutoValue abstract class for AutoValue types).
  2. Alternatively register a coder explicitly: coderRegistry.registerCoderForType(TypeDescriptor.of(Foo.class), new FooCoderProvider()).
  3. Use a built-in codable type or implement/verify the class works with SerializableCoder via @DefaultCoder(AvronCoder.class) as appropriate.
  4. Verify the annotation is on the right class when using AutoValue (the annotation lives on the abstract @AutoValue class, not the generated AutoValue_ subclass).

Example fix

// before
@AutoValue
public abstract class Event { ... }

// after
@DefaultCoder(AvroCoder.class)
@AutoValue
public abstract class Event { ... }
Defensive patterns

Strategy: validation

Validate before calling

if (MyType.class.getAnnotation(org.apache.beam.sdk.coders.DefaultCoder.class) == null) {
  throw new IllegalStateException("Add @DefaultCoder(CoderProviderClass.class) to " + MyType.class.getName());
}

Try / catch

try {
  Coder<MyType> c = registry.getCoder(TypeDescriptor.of(MyType.class));
} catch (CannotProvideCoderException e) {
  // fall back to explicitly registered coder
  c = new MyTypeCoder();
}

Prevention

When it happens

Trigger: Calling CoderRegistry.getCoder(TypeDescriptor<T>) (or a pipeline needing a coder for T) where T's class has no @DefaultCoder annotation and is not registered otherwise; also when an AutoValue_* class's superclass lacks the annotation.

Common situations: Using a custom POJO or AutoValue class as a PCollection element without annotating it @DefaultCoder(FooCoder.class); forgetting that @DefaultCoder must sit on the generated class's superclass for AutoValue types; upgrading Beam and relying on removed implicit coders.

Related errors


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