apache/beam · error · CannotProvideCoderException
Unable to find 'public static CoderProvider getCoderProvider
Error message
Unable to find 'public static CoderProvider getCoderProvider()' on %s
What it means
Thrown by DefaultCoder.coderFor when the class named in @DefaultCoder does not expose the required static method 'public static CoderProvider getCoderProvider()'. Beam uses reflection to obtain a CoderProvider from the annotated coder class; if the method is missing (NoSuchMethodException) it wraps the failure in CannotProvideCoderException.
Source
Thrown at sdks/java/core/src/main/java/org/apache/beam/sdk/coders/DefaultCoder.java:121
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) {
throw new CannotProvideCoderException(
String.format(
"Unable to find 'public static CoderProvider getCoderProvider()' on %s",
defaultAnnotationValue),
e);
}
CoderProvider coderProvider;
try {
coderProvider =
Preconditions.checkStateNotNull(
(CoderProvider) coderProviderMethod.invoke(clazz /* ignored */));
} catch (IllegalAccessException
| IllegalArgumentException
| InvocationTargetException
| NullPointerException
| ExceptionInInitializerError e) {
throw new CannotProvideCoderException(
String.format(View on GitHub (pinned to 12126d8942)
Solutions
- Add the static factory to the coder class: public static CoderProvider getCoderProvider() { return new FooCoderProvider(); }.
- Use Beam's ProviderHelpers or an existing pattern (e.g. AvroCoder.getCoderProvider()) as a template.
- Verify the method signature exactly matches: public static CoderProvider getCoderProvider(), no arguments.
- As an alternative, register the coder via coderRegistry.registerCoderForClass(Foo.class, new FooCoder()) instead of relying on the annotation's reflective lookup.
Example fix
// before
public class EventCoder extends CustomCoder<Event> { ... }
// after
public class EventCoder extends CustomCoder<Event> {
public static CoderProvider getCoderProvider() {
return CoderProviders.fromStaticMethod(EventCoder.class, "getCoderProvider");
}
} Defensive patterns
Strategy: validation
Validate before calling
try {
MyCoder.class.getMethod("getCoderProvider");
} catch (NoSuchMethodException e) {
throw new IllegalStateException("Coder class must define public static CoderProvider getCoderProvider()");
} Try / catch
try {
coder = registry.getCoder(TypeDescriptor.of(MyType.class));
} catch (CannotProvideCoderException e) {
throw new IllegalStateException("Misconfigured @DefaultCoder target: " + e.getMessage(), e);
} Prevention
- Copy the provider pattern from existing coders like AvroCoder.getCoderProvider().
- Add a unit test invoking MyCoder.getCoderProvider() so breakage is caught early.
- Keep the exact signature: public static CoderProvider getCoderProvider().
When it happens
Trigger: Annotating a class @DefaultCoder(FooCoder.class) where FooCoder defines no public static getCoderProvider() method returning CoderProvider; renaming or removing the method during refactoring.
Common situations: Hand-writing a custom coder and copying only the encode/decode methods without the provider factory; upgrading a coder library where the provider method was removed; typo like getCodersProvider().
Related errors
- Unable to invoke 'public static CoderProvider getCoderProvid
- cannot encode a null Byte
- cannot estimate size for unsupported null value
- NonDeterministicException(target, message, e)
- Unable to encode element '" + value + "' with coder '" + thi
AI-assisted analysis of apache/beam@12126d8942 (2026-09-13).
Data as JSON: /api/errors/b32f99a94aad0576.
Report an issue: GitHub.