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

What it means

reflectx.ToFunc3x1 converts a generic Func to a strongly-typed Func3x1 (3 inputs, 1 output). The converter validates the underlying reflect.Type has exactly NumIn()==3 and NumOut()==1 and panics otherwise. Beam throws this to fail fast at conversion time, since a mismatched arity would break the reflection-based invocation inside the pipeline runner.

Source

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

}

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

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

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

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

func MakeFunc3x1(fn any) Func3x1 {
	return ToFunc3x1(MakeFunc(fn))
}

type Func3x2 interface {
	Func
	Call3x2(any, any, any) (any, any)
}

type shimFunc3x2 struct {
	inner Func

View on GitHub (pinned to 12126d8942)

Solutions

  1. Adjust the wrapped function to take exactly 3 parameters and return exactly 1 value.
  2. If it returns (value, error), that is 2 outputs — use ToFunc3x2 instead.
  3. If it returns nothing, use ToFunc3x0.
  4. Inspect reflect.TypeOf(fn) to confirm the actual in/out counts before choosing the converter.

Example fix

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

Strategy: validation

Validate before calling

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

Type guard

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

Try / catch

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

Prevention

When it happens

Trigger: Calling reflectx.ToFunc3x1(f) where f.Type().NumIn() != 3 (e.g. 2 or 4 params) or f.Type().NumOut() != 1 (0 returns, or 2+ returns like (T, error)).

Common situations: A mapping function that gained an extra emitted output during refactoring (now 2 returns), a function missing an emitter/context parameter, or developers accidentally invoking the wrong ToFuncNxM for a function returning (T, error) which counts as 2 outputs.

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