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 1 outputs

What it means

reflectx.ToFunc4x1 converts a generic Func to a strongly-typed Func4x1 (4 inputs, 1 output). The converter verifies the underlying reflect.Type has exactly NumIn()==4 and NumOut()==1 and panics otherwise. The Beam SDK fails fast because the shim reads exactly one return value via reflection; an arity mismatch would corrupt results or crash the runner.

Solutions

  1. Change the function to take exactly 4 parameters and return exactly 1 value.
  2. For (value, error) or (value, bool) shapes use ToFunc4x2; for no returns use ToFunc4x0.
  3. Verify with reflect.TypeOf(fn).NumIn()/NumOut() before wrapping.
  4. If the 4th parameter is unnecessary, close over it instead of declaring it, reducing NumIn to 3 and matching a different converter.

Example fix

// before
f := reflectx.ToFunc4x1(func(a int, b, c string, d bool) (int, error) { return a, nil }) // 2 outputs, panics
// after
f := reflectx.ToFunc4x2(func(a int, b, c string, d bool) (int, error) { return a, nil })
Defensive patterns

Strategy: validation

Validate before calling

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

Type guard

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

Try / catch

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

Prevention

When it happens

Trigger: Calling reflectx.ToFunc4x1(f) where f.Type().NumIn() != 4 (e.g. 3 or 5 params) or f.Type().NumOut() != 1 (no returns, or 2+ returns like (T, error)).

Common situations: Mapping functions that also emit an error (2 outputs), functions missing a parameter after an API change, or selecting the wrong converter for a function that returns (value, bool) — which counts as 2 outputs.

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

Appendix: source

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

}

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

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

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

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

func MakeFunc4x1(fn any) Func4x1 {
	return ToFunc4x1(MakeFunc(fn))
}

type Func4x2 interface {
	Func
	Call4x2(any, any, any, any) (any, any)
}

type shimFunc4x2 struct {
	inner Func

View on GitHub (pinned to 12126d8942)