{"record":{"id":"d84901588034c947","repo":"apache/beam","slug":"unable-to-infer-a-coder-for-bagstate-and-no-coder-was","errorCode":null,"errorMessage":"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.","messagePattern":"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\\.","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"sdks/java/core/src/main/java/org/apache/beam/sdk/state/StateSpecs.java","lineNumber":644,"sourceCode":"    }\n\n    @Override\n    public <ResultT> ResultT match(Cases<ResultT> cases) {\n      return cases.dispatchBag(elemCoder);\n    }\n\n    @SuppressWarnings(\"unchecked\")\n    @Override\n    public void offerCoders(Coder[] coders) {\n      if (this.elemCoder == null && coders[0] != null) {\n        this.elemCoder = (Coder<T>) coders[0];\n      }\n    }\n\n    @Override\n    public void finishSpecifying() {\n      if (elemCoder == null) {\n        throw new IllegalStateException(\n            \"Unable to infer a coder for BagState and no Coder\"\n                + \" was specified. Please set a coder by either invoking\"\n                + \" StateSpecs.bag(Coder<T> elemCoder) or by registering the coder in the\"\n                + \" Pipeline's CoderRegistry.\");\n      }\n    }\n\n    @Override\n    public boolean equals(@Nullable Object obj) {\n      if (obj == this) {\n        return true;\n      }\n\n      if (!(obj instanceof BagStateSpec)) {\n        return false;\n      }\n\n      BagStateSpec<?> that = (BagStateSpec<?>) obj;","sourceCodeStart":626,"sourceCodeEnd":662,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/java/core/src/main/java/org/apache/beam/sdk/state/StateSpecs.java#L626-L662","documentation":"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).","triggerScenarios":"Calling StateSpecs.bag() (no-arg overload) where the element type T's coder cannot be inferred by the CoderRegistry.","commonSituations":"Bagging custom POJOs or third-party types with no registered coder; tests building state cells outside a full pipeline where inference doesn't run.","solutions":["Pass an explicit element coder: StateSpecs.bag(elemCoder).","Register a coder for T in the pipeline's CoderRegistry.","Use a serializable element type (e.g. primitives, String, Avro/annotated POJOs)."],"exampleFix":"// before\nBagStateSpec<String> spec = StateSpecs.bag();\n// after\nBagStateSpec<String> spec = StateSpecs.bag(StringUtf8Coder.of());","handlingStrategy":"validation","validationCode":"if (elemCoder == null) {\n  throw new IllegalStateException(\"Supply StateSpecs.bag(elemCoder) with an explicit element coder\");\n}","typeGuard":null,"tryCatchPattern":"try {\n  spec = StateSpecs.bag();\n} catch (IllegalStateException e) {\n  if (e.getMessage().contains(\"BagState\")) {\n    spec = StateSpecs.bag(elemCoder);\n  } else throw e;\n}","preventionTips":["Always pass an explicit element coder to StateSpecs.bag()","Register coders for custom element types","Run pipeline construction in tests to catch coder issues early"],"tags":["java","apache-beam","state","coder-inference"],"backgroundTag":"missing-required-config","analyzedSha":"12126d8942aaf848030c478b4c6a28c6af861c66","analyzedAt":"2026-09-13T01:50:10.254Z","contentChangedAt":"2026-09-13T01:50:10.254Z","schemaVersion":2},"datasetVersion":"2026-09-14T16:17:12.679Z"}