{"record":{"id":"bfd930342d6f28a4","repo":"apache/beam","slug":"element-d-was-type-t-previous-additions-were-of-type-v","errorCode":null,"errorMessage":"element %d was type %T, previous additions were of type %v","messagePattern":"element (.+?) was type %T, previous additions were of type (.+?)","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"sdks/go/pkg/beam/testing/teststream/teststream.go","lineNumber":121,"sourceCode":"}\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})\n\treturn nil\n}\n\n// AddElementList inserts a slice of elements into the stream at the specified event timestamp. Must be called with","sourceCodeStart":103,"sourceCodeEnd":139,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/go/pkg/beam/testing/teststream/teststream.go#L103-L139","documentation":"After the first element fixes the TestStream's element type, AddElements checks every individual element against that recorded type. Any element whose concrete reflect.Type differs from c.elmType produces this per-index error.","triggerScenarios":"Calling AddElements(ts, e1, e2, ...) where the first call on the config set the type but a later element in the variadic list has a different Go type, e.g. AddElements(0, 1, int64(2)) after the type was fixed to int.","commonSituations":"Mixed-type slices passed as ...any, integer literals defaulting to int while other elements are int64, or accidentally including a nil or wrapper value in the list.","solutions":["Ensure every element in the variadic call has the identical concrete type as previous additions","Cast all elements explicitly to one type (e.g. int64(x))","Split into separate TestStream configs when different types are genuinely needed","Check for unintended nil or typed-nil entries in the element list"],"exampleFix":"// before (previous additions were int64)\ncfg.AddElements(10, 3, 4) // int literals mismatch int64\n// after\ncfg.AddElements(10, int64(3), int64(4))","handlingStrategy":"type-guard","validationCode":"for _, e := range elems { if reflect.TypeOf(e) != expectedType { return fmt.Errorf(\"element %T != %v\", e, expectedType) } }","typeGuard":"func allOfType[T any](elems ...any) bool {\n    var zero T\n    want := reflect.TypeOf(zero)\n    for _, e := range elems { if reflect.TypeOf(e) != want { return false } }\n    return true\n}","tryCatchPattern":"if err := cfg.AddElements(ts, elems...); err != nil {\n    t.Fatalf(\"mixed element types: %v\", err)\n}","preventionTips":["Avoid passing heterogeneous []any slices","Cast every literal to the target type explicitly","Filter out nils before calling AddElements","Use generics or typed helper functions to enforce element type at compile time"],"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"}