apache/beam · error

Incompatible func type: got func

Error message

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

What it means

ToFunc1x1 adapts a generic reflectx Func into a Func1x1 (1 input, 1 output, a mapping function). It panics when the wrapped function's type does not have exactly 1 input and 1 output. This eager check prevents late failures during pipeline execution.

Solutions

  1. Adjust the wrapped function to take exactly 1 argument and return exactly 1 value.
  2. Use the matching ToFunc/MakeFuncNxM variant for the actual arity.
  3. Check the signature against the expected Func1x1 shape before wrapping.

Example fix

// before
reflectx.MakeFunc1x1(func(b []byte) (int, error) { ... }) // panics: 2 outputs
// after
reflectx.MakeFunc1x1(func(b []byte) int { ... })
Defensive patterns

Strategy: validation

Validate before calling

t := c.Type()
if t.NumIn() != 1 || t.NumOut() != 1 {
    return fmt.Errorf("func %v has %d ins/%d outs; ToFunc1x1 needs 1 in/1 out", t, t.NumIn(), t.NumOut())
}

Type guard

func isFunc1x1(c reflectx.Func) bool {
    return c.Type().NumIn() == 1 && c.Type().NumOut() == 1
}

Try / catch

func safeToFunc1x1(c reflectx.Func) (f reflectx.Func1x1) {
    defer func() {
        if r := recover(); r != nil {
            log.Fatalf("ToFunc1x1 arity mismatch: %v", r)
        }
    }()
    return reflectx.ToFunc1x1(c)
}

Prevention

When it happens

Trigger: Calling reflectx.ToFunc1x1 (or MakeFunc1x1, or beam coders' makeDecoder/makeEncoder, or makePartitionFn) with a Func whose Type().NumIn() != 1 or Type().NumOut() != 1.

Common situations: Defining a coder's encode/decode function with an extra error return; a partition function returning an int plus error; refactoring a map function to take a context parameter.

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

Appendix: source

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

}

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

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

func (c *shimFunc1x1) Call1x1(arg0 any) any {
	ret := c.inner.Call([]any{arg0})
	_ = ret
	return ret[0]
}

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

func MakeFunc1x1(fn any) Func1x1 {
	return ToFunc1x1(MakeFunc(fn))
}

type Func1x2 interface {
	Func
	Call1x2(any) (any, any)
}

type shimFunc1x2 struct {
	inner Func

View on GitHub (pinned to 12126d8942)