apache/beam · error

Incompatible func type: got func

Error message

Incompatible func type: got func %v with %v inputs and %v outputs, want 4 inputs and 2 outputs

What it means

reflectx.ToFunc4x2 converts a generic Func to a strongly-typed Func4x2 (4 inputs, 2 outputs). It panics when the underlying reflect.Type does not have exactly NumIn()==4 and NumOut()==2. Beam throws this at conversion time so the fixed-arity shim's two-value return handling matches the wrapped function, printing the actual func type and counts in the panic message.

Solutions

  1. Make the function take exactly 4 parameters and return exactly 2 values.
  2. Use ToFunc4x1 for single-output or ToFunc4x3 for three-output functions.
  3. Check reflect.TypeOf(fn).NumIn()/NumOut() before coercing.
  4. Prefer beam's typed DoFn/Func registration when arity keeps drifting, so Beam derives the shim instead of manual converters.

Example fix

// before
f := reflectx.ToFunc4x2(func(a int, b string, c bool, d float64) int { return a }) // 1 output, panics
// after
f := reflectx.ToFunc4x2(func(a int, b string, c bool, d float64) (int, bool) { return a, c })
Defensive patterns

Strategy: validation

Validate before calling

t := reflect.TypeOf(fn)
if t.NumIn() != 4 || t.NumOut() != 2 {
    panic(fmt.Sprintf("expected 4-in/2-out func, got %v (%d in, %d out)", t, t.NumIn(), t.NumOut()))
}
f := reflectx.ToFunc4x2(f)

Type guard

func isFunc4x2(fn any) bool {
    t := reflect.TypeOf(fn)
    return t != nil && t.Kind() == reflect.Func && t.NumIn() == 4 && t.NumOut() == 2
}

Try / catch

func safeToFunc4x2(f reflectx.Func) (out reflectx.Func4x2, err error) {
    defer func() { if r := recover(); r != nil { err = fmt.Errorf("ToFunc4x2: %v", r) } }()
    out = reflectx.ToFunc4x2(f)
    return
}

Prevention

When it happens

Trigger: Calling reflectx.ToFunc4x2(f) where f.Type().NumIn() != 4 (e.g. 3 or 5 params) or f.Type().NumOut() != 2 (1 return, or 3+ returns).

Common situations: A function that dropped its error/bool return (now 1 output) or gained a third output; a helper parameter removed from the function signature after the converter call was written; wrong converter chosen for a simple single-return mapper.

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

Appendix: source

Thrown at sdks/go/pkg/beam/core/util/reflectx/calls.go:967

}

func (c *shimFunc4x2) Type() reflect.Type {
	return c.inner.Type()
}

func (c *shimFunc4x2) Call(args []any) []any {
	return c.inner.Call(args)
}

func (c *shimFunc4x2) Call4x2(arg0, arg1, arg2, arg3 any) (any, any) {
	ret := c.inner.Call([]any{arg0, arg1, arg2, arg3})
	_ = ret
	return ret[0], ret[1]
}

func ToFunc4x2(c Func) Func4x2 {
	if c.Type().NumIn() != 4 || c.Type().NumOut() != 2 {
		panic(fmt.Sprintf("Incompatible func type: got func %v with %v inputs and %v outputs, want 4 inputs and 2 outputs", c.Type(), c.Type().NumIn(), c.Type().NumOut()))
	}
	if sc, ok := c.(Func4x2); ok {
		return sc
	}
	return &shimFunc4x2{inner: c}
}

func MakeFunc4x2(fn any) Func4x2 {
	return ToFunc4x2(MakeFunc(fn))
}

type Func4x3 interface {
	Func
	Call4x3(any, any, any, any) (any, any, any)
}

type shimFunc4x3 struct {
	inner Func

View on GitHub (pinned to 12126d8942)