{"record":{"id":"f79ee394d2657055","repo":"apache/beam","slug":"expecting-the-input-coder-iterable-to-be-an","errorCode":null,"errorMessage":"expecting the input Coder<Iterable> to be an IterableLikeCoder","messagePattern":"expecting the input Coder<Iterable> to be an IterableLikeCoder","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/Flatten.java","lineNumber":226,"sourceCode":"  }\n\n  /**\n   * {@code FlattenIterables<T>} takes a {@code PCollection<Iterable<T>>} and returns a {@code\n   * PCollection<T>} that contains all the elements from each iterable. Implements {@link\n   * #iterables}.\n   *\n   * @param <T> the type of the elements of the input {@code Iterable}s and the output {@code\n   *     PCollection}\n   */\n  public static class Iterables<T>\n      extends PTransform<PCollection<? extends Iterable<T>>, PCollection<T>> {\n    private Iterables() {}\n\n    @Override\n    public PCollection<T> expand(PCollection<? extends Iterable<T>> in) {\n      Coder<? extends Iterable<T>> inCoder = in.getCoder();\n      if (!(inCoder instanceof IterableLikeCoder)) {\n        throw new IllegalArgumentException(\n            \"expecting the input Coder<Iterable> to be an IterableLikeCoder\");\n      }\n      @SuppressWarnings(\"unchecked\")\n      Coder<T> elemCoder = ((IterableLikeCoder<T, ?>) inCoder).getElemCoder();\n\n      return in.apply(\n              \"FlattenIterables\",\n              FlatMapElements.via(\n                  new SimpleFunction<Iterable<T>, Iterable<T>>() {\n                    @Override\n                    public Iterable<T> apply(Iterable<T> element) {\n                      return element;\n                    }\n                  }))\n          .setCoder(elemCoder);\n    }\n  }\n}","sourceCodeStart":208,"sourceCodeEnd":244,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/Flatten.java#L208-L244","documentation":"Flatten.iterables() decodes each input element (an Iterable<T>) to extract its element coder and re-encode iterables of the flattened stream. This only works when the input coder is an IterableLikeCoder (which knows how to split elements); any other coder type throws IllegalArgumentException.","triggerScenarios":"Applying Flatten.iterables() to a PCollection<Iterable<T>> whose coder was set explicitly (setCoder) to a non-IterableLikeCoder, or whose inference produced a custom/foreign coder.","commonSituations":"Manually calling setCoder with a custom coder; using a custom source/transform that assigns its own coder to Iterable collections; serialization round-trips that replaced the inferred coder.","solutions":["Remove any explicit setCoder call so Beam infers the IterableLikeCoder automatically","Use the standard coder for Iterable<T> (ListCoder/IterableCoder, both IterableLikeCoder subclasses) via input.setCoder(ListCoder.of(elemCoder))","If the upstream transform emits a custom coder, add a re-coding step (apply a map to a List) before Flatten.iterables()"],"exampleFix":"// before\niterPcollection.setCoder(new MyIterableCoder<T>());\n// after\niterPcollection.setCoder(ListCoder.of(elemCoder)); // ListCoder extends IterableLikeCoder","handlingStrategy":"type-guard","validationCode":"// Check the input coder before Flatten.iterables()\nif (!(in.getCoder() instanceof IterableLikeCoder)) {\n  throw new IllegalStateException(\"input must use an IterableLikeCoder (e.g. ListCoder)\");\n}","typeGuard":"boolean hasIterableLikeCoder(PCollection<? extends Iterable<T>> in) {\n  return in.getCoder() instanceof IterableLikeCoder;\n}","tryCatchPattern":"try { out = in.apply(Flatten.iterables()); }\ncatch (IllegalArgumentException e) { // re-code to List and retry\n  ListCoder<T> lc = ListCoder.of(inferElemCoder(in));\n  in.setCoder(lc);\n  out = in.apply(Flatten.iterables());\n}","preventionTips":["Don't call setCoder with custom coders on Iterable-typed PCollections","Let Beam infer coders; only override with ListCoder/IterableCoder","Validate coder type before applying Flatten.iterables() in library code"],"tags":["java","apache-beam","coder","serialization"],"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"}