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

What it means

reflectx.ToFunc4x3 converts a generic Func to a strongly-typed Func4x3 (4 inputs, 3 outputs). The converter asserts the underlying reflect.Type has exactly NumIn()==4 and NumOut()==3 and panics otherwise. The Beam Go SDK fails fast here so the shim's fixed three-value return unpacking cannot index past the end of a shorter return slice at pipeline runtime.

Solutions

  1. Adjust the function to take exactly 4 parameters and return exactly 3 values.
  2. Switch to the converter matching the real shape (ToFunc4x2 or ToFunc4x4).
  3. Confirm in/out counts with reflect.TypeOf(fn) before wrapping.
  4. Where the signature is unstable, register via Beam's higher-level APIs so signature checking is centralized rather than hand-picked converters.

Example fix

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

Strategy: validation

Validate before calling

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

Type guard

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

Try / catch

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

Prevention

When it happens

Trigger: Calling reflectx.ToFunc4x3(f) where f.Type().NumIn() != 4 (e.g. 3 or 5 params) or f.Type().NumOut() != 3 (e.g. 2 or 4 return values).

Common situations: Multi-output functions extended with an extra return during a schema change (4 outputs), trimmed to 2 outputs, or given a wrong converter name after copy-paste; functions where an added emitter parameter pushed inputs to 5.

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

Appendix: source

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

}

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

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

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

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

func MakeFunc4x3(fn any) Func4x3 {
	return ToFunc4x3(MakeFunc(fn))
}

type Func4x4 interface {
	Func
	Call4x4(any, any, any, any) (any, any, any, any)
}

type shimFunc4x4 struct {
	inner Func

View on GitHub (pinned to 12126d8942)