apache/beam · error

element type is , want

Error message

element type is %v, want %v

What it means

reflectx.MakeSlice builds a []T slice from reflect.Values. Every element must have exactly type T; the library panics when an element's reflect.Type differs from the declared element type, since assigning it would be invalid.

Solutions

  1. Convert each element to the exact element type before passing (e.g. reflect.ValueOf(int64(x)) when t is int64).
  2. Ensure t matches the actual dynamic type of all values.
  3. Use a common element type consistently when constructing the values.

Example fix

// before
reflectx.MakeSlice(reflect.TypeOf(int64(0)), reflect.ValueOf(1)) // int != int64, panics
// after
reflectx.MakeSlice(reflect.TypeOf(int64(0)), reflect.ValueOf(int64(1)))
Defensive patterns

Strategy: validation

Validate before calling

for i, v := range values {
    if v.Type() != t {
        return fmt.Errorf("element %d has type %v, want %v", i, v.Type(), t)
    }
}

Type guard

func allOfType(t reflect.Type, values ...reflect.Value) bool {
    for _, v := range values {
        if v.Type() != t {
            return false
        }
    }
    return true
}

Try / catch

func safeMakeSlice(t reflect.Type, values ...reflect.Value) (out reflect.Value, err error) {
    defer func() {
        if r := recover(); r != nil {
            err = fmt.Errorf("MakeSlice failed: %v", r)
        }
    }()
    return reflectx.MakeSlice(t, values...), nil
}

Prevention

When it happens

Trigger: Calling MakeSlice(t, v1, v2, ...) where any value's Type() != t, e.g. mixing int and int64, or passing a pointer where t is a value type.

Common situations: Constructing typed slices from heterogeneous Go values where implicit conversions were assumed; test helpers (TestMakeSlice) or coder code (put) building values of slightly mismatched types.

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/9945db520cc5c73e. Report an issue: GitHub.

Appendix: source

Thrown at sdks/go/pkg/beam/core/util/reflectx/types.go:99

	default:
		return false
	}
}

// SkipPtr returns the target of a Ptr type, if a Ptr. Otherwise itself.
func SkipPtr(t reflect.Type) reflect.Type {
	if t.Kind() == reflect.Ptr {
		return t.Elem()
	}
	return t
}

// MakeSlice creates a slice of type []T with the given elements.
func MakeSlice(t reflect.Type, values ...reflect.Value) reflect.Value {
	ret := reflect.MakeSlice(reflect.SliceOf(t), len(values), len(values))
	for i, value := range values {
		if value.Type() != t {
			panic(fmt.Sprintf("element type is %v, want %v", value.Type(), t))
		}
		ret.Index(i).Set(value)
	}
	return ret
}

// UnderlyingType drops value's type by converting it to an interface and then returning ValueOf() the untyped value.
func UnderlyingType(value reflect.Value) reflect.Value {
	untyped := value.Interface()
	return reflect.ValueOf(untyped)
}

View on GitHub (pinned to 12126d8942)