{"record":{"id":"6a20d6370e565f4f","repo":"apache/beam","slug":"set-or-map-state-type-v-must-have-a-key-coder-type-none","errorCode":null,"errorMessage":"set or map state type %v must have a key coder type, none detected","messagePattern":"set or map state type (.+?) must have a key coder type, none detected","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"sdks/go/pkg/beam/core/runtime/graphx/translate.go","lineNumber":525,"sourceCode":"\t\t\tm.requirements[URNRequiresStatefulProcessing] = true\n\t\t\tstateSpecs := make(map[string]*pipepb.StateSpec)\n\t\t\tfor _, ps := range edge.Edge.DoFn.PipelineState() {\n\t\t\t\tcoderID := \"\"\n\t\t\t\tc, ok := edge.Edge.StateCoders[UserStateCoderID(ps)]\n\t\t\t\tif ok {\n\t\t\t\t\tcoderID, err = m.coders.Add(c)\n\t\t\t\t\tif err != nil {\n\t\t\t\t\t\treturn handleErr(err)\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tkeyCoderID := \"\"\n\t\t\t\tif c, ok := edge.Edge.StateCoders[UserStateKeyCoderID(ps)]; ok {\n\t\t\t\t\tkeyCoderID, err = m.coders.Add(c)\n\t\t\t\t\tif err != nil {\n\t\t\t\t\t\treturn handleErr(err)\n\t\t\t\t\t}\n\t\t\t\t} else if ps.StateType() == state.TypeMap || ps.StateType() == state.TypeSet {\n\t\t\t\t\treturn nil, errors.Errorf(\"set or map state type %v must have a key coder type, none detected\", ps)\n\t\t\t\t}\n\t\t\t\tswitch ps.StateType() {\n\t\t\t\tcase state.TypeValue:\n\t\t\t\t\tstateSpecs[ps.StateKey()] = &pipepb.StateSpec{\n\t\t\t\t\t\tSpec: &pipepb.StateSpec_ReadModifyWriteSpec{\n\t\t\t\t\t\t\tReadModifyWriteSpec: &pipepb.ReadModifyWriteStateSpec{\n\t\t\t\t\t\t\t\tCoderId: coderID,\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t},\n\t\t\t\t\t\tProtocol: &pipepb.FunctionSpec{\n\t\t\t\t\t\t\tUrn: URNBagUserState,\n\t\t\t\t\t\t},\n\t\t\t\t\t}\n\t\t\t\tcase state.TypeBag:\n\t\t\t\t\tstateSpecs[ps.StateKey()] = &pipepb.StateSpec{\n\t\t\t\t\t\tSpec: &pipepb.StateSpec_BagSpec{\n\t\t\t\t\t\t\tBagSpec: &pipepb.BagStateSpec{\n\t\t\t\t\t\t\t\tElementCoderId: coderID,","sourceCodeStart":507,"sourceCodeEnd":543,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/go/pkg/beam/core/runtime/graphx/translate.go#L507-L543","documentation":"A map or set user state (state.TypeMap / state.TypeSet) requires an associated key coder, registered under UserStateKeyCoderID. When translating the DoFn's state specs the marshaller found no key coder for this state, which is mandatory for keyed state types, so it returns this error naming the provider state.","triggerScenarios":"Using state.MapState or state.SetState inside a DoFn without providing the key coder — e.g. declaring the state with a value type but no key type, or the state provider missing its key coder registration.","commonSituations":"Typo or wrong generic parameters when declaring map/set state; Beam SDK versions where key coder inference for map/set state changed; hand-rolled state providers in custom runners.","solutions":["Declare map/set state with the proper key type so the key coder is derived (state.MapState[K,V] / state.SetState[K])","Verify the state's KeyCoder is registered via UserStateKeyCoderID in your provider","Update the SDK if relying on automatic key-coder inference","Replace map state with repeated value state + GBK if key coders cannot be provided"],"exampleFix":"// before\nvar st = state.MapState[...] // missing key coder\n// after\ntype fn struct { counts state.MapState[string, int64] } // key type string derives coder","handlingStrategy":"validation","validationCode":"// At pipeline construction, assert every map/set state declares a key coder\ntype keyed interface { StateType() state.Type; KeyCoder() Coder }\nif st.StateType() == state.TypeMap || st.StateType() == state.TypeSet {\n    if st.(keyed).KeyCoder() == nil { return errors.New(\"map/set state missing key coder\") }\n}","typeGuard":"func hasKeyCoder(ps state.ProviderSpec) bool {\n    return ps.StateType() != state.TypeMap && ps.StateType() != state.TypeSet || ps.Has(UserStateKeyCoderID(ps))\n}","tryCatchPattern":"if _, err := graphx.Marshal(p); err != nil {\n    if strings.Contains(err.Error(), \"must have a key coder type\") {\n        return fmt.Errorf(\"stateful DoFn misconfigured: %v\", err)\n    }\n    return err\n}","preventionTips":["Always parametrize state.MapState / state.SetState with explicit key and value types","Write a smoke test that marshals every stateful DoFn pipeline","Review stateful DoFn declarations when upgrading Beam"],"tags":["go","apache-beam","state","coder"],"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-20T03:17:13.778Z"}