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

What it means

reflectx.ToFunc5x2 wraps a reflectx.Func into a Func5x2 (5 inputs, 2 outputs). It panics with this message if the func's reflect.Type does not have exactly 5 inputs and 2 outputs, since the shim returns ret[0], ret[1] directly.

Solutions

  1. Change the function to return exactly 2 values from 5 inputs.
  2. Pick the correct ToFuncN x M helper for the actual signature (e.g. ToFunc5x1 for (T, error)).
  3. If inputs are wrong (e.g. missing context or event-time), adjust the parameter list to 5 parameters.

Example fix

// before
func fn(a A, b B, c C, d D, e E) (T, error) { ... }
reflectx.ToFunc5x2(fn) // panics: 2 outputs but semantically T+error; or 3 outputs

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

Strategy: validation

Validate before calling

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

Type guard

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

Try / catch

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

Prevention

When it happens

Trigger: Calling reflectx.ToFunc5x2(c) where c.Type().NumIn() != 5 or c.Type().NumOut() != 2, e.g. a function returning a single value plus error vs two emitted values, or with a different input count.

Common situations: FlatMap-style DoFns returning (T, error) where a 5x2 (two emitted values) registration is required; signature changes during refactors; mismatch between emitted-value count and ToFunc helper chosen.

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

Appendix: source

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

}

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

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

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

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

func MakeFunc5x2(fn any) Func5x2 {
	return ToFunc5x2(MakeFunc(fn))
}

type Func5x3 interface {
	Func
	Call5x3(any, any, any, any, any) (any, any, any)
}

type shimFunc5x3 struct {
	inner Func

View on GitHub (pinned to 12126d8942)