{"record":{"id":"5320e09fdac07b27","repo":"apache/beam","slug":"len-mismatch-decoding-a-v-want-d-got-d","errorCode":null,"errorMessage":"len mismatch decoding a %v: want %d got %d","messagePattern":"len mismatch decoding a (.+?): want (.+?) got (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"sdks/go/pkg/beam/core/graph/coder/iterable.go","lineNumber":140,"sourceCode":"\t\tdefault:\n\t\t\treturn errors.Errorf(\"unable to decode slice iterable with size: %d\", n)\n\t\t}\n\t}\n}\n\n// iterableDecoderForArray can decode from only the fixed sized and\n// multi-chunk variant of the beam iterable protocol.\n// Returns an error for other protocols (such as state backed).\nfunc iterableDecoderForArray(rt reflect.Type, decodeToElem typeDecoderFieldReflect) func(reflect.Value, io.Reader) error {\n\treturn func(ret reflect.Value, r io.Reader) error {\n\t\t// (1) Read count prefixed encoded data\n\t\tsize, err := DecodeInt32(r)\n\t\tif err != nil {\n\t\t\treturn err\n\t\t}\n\t\tn := int(size)\n\t\tif rt.Len() != n {\n\t\t\treturn errors.Errorf(\"len mismatch decoding a %v: want %d got %d\", rt, rt.Len(), n)\n\t\t}\n\t\tswitch {\n\t\tcase n >= 0:\n\t\t\tif err := decodeToIterable(ret, r, decodeToElem); err != nil {\n\t\t\t\treturn err\n\t\t\t}\n\t\t\treturn nil\n\t\tdefault:\n\t\t\treturn errors.Errorf(\"unable to decode array iterable with size: %d\", n)\n\t\t}\n\t}\n}\n\nfunc decodeToIterable(rv reflect.Value, r io.Reader, decodeTo typeDecoderFieldReflect) error {\n\tsize := rv.Len()\n\tfor i := 0; i < size; i++ {\n\t\tiv := rv.Index(i)\n\t\tif decodeTo.addr {","sourceCodeStart":122,"sourceCodeEnd":158,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/go/pkg/beam/core/graph/coder/iterable.go#L122-L158","documentation":"When decoding an array-typed iterable, the decoder reads an int32 size from the stream and compares it with the fixed length rt.Len() of the target array type; on mismatch it errors with \"len mismatch decoding a %v: want %d got %d\". The Beam array iterable protocol requires the declared element count to exactly equal the static array length.","triggerScenarios":"Decoding an iterable stream into a fixed-size array [N]T when the encoded size field is not N — e.g. the writer encoded a slice of a different length, or the element type changed between write and read.","commonSituations":"Changing a pipeline's element type from []T to [N]T (or resizing N) while replaying old data; schema drift between producer and consumer stages.","solutions":["Ensure writer and reader agree on the exact array type and length","Re-encode the data after changing array length","Use a slice instead of a fixed array if the length can vary"],"exampleFix":"// before\ntype Batch struct { Items [5]int }\n// after (if data has variable length)\ntype Batch struct { Items []int }","handlingStrategy":"validation","validationCode":"if rt.Kind() == reflect.Array {\n    return fmt.Errorf(\"fixed array coder requires exact length match; prefer a slice for variable-length data\")\n}","typeGuard":"func isSlice(t reflect.Type) bool { return t.Kind() == reflect.Slice }","tryCatchPattern":"if err != nil && strings.Contains(err.Error(), \"len mismatch decoding\") {\n    return fmt.Errorf(\"array length changed between encode and decode: %w\", err)\n}","preventionTips":["Use slices instead of fixed-size arrays for variable-length data","Never change an array length N without re-encoding existing data","Keep element type definitions in a shared package used by all stages"],"tags":["go","beam","iterable","decoding","length-mismatch"],"backgroundTag":"shape-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"}