{"record":{"id":"d912e66504d49d19","repo":"apache/beam","slug":"failed-encoding-key-for-v-v","errorCode":null,"errorMessage":"failed encoding key for %v: %v","messagePattern":"failed encoding key for (.+?): (.+?)","errorType":"error_code","errorClass":null,"httpStatus":null,"severity":"error","filePath":"sdks/go/pkg/beam/runners/direct/gbk.go","lineNumber":165,"sourceCode":"\t\t\t}\n\t\t}\n\t\tfor k := i; k < j; k++ {\n\t\t\tmergeMap[n.wins[k]] = len(mergedWins)\n\t\t}\n\t\tmergedWins = append(mergedWins, window.IntervalWindow{Start: mergeStart, End: mergeEnd})\n\t\ti = j\n\t}\n\tn.wins = mergedWins\n\treturn mergeMap, nil\n}\n\nfunc (n *CoGBK) reprocessByWindow(mergeMap map[typex.Window]int) error {\n\tnewGroups := make(map[string]*group)\n\tfor _, g := range n.m {\n\t\tws := []typex.Window{n.wins[mergeMap[g.key.Windows[0]]]}\n\t\tgr, err := n.getGroup(newGroups, &g.key, ws)\n\t\tif err != nil {\n\t\t\treturn errors.Errorf(\"failed encoding key for %v: %v\", g.key.Elm, err)\n\t\t}\n\t\tfor i, list := range g.values {\n\t\t\tgr.values[i] = append(gr.values[i], list...)\n\t\t}\n\t}\n\tn.m = newGroups\n\treturn nil\n}\n\nfunc (n *CoGBK) Down(ctx context.Context) error {\n\treturn nil\n}\n\nfunc (n *CoGBK) String() string {\n\treturn fmt.Sprintf(\"CoGBK. Out:%v\", n.Out.ID())\n}\n\n// Inject injects the predecessor index into each FullValue, effectively","sourceCodeStart":147,"sourceCodeEnd":183,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/go/pkg/beam/runners/direct/gbk.go#L147-L183","documentation":"During CoGBK reprocessing by window in the direct runner, keys are re-encoded into a new group per merged window. If the key coder fails to encode the key element, reprocessByWindow wraps the underlying coder error as \"failed encoding key for %v: %v\". This indicates the key's encoded bytes cannot be produced, typically due to an unsupported or misregistered key coder.","triggerScenarios":"Calling FinishBundle on a CoGBK node whose pending groups must be reprocessed by window; getGroup fails because the key element cannot be encoded by the pipeline coder for the key PCollection.","commonSituations":"Using a key type without a registered/derivable coder; a custom coder's Encode method returning an error; keys containing values that violate the coder's assumptions (e.g. nil pointers).","solutions":["Inspect the wrapped inner error to identify the failing coder and fix the coder implementation.","Ensure the key type has a valid registered coder or use a natively encodable key type (string, int, etc.).","Check the custom coder's Encode for unhandled cases (nil, oversized buffers) and correct it.","Simplify the key to a basic type via beam.ParDo before the CoGBK."],"exampleFix":"// before: key of struct without coder support\nbeam.CoGBK(s, keyedPairs)\n// after: encode key as string first\nbeam.CoGBK(s, beam.ParDo(s, func(k MyKey, v V) (string, V) { return k.String(), v }, keyedPairs))","handlingStrategy":"try-catch","validationCode":"// Go: sanity-check that the key encodes before building the pipeline\nvar buf bytes.Buffer\nif err := beam.EncodeElement(context.Background(), key, buf); err != nil { /* switch key type or register coder */ }","typeGuard":null,"tryCatchPattern":"if err := beam.Run(...); err != nil {\n    var coderErr interface{ Unwrap() error }\n    if errors.As(err, &coderErr) { log.Printf(\"key coder failed: %v\", coderErr) }\n}","preventionTips":["Use natively encodable key types (string, int, []byte) for CoGBK keys.","Register coders for custom key types or implement a correct custom coder.","Test Encode/Decode round-trips for custom key coders.","Avoid nil or partially-initialized key structs."],"tags":["go","beam","encoding","coder"],"backgroundTag":"json-marshal-failed","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"}