{"record":{"id":"690009542654f1da","repo":"apache/beam","slug":"shardedkeyencoder-elm2-must-be-byte-shardid-got-t","errorCode":null,"errorMessage":"shardedKeyEncoder: Elm2 must be []byte shardID (got %T)","messagePattern":"shardedKeyEncoder: Elm2 must be \\[\\]byte shardID \\(got %T\\)","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"sdks/go/pkg/beam/core/runtime/exec/coder.go","lineNumber":1387,"sourceCode":"\n// shardedKeyEncoder encodes ShardedKey-typed values in the standard\n// beam:coder:sharded_key:v1 wire format:\n//\n//\tByteArrayCoder.encode(ShardID) ++ keyCoder.encode(Key)\n//\n// Runtime values are carried by a FullValue whose Elm holds the user key\n// and whose Elm2 holds the []byte shard identifier — the same two-part\n// convention used by the KV coder. This matches the Java\n// util.ShardedKey.Coder and Python sharded_key encodings exactly; any\n// divergence of a single byte would silently corrupt cross-SDK pipelines.\ntype shardedKeyEncoder struct {\n\tkey ElementEncoder\n}\n\nfunc (e *shardedKeyEncoder) Encode(val *FullValue, w io.Writer) error {\n\tshardID, ok := val.Elm2.([]byte)\n\tif !ok {\n\t\treturn errors.Errorf(\n\t\t\t\"shardedKeyEncoder: Elm2 must be []byte shardID (got %T)\", val.Elm2)\n\t}\n\tif err := coder.EncodeBytes(shardID, w); err != nil {\n\t\treturn errors.WithContext(err, \"shardedKeyEncoder: shardID\")\n\t}\n\treturn e.key.Encode(&FullValue{Elm: val.Elm}, w)\n}\n\n// shardedKeyDecoder is the inverse of shardedKeyEncoder. Decoded values\n// are placed in FullValue{Elm: key, Elm2: shardID}.\ntype shardedKeyDecoder struct {\n\tkey ElementDecoder\n}\n\nfunc (d *shardedKeyDecoder) DecodeTo(r io.Reader, fv *FullValue) error {\n\tshardID, err := coder.DecodeBytes(r)\n\tif err != nil {\n\t\treturn errors.WithContext(err, \"shardedKeyDecoder: shardID\")","sourceCodeStart":1369,"sourceCodeEnd":1405,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/go/pkg/beam/core/runtime/exec/coder.go#L1369-L1405","documentation":"The shardedKeyEncoder encodes a sharded key as encoded bytes: the shard ID (Elm2) followed by the key (Elm). It requires Elm2 to be a []byte shard ID. If the FullValue's Elm2 holds any other type, encoding cannot proceed and this error is returned instead of silently mis-encoding.","triggerScenarios":"Calling Encode on a sharded-key coder (created via NewShardedKeyEncoder) with a FullValue whose Elm2 is nil or a non-[]byte value, e.g. Elm2 left unset or set to a string/int shard ID.","commonSituations":"Custom exec units or test harnesses constructing FullValue objects by hand for sharded keys; pipeline construction bugs where the sharded key coder is attached but the plan emits plain KVs without shard bytes.","solutions":["Ensure the FullValue passed to a sharded-key coder has Elm2 set to a []byte shard ID.","If Elm2 is nil, assign an explicit shard ID: fv.Elm2 = []byte(\"shard-0\").","Verify the coder used matches the element type; a plain KV coder should be used when no shard ID exists."],"exampleFix":"// before\nfv := &FullValue{Elm: key}\nenc.Encode(fv, w) // Elm2 is nil\n\n// after\nfv := &FullValue{Elm: key, Elm2: []byte(shardID)}\nenc.Encode(fv, w)","handlingStrategy":"type-guard","validationCode":"if fv.Elm2 == nil { fv.Elm2 = []byte(defaultShardID) }\nshard, ok := fv.Elm2.([]byte)\nif !ok { return fmt.Errorf(\"Elm2 must be []byte, got %T\", fv.Elm2) }","typeGuard":"func hasShardID(fv *FullValue) bool { _, ok := fv.Elm2.([]byte); return ok }","tryCatchPattern":"if err := enc.Encode(fv, w); err != nil {\n\tif strings.Contains(err.Error(), \"shardedKeyEncoder: Elm2 must be []byte\") {\n\t\t// fix element or fall back to plain KV coder\n\t}\n\treturn err\n}","preventionTips":["Always set Elm2 to a []byte shard ID when using sharded-key coders.","Centralize FullValue construction so shard IDs are assigned consistently.","Add unit tests that encode sharded keys through the full coder path."],"tags":["go","beam","encoding","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-14T21:17:11.552Z"}