apache/beam · error · CannotProvideCoderException
Failed to infer coder for DestinationT from type " +…
Error message
Failed to infer coder for DestinationT from type " + descriptor + ", please provide it explicitly by overriding getDestinationCoder()
What it means
DynamicDestinations.getDestinationCoderWithDefault() wraps CannotProvideCoderException when the CoderRegistry cannot infer a Coder for the DestinationT type parameter from its resolved type descriptor. Beam needs to serialize destinations (e.g. in FileResult coding) and requires an explicit coder when inference fails.
Solutions
- Override getDestinationCoder() in your DynamicDestinations subclass to return an explicit Coder<DestinationT>.
- Register a default coder for the destination type in the CoderRegistry.
- Use a simple destination type with an inferable coder (e.g. String, Integer, or a AvroRecord/POJO with a registered coder).
Example fix
// before
new DynamicDestinations<String, String, String>() { ... } // no getDestinationCoder
// after
new DynamicDestinations<String, String, String>() {
@Override public Coder<String> getDestinationCoder() { return StringUtf8Coder.of(); }
} Defensive patterns
Strategy: validation
Validate before calling
if (dynamicDestinations.getDestinationCoder() == null && registry.getCoder(destinationType) fails) -> provide coder
Try / catch
try { registry.getCoder(descriptor); } catch (CannotProvideCoderException e) { throw new IllegalStateException("Override getDestinationCoder() for type " + descriptor, e); } Prevention
- Always override getDestinationCoder() for non-trivial destination types.
- Prefer simple destination types (String/Integer) when possible.
- Test pipelines with small bounded data before production to surface coder inference failures early.
When it happens
Trigger: Using DynamicDestinations (e.g. TextIO.write().via() or WriteFiles) with a DestinationT type the CoderRegistry cannot infer — non-serializable types, generic/abstract types, or types without a registered default coder — without overriding getDestinationCoder().
Common situations: Custom DynamicDestinations subclasses with complex destination types (records, POJOs without coders, generics erased to type variables), commonly seen when building multi-file output partitions.
Understand the failure class
Background: "missing required argument" and "the following required arguments were not provided": what required-argument errors mean and how to fix them — this error's family across 20 libraries.
Related errors
- ApproximateUnique.PerKey requires its input to use KvCoder
- AvroCoder for GenericRecord requires a schema
- cannot encode a null BitSet
- cannot encode a null byte[]
- cannot encode a null Integer
AI-assisted analysis of apache/beam@12126d8942 (2026-09-13).
Data as JSON: /api/errors/9f8e2a1f33aa8554.
Report an issue: GitHub.
Appendix: source
Thrown at sdks/java/core/src/main/java/org/apache/beam/sdk/io/FileBasedSink.java:353
// Gets the destination coder. If the user does not provide one, try to find one in the coder
// registry. If no coder can be found, throws CannotProvideCoderException.
final Coder<DestinationT> getDestinationCoderWithDefault(CoderRegistry registry)
throws CannotProvideCoderException {
Coder<DestinationT> destinationCoder = getDestinationCoder();
if (destinationCoder != null) {
return destinationCoder;
}
// If dynamicDestinations doesn't provide a coder, try to find it in the coder registry.
@Nullable TypeDescriptor<DestinationT> descriptor =
extractFromTypeParameters(
this,
DynamicDestinations.class,
new TypeVariableExtractor<
DynamicDestinations<UserT, DestinationT, OutputT>, DestinationT>() {});
try {
return registry.getCoder(descriptor);
} catch (CannotProvideCoderException e) {
throw new CannotProvideCoderException(
"Failed to infer coder for DestinationT from type "
+ descriptor
+ ", please provide it explicitly by overriding getDestinationCoder()",
e);
}
}
}
/** A naming policy for output files. */
public abstract static class FilenamePolicy implements Serializable {
/**
* When a sink has requested windowed or triggered output, this method will be invoked to return
* the file {@link ResourceId resource} to be created given the base output directory and a
* {@link OutputFileHints} containing information about the file, including a suggested
* extension (e.g. coming from {@link Compression}).
*
* <p>The policy must return unique and consistent filenames for different windows and panes.
*/View on GitHub (pinned to 12126d8942)