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

What it means

reflectx.ToFunc4x0 converts a generic Func to a strongly-typed Func4x0 (4 inputs, 0 outputs). It panics if the underlying reflect.Type does not have exactly NumIn()==4 and NumOut()==0. Beam throws this to guarantee the fixed-arity shim's invocation contract matches the wrapped function, catching the mismatch at conversion time with the actual type in the message.

Solutions

  1. Make the function take exactly 4 parameters and return nothing.
  2. If it returns a value or error, switch to the matching converter (ToFunc4x1, ToFunc4x2, ...).
  3. Confirm the shape with reflect.TypeOf(fn).NumIn()/NumOut() before wrapping.
  4. Move the error handling inside the function body (log/emit) if a no-output signature is required by the pipeline.

Example fix

// before
f := reflectx.ToFunc4x0(func(a int, b string, c bool, d float64) error { return nil }) // 1 output, panics
// after
f := reflectx.ToFunc4x0(func(a int, b string, c bool, d float64) {
    // handle a,b,c,d without returning
})
Defensive patterns

Strategy: validation

Validate before calling

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

Type guard

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

Try / catch

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

Prevention

When it happens

Trigger: Calling reflectx.ToFunc4x0(f) where f.Type().NumIn() != 4 (e.g. 3 or 5 params) or f.Type().NumOut() != 0 (the function returns anything, including an error).

Common situations: Sink-style 4-argument functions that still return an error value, functions that gained or lost a context/emitter parameter during refactoring, or copy-pasted converter calls pointing at the wrong arity variant.

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

Appendix: source

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

}

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

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

func (c *shimFunc4x0) Call4x0(arg0, arg1, arg2, arg3 any) {
	ret := c.inner.Call([]any{arg0, arg1, arg2, arg3})
	_ = ret
	return
}

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

func MakeFunc4x0(fn any) Func4x0 {
	return ToFunc4x0(MakeFunc(fn))
}

type Func4x1 interface {
	Func
	Call4x1(any, any, any, any) any
}

type shimFunc4x1 struct {
	inner Func

View on GitHub (pinned to 12126d8942)