{"record":{"id":"7296d2ef1a4feead","repo":"apache/beam","slug":"incompatible-func-type-got-func-v-with-v-inputs-and-v-7296d2","errorCode":null,"errorMessage":"Incompatible func type: got func %v with %v inputs and %v outputs, want 2 inputs and 4 outputs","messagePattern":"Incompatible func type: got func (.+?) with (.+?) inputs and (.+?) outputs, want 2 inputs and 4 outputs","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"sdks/go/pkg/beam/core/util/reflectx/calls.go","lineNumber":639,"sourceCode":"}\n\nfunc (c *shimFunc2x4) Type() reflect.Type {\n\treturn c.inner.Type()\n}\n\nfunc (c *shimFunc2x4) Call(args []any) []any {\n\treturn c.inner.Call(args)\n}\n\nfunc (c *shimFunc2x4) Call2x4(arg0, arg1 any) (any, any, any, any) {\n\tret := c.inner.Call([]any{arg0, arg1})\n\t_ = ret\n\treturn ret[0], ret[1], ret[2], ret[3]\n}\n\nfunc ToFunc2x4(c Func) Func2x4 {\n\tif c.Type().NumIn() != 2 || c.Type().NumOut() != 4 {\n\t\tpanic(fmt.Sprintf(\"Incompatible func type: got func %v with %v inputs and %v outputs, want 2 inputs and 4 outputs\", c.Type(), c.Type().NumIn(), c.Type().NumOut()))\n\t}\n\tif sc, ok := c.(Func2x4); ok {\n\t\treturn sc\n\t}\n\treturn &shimFunc2x4{inner: c}\n}\n\nfunc MakeFunc2x4(fn any) Func2x4 {\n\treturn ToFunc2x4(MakeFunc(fn))\n}\n\ntype Func3x0 interface {\n\tFunc\n\tCall3x0(any, any, any)\n}\n\ntype shimFunc3x0 struct {\n\tinner Func","sourceCodeStart":621,"sourceCodeEnd":657,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/go/pkg/beam/core/util/reflectx/calls.go#L621-L657","documentation":"reflectx.ToFunc2x4 converts a generic Func wrapper to a strongly-typed Func2x4 (2 inputs, 4 outputs). Before wrapping, it verifies the underlying reflect.Type has exactly NumIn()==2 and NumOut()==4; otherwise it panics with this message. The Beam Go SDK throws it to fail fast at pipeline-construction time rather than produce a function signature that later explodes when invoked via reflection. The panic message includes the actual func type plus its input and output counts so the mismatch is immediately diagnosable.","triggerScenarios":"Calling reflectx.ToFunc2x4(f) where f.Func.Type().NumIn() != 2 or f.Func.Type().NumOut() != 4 — e.g. passing a function with 1, 3, or more parameters, or returning 2 or 3 values instead of 4.","commonSituations":"Registering a DoFn/emit function whose signature drifted after refactoring (an extra context or error parameter added), wrapping a function that returns only (T, error) instead of 4 values, or Beam's internal signature selection picking a different arity than the user's function because beam.ParDo inferred the wrong registration.","solutions":["Print f.Type() (or fn.TypeOf) and change the wrapped function to have exactly 2 input parameters and 4 return values.","If the function legitimately has a different arity, call the matching ToFuncNxM converter (ToFunc3x1, ToFunc2x2, ...) instead.","If you only have a Func and need flexibility, keep using the generic Func and let Beam's dispatcher invoke it rather than coercing to Func2x4.","Check for accidental closure wrapping: a helper that captured extra arguments still counts them in the signature only if declared as parameters; adjust the wrapper to declare exactly 2 params."],"exampleFix":"// before\nf := reflectx.ToFunc2x4(fn) // fn: func(a int, b string) (int, string, error) — 2 out, panics\n// after\nf := reflectx.ToFunc2x4(func(a int, b string) (int, string, bool, error) {\n    return a, b, true, nil\n})","handlingStrategy":"validation","validationCode":"t := reflect.TypeOf(fn)\nif t.NumIn() != 2 || t.NumOut() != 4 {\n    panic(fmt.Sprintf(\"expected 2-in/4-out func, got %v (%d in, %d out)\", t, t.NumIn(), t.NumOut()))\n}\nf := reflectx.ToFunc2x4(f)","typeGuard":"func isFunc2x4(fn any) bool {\n    t := reflect.TypeOf(fn)\n    return t != nil && t.Kind() == reflect.Func && t.NumIn() == 2 && t.NumOut() == 4\n}","tryCatchPattern":"// Panics are not recoverable via try/catch in Go; use defer/recover at registration boundaries:\nfunc safeToFunc2x4(f reflectx.Func) (out reflectx.Func2x4, err error) {\n    defer func() { if r := recover(); r != nil { err = fmt.Errorf(\"ToFunc2x4: %v\", r) } }()\n    out = reflectx.ToFunc2x4(f)\n    return\n}","preventionTips":["Write a unit test asserting reflect.TypeOf(fn).NumIn()/NumOut() for every function registered with a fixed-arity converter.","Derive converters from the function literal passed inline so signature drift is caught at compile time where possible.","Never add/remove parameters or return values to Beam-registered functions without updating the ToFuncNxM call next to it.","Remember (T, error) counts as 2 outputs; a bare error return counts as 1."],"tags":["go","apache-beam","reflection","panic","function-signature"],"backgroundTag":"type-mismatch","analyzedSha":"12126d8942aaf848030c478b4c6a28c6af861c66","analyzedAt":"2026-09-13T01:50:10.254Z","contentChangedAt":"2026-09-13T01:50:10.254Z","schemaVersion":2},"datasetVersion":"2026-09-14T11:17:12.474Z"}