apache/beam · error · IllegalStateException

Unable to infer a coder for SetState and no Coder was…

Error message

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

What it means

finishSpecifying on the SetState spec found elemCoder null: StateSpecs.set(...) was called without a coder and CoderRegistry inference failed to offer one before specification finished. The set's element type is the input at fault; the message prescribes the two remedies (pass a coder explicitly or register one).

Solutions

  1. Pass an explicit element coder: StateSpecs.set(elemCoder).
  2. Register a coder for T in the pipeline's CoderRegistry.
  3. Use an element type with a default/inferrable coder.

Example fix

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

Strategy: validation

Validate before calling

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

Try / catch

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

Prevention

When it happens

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

Common situations: Sets of custom POJOs or third-party types without registered coders; building state specs in tests outside normal pipeline coder inference.

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/9897aa674392e51f. Report an issue: GitHub.

Appendix: source

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

    }

    @Override
    public <ResultT> ResultT match(Cases<ResultT> cases) {
      return cases.dispatchSet(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 SetState and no Coder"
                + " was specified. Please set a coder by either invoking"
                + " StateSpecs.set(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 SetStateSpec)) {
        return false;
      }

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

View on GitHub (pinned to 12126d8942)