{"record":{"id":"7e25bbc9232e6ea8","repo":"apache/beam","slug":"cannot-encode-elements-of-type-s-with-coder-s-because-the","errorCode":null,"errorMessage":"Cannot encode elements of type %s with coder %s because the coded type %s is not assignable from %s","messagePattern":"Cannot encode elements of type (.+?) with coder (.+?) because the coded type (.+?) is not assignable from (.+?)","errorType":"exception","errorClass":"IncompatibleCoderException","httpStatus":null,"severity":"error","filePath":"sdks/java/core/src/main/java/org/apache/beam/sdk/coders/CoderRegistry.java","lineNumber":537,"sourceCode":"    Type codedType = codedDescriptor.getType();\n\n    // Various representations of the candidate type\n    @SuppressWarnings(\"unchecked\")\n    TypeDescriptor<CandidateT> candidateDescriptor =\n        (TypeDescriptor<CandidateT>) TypeDescriptor.of(candidateType);\n    @SuppressWarnings(\"unchecked\")\n    Class<CandidateT> candidateClass = (Class<CandidateT>) candidateDescriptor.getRawType();\n\n    // If coder has type Coder<T> where the actual value of T is lost\n    // to erasure, then we cannot rule it out.\n    if (candidateType instanceof TypeVariable) {\n      return;\n    }\n\n    // If the raw types are not compatible, we can certainly rule out\n    // coder compatibility\n    if (!codedClass.isAssignableFrom(candidateClass)) {\n      throw new IncompatibleCoderException(\n          String.format(\n              \"Cannot encode elements of type %s with coder %s because the\"\n                  + \" coded type %s is not assignable from %s\",\n              candidateType, coder, codedClass, candidateType),\n          coder,\n          candidateType);\n    }\n    // we have established that this is a covariant upcast... though\n    // coders are invariant, we are just checking one direction\n    @SuppressWarnings(\"unchecked\")\n    TypeDescriptor<T> candidateOkDescriptor = (TypeDescriptor<T>) candidateDescriptor;\n\n    // If the coded type is a parameterized type where any of the actual\n    // type parameters are not compatible, then the whole thing is certainly not\n    // compatible.\n    if ((codedType instanceof ParameterizedType) && !isNullOrEmpty(coder.getCoderArguments())) {\n      ParameterizedType parameterizedSupertype =\n          (ParameterizedType) candidateOkDescriptor.getSupertype(codedClass).getType();","sourceCodeStart":519,"sourceCodeEnd":555,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/java/core/src/main/java/org/apache/beam/sdk/coders/CoderRegistry.java#L519-L555","documentation":"verifyCompatible rejects a coder when the raw class it is coded for (codedClass) is not assignable from the candidate type's raw class (candidateClass) — the coder provably cannot encode values of that type. It throws IncompatibleCoderException which callers surface to users. This is the first, cheapest compatibility check before deeper generic-argument analysis.","triggerScenarios":"Any path through CoderRegistry.verifyCompatible — getDefaultCoders or recursive verification — where coder.getEncodedTypeDescriptor().getRawType() is not a supertype of the candidate type's raw class, e.g. verifying Coder<String> against type Integer.","commonSituations":"Passing the wrong Coder for a generic parameter; a custom coder that overrides getEncodedTypeDescriptor incorrectly; upstream code that changed a PCollection's element type but kept the old coder.","solutions":["Use a coder whose encoded type matches (or is a supertype of) the element type, e.g. StringUtf8Coder for String values.","Fix a custom coder's getEncodedTypeDescriptor to return the correct TypeDescriptor.","Register the coder for the correct type with registerCoderForType.","Let the registry infer the coder instead of supplying one explicitly."],"exampleFix":"// before\nCoder<MyBase> coder = ...; // used for MySub that does not extend MyBase\n// after\nclass MySubCoder extends CustomCoder<MySub> { ... } // encoded type MySub\nCoder<MySub> coder = new MySubCoder();","handlingStrategy":"validation","validationCode":"if (!coder.getEncodedTypeDescriptor().getRawType().isAssignableFrom(elementType)) {\n  throw new IllegalArgumentException(\"Coder \" + coder + \" cannot encode \" + elementType);\n}","typeGuard":"static <T> boolean canEncode(Coder<?> coder, Class<T> type) {\n  return coder.getEncodedTypeDescriptor().getRawType().isAssignableFrom(type);\n}","tryCatchPattern":null,"preventionTips":["Match coders to element types explicitly, not by position.","Use well-known coders (StringUtf8Coder, VarIntCoder, etc.) for standard types.","Check getEncodedTypeDescriptor when writing custom coders."],"tags":["java","apache-beam","coders","assignability"],"backgroundTag":"type-mismatch","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"}