{"record":{"id":"21b1040fd961ac31","repo":"apache/beam","slug":"duplicate-state-key-v-used-by-v-and-v-ensure-that-state-keys","errorCode":null,"errorMessage":"Duplicate state key %v used by %v and %v. Ensure that state keys areunique per DoFn","messagePattern":"Duplicate state key (.+?) used by (.+?) and (.+?)\\. Ensure that state keys areunique per DoFn","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"sdks/go/pkg/beam/core/graph/fn.go","lineNumber":1366,"sourceCode":"\tps := fn.PipelineState()\n\n\tif _, hasSp := fn.methods[processElementName].StateProvider(); hasSp {\n\t\tif numIn == MainSingle {\n\t\t\terr := errors.Errorf(\"ProcessElement uses a StateProvider, but is not keyed\")\n\t\t\treturn errors.SetTopLevelMsgf(err, \"ProcessElement uses a StateProvider, but is not keyed. \"+\n\t\t\t\t\"All stateful DoFns must take a key/value pair as an input.\")\n\t\t}\n\t\tif len(ps) == 0 {\n\t\t\terr := errors.Errorf(\"ProcessElement uses a StateProvider, but noState structs are attached to the DoFn\")\n\t\t\treturn errors.SetTopLevelMsgf(err, \"ProcessElement uses a StateProvider, but no State structs are \"+\n\t\t\t\t\"attached to the DoFn. Ensure that you are including the State structs you're using to read/write\"+\n\t\t\t\t\"global state as public uppercase member variables.\")\n\t\t}\n\t\tstateKeys := make(map[string]state.PipelineState)\n\t\tfor _, s := range ps {\n\t\t\tk := s.StateKey()\n\t\t\tif orig, ok := stateKeys[k]; ok {\n\t\t\t\terr := errors.Errorf(\"Duplicate state key %v\", k)\n\t\t\t\treturn errors.SetTopLevelMsgf(err, \"Duplicate state key %v used by %v and %v. Ensure that state keys are\"+\n\t\t\t\t\t\"unique per DoFn\", k, orig, s)\n\t\t\t}\n\t\t\tt := s.StateType()\n\t\t\tif t != state.TypeValue && t != state.TypeBag && t != state.TypeCombining && t != state.TypeSet && t != state.TypeMap && t != state.TypeOrderedList {\n\t\t\t\terr := errors.Errorf(\"Unrecognized state type %v for state %v\", t, s)\n\t\t\t\treturn errors.SetTopLevelMsgf(err, \"Unrecognized state type %v for state %v. Currently the only supported state\"+\n\t\t\t\t\t\"types are state.Value, state.Combining, state.Bag, state.Set, state.Map, and state.OrderedList\", t, s)\n\t\t\t}\n\t\t\tstateKeys[k] = s\n\t\t}\n\t} else {\n\t\tif len(ps) > 0 {\n\t\t\terr := errors.Errorf(\"ProcessElement doesn't use a StateProvider, but State structs are attached to \"+\n\t\t\t\t\"the DoFn: %v\", ps)\n\t\t\treturn errors.SetTopLevelMsgf(err, \"ProcessElement doesn't use a StateProvider, but State structs are \"+\n\t\t\t\t\"attached to the DoFn: %v\\nEnsure that you are using the StateProvider to perform any reads or writes\"+\n\t\t\t\t\"of pipeline state.\", ps)","sourceCodeStart":1348,"sourceCodeEnd":1384,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/go/pkg/beam/core/graph/fn.go#L1348-L1384","documentation":"Beam Go assigns each state field a unique state key derived from the field name. During DoFn validation, if two State structs on the same DoFn resolve to the same state key, this error is thrown, since state would be silently shared/corrupted between the two fields.","triggerScenarios":"Declaring two State fields on one DoFn whose keys collide — typically two fields with the same name in embedded structs, or two State structs constructed with the same state key identifier.","commonSituations":"Embedding two structs that both contain a State field with the same name; copy-pasting a state field and forgetting to rename it; generated DoFns where template fields weren't uniquified.","solutions":["Rename one of the conflicting State fields so each has a unique name/key.","Flatten embedded structs and deduplicate state field names.","Merge the two state fields into one if they intentionally represent the same state."],"exampleFix":"// before\ntype CountFn struct {\n    Seen state.Value[string, int]\n    Seen state.Bag[string] // duplicate key\n}\n// after\ntype CountFn struct {\n    Seen      state.Value[string, int]\n    SeenItems state.Bag[string]\n}","handlingStrategy":"validation","validationCode":"seen := map[string]bool{}\nt := reflect.TypeOf(fn)\nfor i := 0; i < t.NumField(); i++ {\n    k := t.Field(i).Name\n    if seen[k] { log.Fatalf(\"duplicate state field name: %s\", k) }\n    seen[k] = true\n}","typeGuard":null,"tryCatchPattern":"if err := beam.Run(ctx, p); err != nil {\n    if strings.Contains(err.Error(), \"Duplicate state key\") {\n        log.Fatalf(\"rename one of the colliding State fields: %v\", err)\n    }\n    return err\n}","preventionTips":["Give every State field a distinct, descriptive name","Avoid embedding multiple structs that contain same-named State fields","Search the DoFn struct for repeated field names before building"],"tags":["go","apache-beam","dofn","state","duplicate-key"],"backgroundTag":"conflicting-config-options","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"}