apache/beam · error

element was type %T, previous additions were of type

Error message

element %d was type %T, previous additions were of type %v

What it means

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.

Solutions

  1. Ensure every element in the variadic call has the identical concrete type as previous additions
  2. Cast all elements explicitly to one type (e.g. int64(x))
  3. Split into separate TestStream configs when different types are genuinely needed
  4. Check for unintended nil or typed-nil entries in the element list

Example fix

// before (previous additions were int64)
cfg.AddElements(10, 3, 4) // int literals mismatch int64
// after
cfg.AddElements(10, int64(3), int64(4))
Defensive patterns

Strategy: type-guard

Validate before calling

for _, e := range elems { if reflect.TypeOf(e) != expectedType { return fmt.Errorf("element %T != %v", e, expectedType) } }

Type guard

func allOfType[T any](elems ...any) bool {
    var zero T
    want := reflect.TypeOf(zero)
    for _, e := range elems { if reflect.TypeOf(e) != want { return false } }
    return true
}

Try / catch

if err := cfg.AddElements(ts, elems...); err != nil {
    t.Fatalf("mixed element types: %v", err)
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Understand the failure class

Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.

Related errors


AI-assisted analysis of apache/beam@12126d8942 (2026-09-13). Data as JSON: /api/errors/bfd930342d6f28a4. Report an issue: GitHub.

Appendix: source

Thrown at sdks/go/pkg/beam/testing/teststream/teststream.go:121

}

// AddElements adds a number of elements to the stream at the specified event timestamp. Must be called with
// at least one element.
//
// On the first call, a type will be inferred from the passed in elements, which must be of all the same type.
// Type mismatches on this or subsequent calls will cause AddElements to return an error.
//
// Element types must have built-in coders in Beam.
func (c *Config) AddElements(timestamp int64, elements ...any) error {
	t := reflect.TypeOf(elements[0])
	if c.elmType == nil {
		c.elmType = typex.New(t)
	} else if c.elmType.Type() != t {
		return fmt.Errorf("element type mismatch, previous additions were of type %v, tried to add type %v", c.elmType, t)
	}
	for i, ele := range elements {
		if reflect.TypeOf(ele) != c.elmType.Type() {
			return fmt.Errorf("element %d was type %T, previous additions were of type %v", i, ele, c.elmType)
		}
	}
	newElements := []*pipepb.TestStreamPayload_TimestampedElement{}
	enc := beam.NewElementEncoder(t)
	for _, e := range elements {
		var buf bytes.Buffer
		if err := enc.Encode(e, &buf); err != nil {
			return fmt.Errorf("encoding value %v failed, got %v", e, err)
		}
		newElements = append(newElements, &pipepb.TestStreamPayload_TimestampedElement{EncodedElement: buf.Bytes(), Timestamp: timestamp})
	}
	addElementsEvent := &pipepb.TestStreamPayload_Event_AddElements{Elements: newElements}
	elementEvent := &pipepb.TestStreamPayload_Event_ElementEvent{ElementEvent: addElementsEvent}
	c.events = append(c.events, &pipepb.TestStreamPayload_Event{Event: elementEvent})
	return nil
}

// AddElementList inserts a slice of elements into the stream at the specified event timestamp. Must be called with

View on GitHub (pinned to 12126d8942)