apache/beam · error · IllegalArgumentException

expecting the input Coder

Error message

expecting the input Coder<Iterable> to be an IterableLikeCoder

What it means

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.

Solutions

  1. Remove any explicit setCoder call so Beam infers the IterableLikeCoder automatically
  2. Use the standard coder for Iterable<T> (ListCoder/IterableCoder, both IterableLikeCoder subclasses) via input.setCoder(ListCoder.of(elemCoder))
  3. If the upstream transform emits a custom coder, add a re-coding step (apply a map to a List) before Flatten.iterables()

Example fix

// before
iterPcollection.setCoder(new MyIterableCoder<T>());
// after
iterPcollection.setCoder(ListCoder.of(elemCoder)); // ListCoder extends IterableLikeCoder
Defensive patterns

Strategy: type-guard

Validate before calling

// Check the input coder before Flatten.iterables()
if (!(in.getCoder() instanceof IterableLikeCoder)) {
  throw new IllegalStateException("input must use an IterableLikeCoder (e.g. ListCoder)");
}

Type guard

boolean hasIterableLikeCoder(PCollection<? extends Iterable<T>> in) {
  return in.getCoder() instanceof IterableLikeCoder;
}

Try / catch

try { out = in.apply(Flatten.iterables()); }
catch (IllegalArgumentException e) { // re-code to List and retry
  ListCoder<T> lc = ListCoder.of(inferElemCoder(in));
  in.setCoder(lc);
  out = in.apply(Flatten.iterables());
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Understand the failure class

Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.

Related errors


AI-assisted analysis of apache/beam@12126d8942 (2026-09-13). Data as JSON: /api/errors/f79ee394d2657055. Report an issue: GitHub.

Appendix: source

Thrown at sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/Flatten.java:226

  }

  /**
   * {@code FlattenIterables<T>} takes a {@code PCollection<Iterable<T>>} and returns a {@code
   * PCollection<T>} that contains all the elements from each iterable. Implements {@link
   * #iterables}.
   *
   * @param <T> the type of the elements of the input {@code Iterable}s and the output {@code
   *     PCollection}
   */
  public static class Iterables<T>
      extends PTransform<PCollection<? extends Iterable<T>>, PCollection<T>> {
    private Iterables() {}

    @Override
    public PCollection<T> expand(PCollection<? extends Iterable<T>> in) {
      Coder<? extends Iterable<T>> inCoder = in.getCoder();
      if (!(inCoder instanceof IterableLikeCoder)) {
        throw new IllegalArgumentException(
            "expecting the input Coder<Iterable> to be an IterableLikeCoder");
      }
      @SuppressWarnings("unchecked")
      Coder<T> elemCoder = ((IterableLikeCoder<T, ?>) inCoder).getElemCoder();

      return in.apply(
              "FlattenIterables",
              FlatMapElements.via(
                  new SimpleFunction<Iterable<T>, Iterable<T>>() {
                    @Override
                    public Iterable<T> apply(Iterable<T> element) {
                      return element;
                    }
                  }))
          .setCoder(elemCoder);
    }
  }
}

View on GitHub (pinned to 12126d8942)