apache/beam · error

Incompatible func type: got func

Error message

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

What it means

reflectx.ToFunc5x4 wraps a reflectx.Func into a Func5x4 (5 inputs, 4 outputs). It panics with this message unless the func's reflect.Type has exactly 5 inputs and 4 outputs, because the shim unpacks four return values positionally.

Solutions

  1. Make the function take 5 inputs and return exactly 4 values.
  2. Choose the ToFuncN x M wrapper matching the actual signature.
  3. Confirm the Beam registration (beam.ParDo / TryParDo) expects a 5x4 signature and adjust whichever side is wrong.

Example fix

// before
func fn(a A, b B, c C, d D, e E) (T, U, V) { ... }
reflectx.ToFunc5x4(fn) // panics: 3 outputs

// after
func fn(a A, b B, c C, d D, e E) (Q, T, U, V) { ... }
reflectx.ToFunc5x4(fn)
Defensive patterns

Strategy: validation

Validate before calling

fn := reflect.TypeOf(fnAny)
if fn.Kind() == reflect.Func && fn.NumIn() == 5 && fn.NumOut() == 4 {
    _ = reflectx.ToFunc5x4(fnAny)
}

Type guard

func isFunc5x4(f interface{}) bool {
    t := reflect.TypeOf(f)
    return t != nil && t.Kind() == reflect.Func && t.NumIn() == 5 && t.NumOut() == 4
}

Try / catch

func safeToFunc5x4(c reflectx.Func) (f reflectx.Func5x4, err error) {
    defer func() {
        if r := recover(); r != nil {
            err = fmt.Errorf("ToFunc5x4 failed: %v", r)
        }
    }()
    return reflectx.ToFunc5x4(c), nil
}

Prevention

When it happens

Trigger: Calling reflectx.ToFunc5x4(c) where c.Type().NumIn() != 5 or c.Type().NumOut() != 4 — e.g. a multi-output DoFn emitting 3 values plus error (4 outputs but intended differently) or 4 values from the wrong input count.

Common situations: Complex fan-out DoFns whose output tuple grew/shrank during development; registering with the wrong fixed-arity wrapper; Go reflection surface mismatch after switching between func and DoFn struct styles.

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

Appendix: source

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

}

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

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

func (c *shimFunc5x4) Call5x4(arg0, arg1, arg2, arg3, arg4 any) (any, any, any, any) {
	ret := c.inner.Call([]any{arg0, arg1, arg2, arg3, arg4})
	_ = ret
	return ret[0], ret[1], ret[2], ret[3]
}

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

func MakeFunc5x4(fn any) Func5x4 {
	return ToFunc5x4(MakeFunc(fn))
}

type Func6x0 interface {
	Func
	Call6x0(any, any, any, any, any, any)
}

type shimFunc6x0 struct {
	inner Func

View on GitHub (pinned to 12126d8942)