apache/beam · error

Incompatible func type: got func %v with %v inputs and %v ou

Error message

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

What it means

reflectx.ToFunc3x4 converts a generic Func to a strongly-typed Func3x4 (3 inputs, 4 outputs). The converter checks that the underlying reflect.Type has exactly NumIn()==3 and NumOut()==4 and panics otherwise. The Beam Go SDK fails fast here because its fixed-arity shim would otherwise mis-dispatch return values during pipeline execution.

Source

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

}

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

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

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

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

func MakeFunc3x4(fn any) Func3x4 {
	return ToFunc3x4(MakeFunc(fn))
}

type Func4x0 interface {
	Func
	Call4x0(any, any, any, any)
}

type shimFunc4x0 struct {
	inner Func

View on GitHub (pinned to 12126d8942)

Solutions

  1. Adjust the function to accept exactly 3 parameters and return exactly 4 values.
  2. Pick the converter matching the actual shape (ToFunc3x3 for 3 outputs, ToFunc3x2 for 2).
  3. Validate with reflect.TypeOf(fn).NumIn()/NumOut() before calling ToFunc3x4.
  4. If 4 outputs are needed but the function lacks one, add the extra return (e.g. an emitter or boolean) at the function definition site.

Example fix

// before
f := reflectx.ToFunc3x4(func(a int, b, c string) (int, string, error) { return a, b, nil }) // 3 outputs, panics
// after
f := reflectx.ToFunc3x3(func(a int, b, c string) (int, string, error) { return a, b, nil })
Defensive patterns

Strategy: validation

Validate before calling

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

Type guard

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

Try / catch

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

Prevention

When it happens

Trigger: Calling reflectx.ToFunc3x4(f) where f.Type().NumIn() != 3 (e.g. 2 or 4 params) or f.Type().NumOut() != 4 (e.g. 2 or 3 return values, such as a function returning (T, error)).

Common situations: Functions returning (value1, value2, error) — 3 outputs, not 4 — passed to the wrong converter; an emitter parameter added to the function after the converter call was written; copy-pasted converter names not updated when a function signature changed.

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