{"record":{"id":"2877feed360485cb","repo":"apache/beam","slug":"groupintobatcheswithshardedkey-input-pcollection-must-be-kv","errorCode":null,"errorMessage":"GroupIntoBatchesWithShardedKey: input PCollection must be KV-typed; got %v","messagePattern":"GroupIntoBatchesWithShardedKey: input PCollection must be KV-typed; got (.+?)","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"sdks/go/pkg/beam/transforms/batch/batch.go","lineNumber":663,"sourceCode":"// GroupIntoBatchesWithShardedKey wraps each user key with a\n// ShardedKey{Key: K, ShardID: [24]byte} and then applies\n// GroupIntoBatches. Output is PCollection<KV<ShardedKey[K], []V>>.\n//\n// The key type K must have been registered via\n// RegisterShardedKeyType[K] at init time. Common types (string,\n// []byte, int, int64) are registered automatically.\n//\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":645,"sourceCodeEnd":678,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/go/pkg/beam/transforms/batch/batch.go#L645-L678","documentation":"GroupIntoBatchesWithShardedKey is generic over key type K and requires the input PCollection's KV key component to match K exactly. A mismatch cannot be reconciled, so the transform panics with both types in the message. Re-shape the input or supply the correct type parameter.","triggerScenarios":"Calling GroupIntoBatchesWithShardedKey[string](s, params, kvCol) where kvCol's key component is int (or another type), failing keyFT.Type() != reflect.TypeOf(zero K).","commonSituations":"Letting Go infer K incorrectly, or feeding a KV collection whose key type changed upstream after a refactor.","solutions":["Make the input a KV with key type equal to K, or change K to match keyFT","Check col.Type().Components()[0] before calling","Insert a ParDo to convert keys to the expected type","Pin K explicitly at the call site instead of relying on inference"],"exampleFix":"// before\nbatch.GroupIntoBatchesWithShardedKey[string](s, params, intKeyed) // keys are int\n// after\nbatch.GroupIntoBatchesWithShardedKey[int](s, params, intKeyed)","handlingStrategy":"type-guard","validationCode":"var zero K\nif col.Type().Components()[0].Type() != reflect.TypeOf(zero) {\n    return fmt.Errorf(\"key type mismatch\")\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":["Specify K explicitly at call sites","Check component types before generic calls","Test generic helpers with representative key types"],"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"}