apache/beam · error

Mismatched element type in method

Error message

Mismatched element type in method %v, parameter at index %v. Got: %v, Want: %v (from method %v). Ensure that element parameters in SDF methods have consistent types with element parameters in %v.

What it means

Element parameters of SDF methods (SplitRestriction, TruncateRestriction, ProcessElement, Size, etc.) must use consistent types across all methods. Beam compares each parameter's type against the corresponding element parameter of ProcessElement and fails when they differ.

Solutions

  1. Align the element/restriction parameter types of the failing method with ProcessElement's corresponding parameter (the message gives Got/Want types and the index).
  2. Update all SDF methods together whenever the element type changes.
  3. If using Go generics, instantiate all methods with the same type parameters.

Example fix

// before
func (fn *f) ProcessElement(ctx, r RestA, elem string, emit func(int)) {...}
func (fn *f) Size(r RestB) int {...}
// after
func (fn *f) Size(r RestA) int {...}
Defensive patterns

Strategy: validation

Validate before calling

func validateElementTypes(fn interface{}) error {
    // Beam validates at graph construction; pre-check in tests:
    if _, err := beam.TryCreateDoFn(reflect.TypeOf(fn)); err != nil {
        return err
    }
    return nil
}

Type guard

func elementTypesMatch(fn interface{}) bool {
    t := reflect.TypeOf(fn)
    pe, a := t.MethodByName("ProcessElement")
    sz, b := t.MethodByName("Size")
    return !a || !b || pe.Type.In(pe.Type.NumIn()-1) != sz.Type.In(1) // adjust for actual param layout
}

Prevention

When it happens

Trigger: Defining e.g. Size(r MyRestriction) or SplitRestriction(r OtherRestriction) while ProcessElement takes a different element/restriction type; mismatch at parameter index idx = i + startIndex for method 'name'.

Common situations: Refactoring the element type in ProcessElement but not in auxiliary methods; using generic type parameters inconsistently across methods; copying method signatures from another DoFn.

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

Appendix: source

Thrown at sdks/go/pkg/beam/core/graph/fn.go:1141

		return ctxIndex + 1
	}

	return 0
}

// validateSdfElementT validates that element types in an SDF method are
// consistent with the ProcessElement method. This method assumes that the
// first 'num' parameters starting with startIndex are the elements.
func validateSdfElementT(fn *Fn, name string, method *funcx.Fn, num int, startIndex int) error {
	// ProcessElement is the most canonical source of the element type. We can
	// processFn is valid by this point and skip unnecessary validation.
	processFn := fn.methods[processElementName]
	pos, _, _ := processFn.Inputs()

	for i := 0; i < num; i++ {
		idx := i + startIndex
		if got, want := method.Param[i+startIndex].T, processFn.Param[pos+i].T; got != want {
			err := errors.Errorf("mismatched element type in method %v, param %v. got: %v, want: %v",
				name, idx, got, want)
			return errors.SetTopLevelMsgf(err, "Mismatched element type in method %v, "+
				"parameter at index %v. Got: %v, Want: %v (from method %v). "+
				"Ensure that element parameters in SDF methods have consistent types with element parameters in %v.",
				name, idx, got, want, processElementName, processElementName)
		}
	}
	return nil
}

// validateIsWatermarkEstimating returns true if watermark estimator methods are present on the DoFn, returns
// false if they aren't, and returns an error if they are present but the function isn't an sdf and thus doesn't
// support watermark estimation
func validateIsWatermarkEstimating(fn *Fn, isSdf bool) (bool, error) {
	_, isWatermarkEstimating := fn.methods[createWatermarkEstimatorName]
	if !isSdf && isWatermarkEstimating {
		return false, errors.Errorf("watermark estimation method %v is defined on non-splittable DoFn. Watermark"+
			"estimation is only valid on splittable DoFns", createWatermarkEstimatorName)

View on GitHub (pinned to 12126d8942)