{"record":{"id":"12a5fed7e3ac0c6f","repo":"apache/beam","slug":"err-batch","errorCode":null,"errorMessage":"err","messagePattern":"err","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"sdks/go/pkg/beam/transforms/batch/batch.go","lineNumber":112,"sourceCode":"// Users of other K types must call this at init time.\nfunc RegisterShardedKeyType[K any]() {\n\tvar zero K\n\tkeyT := reflect.TypeOf(zero)\n\tskT := reflect.TypeOf(ShardedKey[K]{})\n\n\tregister.DoFn3x0[K, typex.V, func(ShardedKey[K], typex.V)](&wrapShardedKeyFn[K]{})\n\tregister.Emitter2[ShardedKey[K], typex.V]()\n\tbeam.RegisterType(skT)\n\n\tkeyEnc := beam.NewElementEncoder(keyT)\n\tkeyDec := beam.NewElementDecoder(keyT)\n\n\tenc := func(sk ShardedKey[K]) []byte {\n\t\tvar buf bytes.Buffer\n\t\twriteVarInt(&buf, int64(len(sk.ShardID)))\n\t\tbuf.Write(sk.ShardID)\n\t\tif err := keyEnc.Encode(sk.Key, &buf); err != nil {\n\t\t\tpanic(err)\n\t\t}\n\t\treturn buf.Bytes()\n\t}\n\tdec := func(b []byte) ShardedKey[K] {\n\t\tr := bytes.NewReader(b)\n\t\tn := readVarInt(r)\n\t\tshardID := make([]byte, n)\n\t\tif n > 0 {\n\t\t\tif _, err := r.Read(shardID); err != nil {\n\t\t\t\tpanic(err)\n\t\t\t}\n\t\t}\n\t\tk, err := keyDec.Decode(r)\n\t\tif err != nil {\n\t\t\tpanic(err)\n\t\t}\n\t\treturn ShardedKey[K]{Key: k.(K), ShardID: shardID}\n\t}","sourceCodeStart":94,"sourceCodeEnd":130,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/go/pkg/beam/transforms/batch/batch.go#L94-L130","documentation":"Inside batch.RegisterShardedKeyType's ShardedKey encoder closure, encoding the key component via the registered key encoder fails, and the closure panics with the raw error. This is an internal invariant: deterministic encoding of the sharded key should never fail for a properly registered key type.","triggerScenarios":"The enc closure for ShardedKey[K] is invoked during coder serialization when keyEnc.Encode(sk.Key, &buf) returns an error — e.g. the key type K lacks a registered deterministic encoder or contains an unsupported field type.","commonSituations":"GroupIntoBatches used with a key type that was never registered via RegisterShardedKeyType / beamcoder deterministic coder; keys containing channels, funcs, or unencodable nested types.","solutions":["Register the key type K with a deterministic coder (RegisterShardedKeyType / beamcoder.RegisterDeterministicCoderWithFuncs) before using GroupIntoBatches.","Simplify the key type to encodable primitives/structs.","Recover in a wrapper and surface a descriptive error naming the key type."],"exampleFix":"// before\nif err := keyEnc.Encode(sk.Key, &buf); err != nil { panic(err) }\n// after\nif err := keyEnc.Encode(sk.Key, &buf); err != nil {\n    panic(fmt.Errorf(\"batch: encoding ShardedKey key %T: %w\", sk.Key, err))\n}","handlingStrategy":"validation","validationCode":"// before using GroupIntoBatches, ensure K has a deterministic coder\nif !coder.HasDeterministicCoder(reflect.TypeOf(KeyType{})) {\n    batch.RegisterShardedKeyType[KeyType]()\n}","typeGuard":"func keyTypeEncodable[K any]() bool {\n    var k K\n    switch reflect.TypeOf(k).Kind() {\n    case reflect.Chan, reflect.Func, reflect.UnsafePointer:\n        return false\n    }\n    return true\n}","tryCatchPattern":"defer func() {\n    if r := recover(); r != nil {\n        log.Fatalf(\"ShardedKey encode failed (check key type registration): %v\", r)\n    }\n}()","preventionTips":["Always register a deterministic coder for the key type K","Avoid channels/funcs/unsupported fields in key types","Pin the Beam Go SDK version across pipeline stages"],"tags":["go","beam","coder","encoding","panic"],"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"}