{"record":{"id":"9277208190b4e02a","repo":"apache/beam","slug":"groupintobatcheswithshardedkey-type-parameter-k-v-does-not","errorCode":null,"errorMessage":"GroupIntoBatchesWithShardedKey: type parameter K (%v) does not match input key type (%v)","messagePattern":"GroupIntoBatchesWithShardedKey: type parameter K \\((.+?)\\) does not match input key type \\((.+?)\\)","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"sdks/go/pkg/beam/transforms/batch/batch.go","lineNumber":670,"sourceCode":"//\n// Sharding spreads the processing of a single hot logical key across\n// multiple workers: each shard is independent state, so distributed\n// runners can parallelize without the user's key type changing.\nfunc GroupIntoBatchesWithShardedKey[K any](s beam.Scope, params Params, col beam.PCollection) beam.PCollection {\n\ts = s.Scope(\"batch.GroupIntoBatchesWithShardedKey\")\n\n\tif err := params.validate(); err != nil {\n\t\tpanic(fmt.Errorf(\"GroupIntoBatchesWithShardedKey: %w\", err))\n\t}\n\tif !typex.IsKV(col.Type()) {\n\t\tpanic(fmt.Errorf(\n\t\t\t\"GroupIntoBatchesWithShardedKey: input PCollection must be KV-typed; got %v\",\n\t\t\tcol.Type()))\n\t}\n\tkeyFT := col.Type().Components()[0]\n\tvar zero K\n\tif keyFT.Type() != reflect.TypeOf(zero) {\n\t\tpanic(fmt.Errorf(\n\t\t\t\"GroupIntoBatchesWithShardedKey: type parameter K (%v) does not match input key type (%v)\",\n\t\t\treflect.TypeOf(zero), keyFT.Type()))\n\t}\n\n\twrapped := beam.ParDo(s, &wrapShardedKeyFn[K]{}, col)\n\treturn GroupIntoBatches(s, params, wrapped)\n}\n","sourceCodeStart":652,"sourceCodeEnd":678,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/go/pkg/beam/transforms/batch/batch.go#L652-L678","documentation":"GroupIntoBatchesWithShardedKey is generic over key type K and checks that the input PCollection's KV key component equals K via reflect.TypeOf(zero K). A mismatch means the wrapped KV pairs cannot be typed as K, so the transform panics with both type names in the message. Align K with the input key type or convert the keys.","triggerScenarios":"Calling GroupIntoBatchesWithShardedKey[K] where K differs from the input's KV key component, e.g. GroupIntoBatchesWithShardedKey[string] fed a KV<int, V> collection.","commonSituations":"Relying on Go type inference after an upstream key-type change; copy-pasting a generic call and forgetting to update K; an intermediate ParDo changing the key type silently.","solutions":["Set K to match the input key type (or vice versa)","Insert a ParDo that converts keys to type K before the call","Check reflect.TypeOf(zero K) against col.Type().Components()[0] before calling","Pin K explicitly at the call site rather than relying on inference"],"exampleFix":"// before\nbatch.GroupIntoBatchesWithShardedKey[string](s, params, intKeyedKV)\n// after\nbatch.GroupIntoBatchesWithShardedKey[int](s, params, intKeyedKV)","handlingStrategy":"type-guard","validationCode":"var zero K\nif col.Type().Components()[0].Type() != reflect.TypeOf(zero) {\n    return fmt.Errorf(\"K does not match input key type\")\n}","typeGuard":"func keyMatches[K any](col beam.PCollection) bool {\n    var zero K\n    return col.Type().Components()[0].Type() == reflect.TypeOf(zero)\n}","tryCatchPattern":null,"preventionTips":["Pin K explicitly instead of relying on inference","Convert keys with a ParDo when upstream types change","Add unit tests asserting K matches the input key type"],"tags":["go","apache-beam","generics","type-mismatch"],"backgroundTag":"type-mismatch","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"}