{"record":{"id":"baf2a8519b681943","repo":"apache/beam","slug":"element-type-mismatch-previous-additions-were-of-type-v","errorCode":null,"errorMessage":"element type mismatch, previous additions were of type %v, tried to add type %v","messagePattern":"element type mismatch, previous additions were of type (.+?), tried to add type (.+?)","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"sdks/go/pkg/beam/testing/teststream/teststream.go","lineNumber":117,"sourceCode":"// AdvanceProcessingTimeToInfinity moves the TestStream processing time to the largest possible\n// timestamp.\nfunc (c *Config) AdvanceProcessingTimeToInfinity() {\n\tc.AdvanceProcessingTime(mtime.MaxTimestamp.Milliseconds())\n}\n\n// AddElements adds a number of elements to the stream at the specified event timestamp. Must be called with\n// at least one element.\n//\n// On the first call, a type will be inferred from the passed in elements, which must be of all the same type.\n// Type mismatches on this or subsequent calls will cause AddElements to return an error.\n//\n// Element types must have built-in coders in Beam.\nfunc (c *Config) AddElements(timestamp int64, elements ...any) error {\n\tt := reflect.TypeOf(elements[0])\n\tif c.elmType == nil {\n\t\tc.elmType = typex.New(t)\n\t} else if c.elmType.Type() != t {\n\t\treturn fmt.Errorf(\"element type mismatch, previous additions were of type %v, tried to add type %v\", c.elmType, t)\n\t}\n\tfor i, ele := range elements {\n\t\tif reflect.TypeOf(ele) != c.elmType.Type() {\n\t\t\treturn fmt.Errorf(\"element %d was type %T, previous additions were of type %v\", i, ele, c.elmType)\n\t\t}\n\t}\n\tnewElements := []*pipepb.TestStreamPayload_TimestampedElement{}\n\tenc := beam.NewElementEncoder(t)\n\tfor _, e := range elements {\n\t\tvar buf bytes.Buffer\n\t\tif err := enc.Encode(e, &buf); err != nil {\n\t\t\treturn fmt.Errorf(\"encoding value %v failed, got %v\", e, err)\n\t\t}\n\t\tnewElements = append(newElements, &pipepb.TestStreamPayload_TimestampedElement{EncodedElement: buf.Bytes(), Timestamp: timestamp})\n\t}\n\taddElementsEvent := &pipepb.TestStreamPayload_Event_AddElements{Elements: newElements}\n\telementEvent := &pipepb.TestStreamPayload_Event_ElementEvent{ElementEvent: addElementsEvent}\n\tc.events = append(c.events, &pipepb.TestStreamPayload_Event{Event: elementEvent})","sourceCodeStart":99,"sourceCodeEnd":135,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/go/pkg/beam/testing/teststream/teststream.go#L99-L135","documentation":"A TestStream PCollection must have a single element type, so the first AddElements call fixes the config's element type. AddElements rejects any subsequent call whose first element's reflect.Type differs from the recorded type.","triggerScenarios":"Calling Config.AddElements twice with elements of different Go types (e.g. first []any{1,2} (int) then string elements) on the same TestStream config.","commonSituations":"Reusing one TestStream config for multiple PCollections of different types, or passing untyped literals that resolve to different concrete types (int vs int64).","solutions":["Use a separate TestStream config for each element type","Make all AddElements calls use the same concrete type (e.g. consistently int64)","Convert elements to a common type before adding","Verify element types match what the pipeline's beam.Create/TestStream expects"],"exampleFix":"// before\ncfg.AddElements(0, 1, 2)      // int\ncfg.AddElements(10, \"three\")  // mismatch\n// after\ncfg.AddElements(0, int64(1), int64(2))\ncfg.AddElements(10, int64(3))","handlingStrategy":"type-guard","validationCode":"if firstType == nil { firstType = reflect.TypeOf(elements[0]) } else if reflect.TypeOf(elements[0]) != firstType { /* handle mismatch */ }","typeGuard":"func sameType(prev reflect.Type, elems ...any) bool {\n    if len(elems) == 0 { return true }\n    return prev == reflect.TypeOf(elems[0])\n}","tryCatchPattern":"if err := cfg.AddElements(ts, elems...); err != nil {\n    t.Fatalf(\"element type mismatch: %v\", err)\n}","preventionTips":["Use one TestStream config per element type","Cast literals explicitly (int64(x)) to avoid int/int64 drift","Define typed helper wrappers around AddElements","Document the config's element type in test setup"],"tags":["go","testing","teststream","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-20T03:17:13.778Z"}