{"record":{"id":"8153a7fef2e41f13","repo":"apache/beam","slug":"unable-to-apply-create-s-using-coder-s","errorCode":null,"errorMessage":"Unable to apply Create %s using Coder %s.","messagePattern":"Unable to apply Create (.+?) using Coder (.+?)\\.","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/Create.java","lineNumber":442,"sourceCode":"                .apply(\n                    MapElements.via(\n                        new SimpleFunction<byte[], T>() {\n                          @Override\n                          public T apply(byte[] input) {\n                            try {\n                              return CoderUtils.decodeFromByteArray(capturedCoder, encodedElement);\n                            } catch (CoderException exn) {\n                              throw new RuntimeException(exn);\n                            }\n                          }\n                        }))\n                .setCoder(coder);\n          }\n        }\n        CreateSource<T> source = CreateSource.fromIterable(elems, coder);\n        return input.getPipeline().apply(Read.from(source));\n      } catch (IOException e) {\n        throw new RuntimeException(\n            String.format(\"Unable to apply Create %s using Coder %s.\", this, coder), e);\n      }\n    }\n\n    /////////////////////////////////////////////////////////////////////////////\n\n    /** The elements of the resulting PCollection. */\n    private final transient Iterable<T> elems;\n\n    /** The coder used to encode the values to and from a binary representation. */\n    private final transient Optional<Coder<T>> coder;\n\n    /** The value type. */\n    private final transient Optional<TypeDescriptor<T>> typeDescriptor;\n\n    /** Whether to unconditionally implement this via reading a CreateSource. */\n    private final transient boolean alwaysUseRead;\n","sourceCodeStart":424,"sourceCodeEnd":460,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/Create.java#L424-L460","documentation":"After choosing a coder, Create.expand() serializes its elements into a CreateSource via CreateSource.fromIterable; if that serialization throws IOException (the coder failed to encode the elements), Beam wraps it in this RuntimeException. The coder was accepted by inference but is incompatible with the actual element values at encode time.","triggerScenarios":"Create.of(elems).withCoder(c) where c cannot actually encode the runtime elements — e.g. a coder expecting a different type than supplied, AvroCoder misconfigured with wrong schema class, or a coder that fails on null values in the list.","commonSituations":"Passing a coder typed for a subclass/superclass mismatch; AvroCoder with wrong schema; custom coder throwing on encode (e.g. non-serializable fields in SerializableCoder).","solutions":["Verify the coder's type parameter matches the element type passed to Create.of (fix generics / explicit type witness).","If using AvroCoder, confirm the schema/class matches the elements.","Check elements for nulls or non-serializable fields the chosen coder cannot handle; use a coder that supports them (e.g. NullableCoder, or fix the POJO).","Run the coder standalone: Coder.encode on one element in a unit test to reproduce and debug the IOException."],"exampleFix":"// before\np.apply(Create.of(listOfDogs).withCoder(AvroCoder.of(Cat.class))); // encode fails\n// after\np.apply(Create.of(listOfDogs).withCoder(AvroCoder.of(Dog.class)));","handlingStrategy":"try-catch","validationCode":"try { coder.encode(sampleElem, new ByteArrayOutputStream(), Coder.Context.OUTER); } catch (Exception e) { /* coder incompatible with elements */ }","typeGuard":"boolean coderCanEncode(Coder<T> c, T sample) { try { c.encode(sample, new ByteArrayOutputStream(), Coder.Context.OUTER); return true; } catch (Exception e) { return false; } }","tryCatchPattern":"try { p.apply(Create.of(elems).withCoder(coder)); } catch (RuntimeException e) { if (e.getMessage() != null && e.getMessage().startsWith(\"Unable to apply Create\")) { /* verify coder type matches elements */ } throw e; }","preventionTips":["Smoke-test coders by encoding one representative element in a unit test","Ensure coder generics exactly match the element type","Check Avro schema/class alignment when using AvroCoder","Handle/avoid null elements unless coder supports nulls"],"tags":["java","apache-beam","create","coder","io"],"backgroundTag":"json-marshal-failed","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"}