{"record":{"id":"9f8e2a1f33aa8554","repo":"apache/beam","slug":"failed-to-infer-coder-for-destinationt-from-type-descriptor","errorCode":null,"errorMessage":"Failed to infer coder for DestinationT from type \" + descriptor + \", please provide it explicitly by overriding getDestinationCoder()","messagePattern":"Failed to infer coder for DestinationT from type \" \\+ descriptor \\+ \", please provide it explicitly by overriding getDestinationCoder\\(\\)","errorType":"exception","errorClass":"CannotProvideCoderException","httpStatus":null,"severity":"error","filePath":"sdks/java/core/src/main/java/org/apache/beam/sdk/io/FileBasedSink.java","lineNumber":353,"sourceCode":"    // Gets the destination coder. If the user does not provide one, try to find one in the coder\n    // registry. If no coder can be found, throws CannotProvideCoderException.\n    final Coder<DestinationT> getDestinationCoderWithDefault(CoderRegistry registry)\n        throws CannotProvideCoderException {\n      Coder<DestinationT> destinationCoder = getDestinationCoder();\n      if (destinationCoder != null) {\n        return destinationCoder;\n      }\n      // If dynamicDestinations doesn't provide a coder, try to find it in the coder registry.\n      @Nullable TypeDescriptor<DestinationT> descriptor =\n          extractFromTypeParameters(\n              this,\n              DynamicDestinations.class,\n              new TypeVariableExtractor<\n                  DynamicDestinations<UserT, DestinationT, OutputT>, DestinationT>() {});\n      try {\n        return registry.getCoder(descriptor);\n      } catch (CannotProvideCoderException e) {\n        throw new CannotProvideCoderException(\n            \"Failed to infer coder for DestinationT from type \"\n                + descriptor\n                + \", please provide it explicitly by overriding getDestinationCoder()\",\n            e);\n      }\n    }\n  }\n\n  /** A naming policy for output files. */\n  public abstract static class FilenamePolicy implements Serializable {\n    /**\n     * When a sink has requested windowed or triggered output, this method will be invoked to return\n     * the file {@link ResourceId resource} to be created given the base output directory and a\n     * {@link OutputFileHints} containing information about the file, including a suggested\n     * extension (e.g. coming from {@link Compression}).\n     *\n     * <p>The policy must return unique and consistent filenames for different windows and panes.\n     */","sourceCodeStart":335,"sourceCodeEnd":371,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/java/core/src/main/java/org/apache/beam/sdk/io/FileBasedSink.java#L335-L371","documentation":"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.","triggerScenarios":"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().","commonSituations":"Custom DynamicDestinations subclasses with complex destination types (records, POJOs without coders, generics erased to type variables), commonly seen when building multi-file output partitions.","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)."],"exampleFix":"// before\nnew DynamicDestinations<String, String, String>() { ... } // no getDestinationCoder\n// after\nnew DynamicDestinations<String, String, String>() {\n  @Override public Coder<String> getDestinationCoder() { return StringUtf8Coder.of(); }\n}","handlingStrategy":"validation","validationCode":"if (dynamicDestinations.getDestinationCoder() == null && registry.getCoder(destinationType) fails) -> provide coder","typeGuard":null,"tryCatchPattern":"try { registry.getCoder(descriptor); } catch (CannotProvideCoderException e) { throw new IllegalStateException(\"Override getDestinationCoder() for type \" + descriptor, e); }","preventionTips":["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."],"tags":["java","apache-beam","coder","type-inference"],"backgroundTag":"missing-required-argument","analyzedSha":"12126d8942aaf848030c478b4c6a28c6af861c66","analyzedAt":"2026-09-13T01:50:10.254Z","contentChangedAt":"2026-09-13T01:50:10.254Z","schemaVersion":2},"datasetVersion":"2026-09-20T03:17:13.778Z"}