{"record":{"id":"fbf66fbfd2572662","repo":"apache/beam","slug":"cannot-provide-coder-for-create-the-elements-are-not-all-of","errorCode":null,"errorMessage":"Cannot provide coder for Create: The elements are not all of the same class.","messagePattern":"Cannot provide coder for Create: The elements are not all of the same class\\.","errorType":"exception","errorClass":"CannotProvideCoderException","httpStatus":null,"severity":"error","filePath":"sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/Create.java","lineNumber":949,"sourceCode":"      throws CannotProvideCoderException {\n    checkArgument(\n        !Iterables.isEmpty(elems),\n        \"Can not determine a default Coder for a 'Create' PTransform that \"\n            + \"has no elements.  Either add elements, call Create.empty(Coder),\"\n            + \" Create.empty(TypeDescriptor), or call 'withCoder(Coder)' or \"\n            + \"'withType(TypeDescriptor)' on the PTransform.\");\n    // First try to deduce a coder using the types of the elements.\n    Class<?> elementClazz = Void.class;\n    for (T elem : elems) {\n      if (elem == null) {\n        continue;\n      }\n      Class<?> clazz = elem.getClass();\n      if (elementClazz.equals(Void.class)) {\n        elementClazz = clazz;\n      } else if (!elementClazz.equals(clazz)) {\n        // Elements are not the same type, require a user-specified coder.\n        throw new CannotProvideCoderException(\n            String.format(\n                \"Cannot provide coder for %s: The elements are not all of the same class.\",\n                Create.class.getSimpleName()));\n      }\n    }\n\n    TypeDescriptor<T> typeDescriptor = (TypeDescriptor<T>) TypeDescriptor.of(elementClazz);\n    if (elementClazz.getTypeParameters().length == 0) {\n      try {\n        Coder<T> coder =\n            SchemaCoder.of(\n                schemaRegistry.getSchema(typeDescriptor),\n                typeDescriptor,\n                schemaRegistry.getToRowFunction(typeDescriptor),\n                schemaRegistry.getFromRowFunction(typeDescriptor));\n        return coder;\n      } catch (NoSuchSchemaException e) {\n        // No schema.","sourceCodeStart":931,"sourceCodeEnd":967,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/Create.java#L931-L967","documentation":"Create's default coder inference walks the supplied elements and requires them to share a single runtime class; the first element fixes elementClazz (or Void is replaced), and any element of a different class causes CannotProvideCoderException. Beam throws this because a single coder cannot be inferred for heterogeneous element lists.","triggerScenarios":"Create.of(1, \"two\", 3L) or Create.of(subclassA, subclassB) where instances are different concrete classes; mixing null with elements where the first check yields Void then a mismatch occurs.","commonSituations":"Prototyping with mixed literal lists; lists declared as List<Object>; subclass hierarchies where elements are different concrete implementations of a common base/interface.","solutions":["Make all elements the same runtime class, or declare the list as List<BaseType> and pass .withCoder(coderForBaseType).","Provide an explicit coder via .withCoder(...) so inference is skipped entirely.","Normalize elements to a common type before Create.of (map subclasses to a common supertype with a registered coder).","For heterogeneous unions, define a schema/Avro union type and use withSchema/AvroCoder."],"exampleFix":"// before\np.apply(Create.of(1, \"two\")); // mixed classes\n// after\nList<Object> vals = Arrays.asList(1, \"two\");\np.apply(Create.of(vals).withCoder(SerializableCoder.of(Object.class)));","handlingStrategy":"validation","validationCode":"Set<Class<?>> classes = elems.stream().map(Object::getClass).collect(Collectors.toSet()); if (classes.size() > 1) { /* supply .withCoder for a common supertype */ }","typeGuard":"boolean homogeneousClasses(List<?> l) { return l.stream().map(Object::getClass).distinct().count() <= 1; }","tryCatchPattern":"try { p.apply(Create.of(elems)); } catch (CannotProvideCoderException e) { if (e.getMessage().contains(\"not all of the same class\")) { /* add withCoder or homogenize elements */ } throw e; }","preventionTips":["Keep Create.of element lists type-homogeneous","Declare collections as List<CommonBase> with a registered base coder","Prefer explicit .withCoder whenever mixing subtypes","Catch CannotProvideCoderException during pipeline construction in tests"],"tags":["java","apache-beam","create","coder-inference","heterogeneous-elements"],"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"}