{"record":{"id":"a89e53de760053a0","repo":"apache/beam","slug":"cannot-provide-coder-for-elements-of-create-for-their-common","errorCode":null,"errorMessage":"Cannot provide coder for elements of Create: For their common class, no coder could be provided. Based on their values, they do not all default to the same Coder.","messagePattern":"Cannot provide coder for elements of Create: For their common class, no coder could be provided\\. Based on their values, they do not all default to the same Coder\\.","errorType":"exception","errorClass":"CannotProvideCoderException","httpStatus":null,"severity":"error","filePath":"sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/Create.java","lineNumber":996,"sourceCode":"\n    // If that fails, try to deduce a coder using the elements themselves\n    return (Coder<T>) inferCoderFromObjects(coderRegistry, schemaRegistry, elems);\n  }\n\n  /**\n   * Attempts to infer the {@link Coder} of the elements ensuring that the returned coder is\n   * equivalent for all elements.\n   */\n  private static Coder<?> inferCoderFromObjects(\n      CoderRegistry coderRegistry, SchemaRegistry schemaRegistry, Iterable<?> elems)\n      throws CannotProvideCoderException {\n    Optional<Coder<?>> coder = Optional.absent();\n    for (Object elem : elems) {\n      Coder<?> c = inferCoderFromObject(coderRegistry, schemaRegistry, elem);\n      if (!coder.isPresent()) {\n        coder = (Optional) Optional.of(c);\n      } else if (!Objects.equals(c, coder.get())) {\n        throw new CannotProvideCoderException(\n            \"Cannot provide coder for elements of \"\n                + Create.class.getSimpleName()\n                + \":\"\n                + \" For their common class, no coder could be provided.\"\n                + \" Based on their values, they do not all default to the same Coder.\");\n      }\n    }\n    if (coder.isPresent()) {\n      return coder.get();\n    }\n\n    throw new CannotProvideCoderException(\n        \"Cannot provide coder for elements of \"\n            + Create.class.getSimpleName()\n            + \":\"\n            + \" For their common class, no coder could be provided.\"\n            + \" Based on their values, no coder could be inferred.\");\n  }","sourceCodeStart":978,"sourceCodeEnd":1014,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/Create.java#L978-L1014","documentation":"Create.of(...) infers a Coder for its elements by calling inferCoderFromObject for each element; if the inferred coders differ between elements, no single coder can encode the PCollection and the pipeline construction fails with CannotProvideCoderException. This happens because Beam cannot guess a consistent encoding when a common class has no registered coder and the element values imply different coders (e.g. mixed generic/Polymorphic values).","triggerScenarios":"Calling Create.of(e1, e2, ...) where inferCoderFromObject returns different Coder instances for different elements, or where no coder can be resolved for the shared runtime class of the elements.","commonSituations":"Creating a PCollection from heterogeneous values typed only as Object, raw generic types, or classes without a registered default coder (no DefaultCoder annotation, no Coder registered in the CoderRegistry).","solutions":["Supply the coder explicitly: Create.of(elems).withCoder(MyCoder.of())","Annotate the element class with @DefaultCoder(MyCoder.class) or register a coder via CoderRegistry.registerCoderForClass","Ensure all elements in Create.of infer the same coder (same concrete type, not raw generics/Object)","If elements are of an Avro/POJO/Proto type, make sure the schema/coder provider (e.g. AvroCoder via @DefaultSchema) is on the classpath and registered"],"exampleFix":"// before\nPCollection<Object> p = pipeline.apply(Create.of(new Foo(), new Bar()));\n// after\nPCollection<Foo> p = pipeline.apply(Create.of(foo1, foo2).withCoder(FooCoder.of()));","handlingStrategy":"validation","validationCode":"// Verify every element infers the same coder before Create.of\nList<Coder<?>> coders = elems.stream()\n    .map(e -> pipeline.getCoderRegistry().getCoder(e.getClass()))\n    .distinct()\n    .collect(Collectors.toList());\nif (coders.size() != 1) {\n  // supply an explicit coder via .withCoder(...)\n}","typeGuard":null,"tryCatchPattern":"try {\n  pipeline.apply(Create.of(elems));\n} catch (CannotProvideCoderException e) {\n  pipeline.apply(Create.of(elems).withCoder(MyCoder.of()));\n}","preventionTips":["Annotate custom element classes with @DefaultCoder","Never use raw generic or Object element types with Create.of","Register coders for shared domain classes in a common pipeline setup"],"tags":["apache-beam","java","coder-inference","pipeline-construction"],"backgroundTag":"type-mismatch","analyzedSha":"12126d8942aaf848030c478b4c6a28c6af861c66","analyzedAt":"2026-09-13T01:50:10.254Z","contentChangedAt":"2026-09-13T01:50:10.254Z","schemaVersion":2},"datasetVersion":"2026-09-14T16:17:12.679Z"}