{"record":{"id":"9726030a331877c1","repo":"apache/beam","slug":"couldn-t-infer-coder-from","errorCode":null,"errorMessage":"Couldn't infer Coder from ","messagePattern":"Couldn't infer Coder from ","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"sdks/java/io/kafka/src/main/java/org/apache/beam/sdk/io/kafka/KafkaIO.java","lineNumber":1002,"sourceCode":"          }\n        }\n      }\n\n      private static <T> Coder<T> resolveCoder(Class<Deserializer<T>> deserializer) {\n        for (Method method : deserializer.getDeclaredMethods()) {\n          if (method.getName().equals(\"deserialize\")) {\n            Class<?> returnType = method.getReturnType();\n            if (returnType.equals(Object.class)) {\n              continue;\n            }\n            if (returnType.equals(byte[].class)) {\n              return (Coder<T>) NullableCoder.of(ByteArrayCoder.of());\n            } else if (returnType.equals(Integer.class)) {\n              return (Coder<T>) NullableCoder.of(VarIntCoder.of());\n            } else if (returnType.equals(Long.class)) {\n              return (Coder<T>) NullableCoder.of(VarLongCoder.of());\n            } else {\n              throw new RuntimeException(\"Couldn't infer Coder from \" + deserializer);\n            }\n          }\n        }\n        throw new RuntimeException(\"Couldn't resolve coder for Deserializer: \" + deserializer);\n      }\n    }\n\n    /**\n     * Exposes {@link KafkaIO.TypedWithoutMetadata} as an external transform for cross-language\n     * usage.\n     */\n    @AutoService(ExternalTransformRegistrar.class)\n    public static class External implements ExternalTransformRegistrar {\n\n      // Using the transform name in the URN so that the corresponding transform can be easily\n      // identified.\n      public static final String URN_WITH_METADATA =\n          \"beam:transform:org.apache.beam:kafka_read_with_metadata:v1\";","sourceCodeStart":984,"sourceCodeEnd":1020,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/java/io/kafka/src/main/java/org/apache/beam/sdk/io/kafka/KafkaIO.java#L984-L1020","documentation":"KafkaIO.resolveCoder() can only infer a Coder for Deserializer implementations whose decode returns a known boxed type (byte[], Integer, Long, etc.). When the deserializer's return type is unrecognized, it throws a RuntimeException indicating the coder could not be inferred.","triggerScenarios":"Using a custom consumer/producer deserializer class whose return type is not one of the supported types (e.g. String, Double, custom POJO, or an interface) when building via the external config path where the coder must be auto-resolved.","commonSituations":"Custom deserializers returning String or POJOs; third-party deserializers (e.g. Avro/Json) unsupported by the inference branch; specifying key/value deserializer classes without an explicit key/value coder.","solutions":["Specify the coder explicitly via withCoder(), keyCoder()/valueCoder() instead of relying on inference.","Make the deserializer return one of the supported types (byte[], Integer, Long) or wrap it.","Use a known deserializer (ByteArrayDeserializer, Integer/Long deserializers).","Improve the code path by adding your type to resolveCoder's inference chain or registering a CoderProvider."],"exampleFix":"// before\nKafkaIO.<byte[], String>read().withValueDeserializerAndCoder(MyStringDeserializer.class) // implicit coder inference fails\n// after\nKafkaIO.<byte[], String>read()\n  .withValueDeserializerAndCoder(MyStringDeserializer.class, StringUtf8Coder.of());","handlingStrategy":"validation","validationCode":"Class<?> rt = method.getReturnType();\nboolean ok = rt==byte[].class||rt==Integer.class||rt==Long.class;","typeGuard":null,"tryCatchPattern":"try { resolveCoder(d); } catch (RuntimeException e) { useExplicitCoder(); }","preventionTips":["Pass explicit coders with custom deserializers"],"tags":["kafka","coder","serialization","beam-io"],"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"}