apache/beam · error

Incompatible func type: got func

Error message

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

What it means

reflectx.ToFunc3x2 converts a generic Func to a strongly-typed Func3x2 (3 inputs, 2 outputs). It panics if the underlying reflect.Type does not have exactly NumIn()==3 and NumOut()==2. The Beam Go SDK uses this guard so reflection-based calls in the pipeline always match the shim's declared signature, failing fast with the actual type and counts in the panic message.

Solutions

  1. Change the function to have exactly 3 parameters and exactly 2 return values.
  2. For a single return value, use ToFunc3x1; for no returns, ToFunc3x0.
  3. Print reflect.TypeOf(fn) and compare NumIn/NumOut against the target 3x2 shape before wrapping.
  4. If the arity legitimately differs, register the function through the higher-level beam API (beam.ParDo with a typed DoFn) instead of manual reflectx coercion.

Example fix

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

Strategy: validation

Validate before calling

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

Type guard

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

Try / catch

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

Prevention

When it happens

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

Common situations: A function that dropped its error return after refactoring (now 1 output), a function that gained a third return, or wrapping a function whose context/emitter parameter was added or removed, changing the input arity to 2 or 4.

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

Appendix: source

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

}

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

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

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

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

func MakeFunc3x2(fn any) Func3x2 {
	return ToFunc3x2(MakeFunc(fn))
}

type Func3x3 interface {
	Func
	Call3x3(any, any, any) (any, any, any)
}

type shimFunc3x3 struct {
	inner Func

View on GitHub (pinned to 12126d8942)