apache/beam · error · IllegalStateException

Unable to infer a coder for BagState and no Coder was specif

Error message

Unable to infer a coder for BagState and no Coder was specified. Please set a coder by either invoking StateSpecs.bag(Coder<T> elemCoder) or by registering the coder in the Pipeline's CoderRegistry.

What it means

finishSpecifying on the BagState spec found elemCoder still null: neither was a coder passed to StateSpecs.bag(...) nor could one be inferred/offered via offerCoders during pipeline construction. The bag state spec is the faulty input — its element type is invisible to CoderRegistry inference (often due to erasure or unregistered types).

Source

Thrown at sdks/java/core/src/main/java/org/apache/beam/sdk/state/StateSpecs.java:644

    }

    @Override
    public <ResultT> ResultT match(Cases<ResultT> cases) {
      return cases.dispatchBag(elemCoder);
    }

    @SuppressWarnings("unchecked")
    @Override
    public void offerCoders(Coder[] coders) {
      if (this.elemCoder == null && coders[0] != null) {
        this.elemCoder = (Coder<T>) coders[0];
      }
    }

    @Override
    public void finishSpecifying() {
      if (elemCoder == null) {
        throw new IllegalStateException(
            "Unable to infer a coder for BagState and no Coder"
                + " was specified. Please set a coder by either invoking"
                + " StateSpecs.bag(Coder<T> elemCoder) or by registering the coder in the"
                + " Pipeline's CoderRegistry.");
      }
    }

    @Override
    public boolean equals(@Nullable Object obj) {
      if (obj == this) {
        return true;
      }

      if (!(obj instanceof BagStateSpec)) {
        return false;
      }

      BagStateSpec<?> that = (BagStateSpec<?>) obj;

View on GitHub (pinned to 12126d8942)

Solutions

  1. Pass an explicit element coder: StateSpecs.bag(elemCoder).
  2. Register a coder for T in the pipeline's CoderRegistry.
  3. Use a serializable element type (e.g. primitives, String, Avro/annotated POJOs).

Example fix

// before
BagStateSpec<String> spec = StateSpecs.bag();
// after
BagStateSpec<String> spec = StateSpecs.bag(StringUtf8Coder.of());
Defensive patterns

Strategy: validation

Validate before calling

if (elemCoder == null) {
  throw new IllegalStateException("Supply StateSpecs.bag(elemCoder) with an explicit element coder");
}

Try / catch

try {
  spec = StateSpecs.bag();
} catch (IllegalStateException e) {
  if (e.getMessage().contains("BagState")) {
    spec = StateSpecs.bag(elemCoder);
  } else throw e;
}

Prevention

When it happens

Trigger: Calling StateSpecs.bag() (no-arg overload) where the element type T's coder cannot be inferred by the CoderRegistry.

Common situations: Bagging custom POJOs or third-party types with no registered coder; tests building state cells outside a full pipeline where inference doesn't run.

Understand the failure class

Background: "X is required", "must be set", "cannot be empty": the missing-required-config error family, from Vertex AI project/location to WeChat keys — this error's family across 18 libraries.

Related errors


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