{"record":{"id":"8dbe2aad5fc8f70a","repo":"apache/beam","slug":"may-only-have-a-single-context-context-parameter-and-it-must","errorCode":null,"errorMessage":"may only have a single context.Context parameter and it must be the first parameter","messagePattern":"may only have a single context\\.Context parameter and it must be the first parameter","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"sdks/go/pkg/beam/core/funcx/fn.go","lineNumber":529,"sourceCode":"\tvar err error\n\t// Validate the parameter ordering.\n\tfor i, p := range u.Param {\n\t\tif paramState, err = nextParamState(paramState, p.Kind); err != nil {\n\t\t\treturn errors.WithContextf(err, \"validating parameter %d for %s\", i, u.Fn.Name())\n\t\t}\n\t}\n\t// Validate the return value ordering.\n\tretState := rsStart\n\tfor i, r := range u.Ret {\n\t\tif retState, err = nextRetState(retState, r.Kind); err != nil {\n\t\t\treturn errors.WithContextf(err, \"validating return value %d for %s\", i, u.Fn.Name())\n\t\t}\n\t}\n\treturn nil\n}\n\nvar (\n\terrContextParam                      = errors.New(\"may only have a single context.Context parameter and it must be the first parameter\")\n\terrPaneParamPrecedence               = errors.New(\"may only have a single PaneInfo parameter and it must precede the WindowParam, EventTime and main input parameter\")\n\terrWindowParamPrecedence             = errors.New(\"may only have a single Window parameter and it must precede the EventTime and main input parameter\")\n\terrEventTimeParamPrecedence          = errors.New(\"may only have a single beam.EventTime parameter and it must precede the main input parameter\")\n\terrWatermarkEstimatorParamPrecedence = errors.New(\"may only have a single sdf.WatermarkEstimator parameter and it must precede the main input parameter\")\n\terrReflectTypePrecedence             = errors.New(\"may only have a single reflect.Type parameter and it must precede the main input parameter\")\n\terrRTrackerPrecedence                = errors.New(\"may only have a single sdf.RTracker parameter and it must precede the main input parameter\")\n\terrBundleFinalizationPrecedence      = errors.New(\"may only have a single BundleFinalization parameter and it must precede the main input parameter\")\n\terrStateProviderPrecedence           = errors.New(\"may only have a single state.Provider parameter and it must precede the main input parameter\")\n\terrTimerProviderPrecedence           = errors.New(\"may only have a single timer.Provider parameter and it must precede the main input parameter\")\n\terrInputPrecedence                   = errors.New(\"inputs parameters must precede emit function parameters\")\n)\n\ntype paramState int\n\nconst (\n\tpsStart paramState = iota\n\tpsContext\n\tpsPane","sourceCodeStart":511,"sourceCodeEnd":547,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/go/pkg/beam/core/funcx/fn.go#L511-L547","documentation":"funcx validates user DoFn signatures via reflection. errContextParam is returned by nextParamState when a context.Context parameter appears anywhere but the first position (or more than once), since Go reflection cannot disambiguate multiple contexts and the convention requires it first.","triggerScenarios":"Registering a DoFn/combineFn method like func(string, context.Context) — a context after main input — via beam.ParDo/beam.CombinePerKey/TestNew.","commonSituations":"Adding a context parameter later for cancellation/tracing and appending it at the end of the signature; test fn_test.go exercises exactly this case.","solutions":["Move context.Context to be the first parameter of the function.","Remove duplicate context.Context parameters, keeping only one.","If the parameter is not really a context, change its type so it isn't detected as context.Context."],"exampleFix":"// before\nfunc(item string, ctx context.Context) { ... }\n// after\nfunc(ctx context.Context, item string) { ... }","handlingStrategy":"validation","validationCode":"t := reflect.TypeOf(fn)\nfor i, p := range paramTypes(t) {\n\tif p == contextType && i != 0 {\n\t\treturn errors.New(\"context.Context must be the first parameter\")\n\t}\n}","typeGuard":null,"tryCatchPattern":"if _, err := funcx.New(reflect.ValueOf(fn)); err != nil {\n\treturn fmt.Errorf(\"invalid DoFn signature: %w\", err)\n}","preventionTips":["Always declare context.Context as the first parameter of Beam Go DoFns.","Have at most one context.Context per function.","Run funcx.TestNew-style signature tests in CI for custom DoFns."],"tags":["go","apache-beam","function-signature","reflection"],"backgroundTag":"invalid-argument-value","analyzedSha":"12126d8942aaf848030c478b4c6a28c6af861c66","analyzedAt":"2026-09-13T01:50:10.254Z","contentChangedAt":"2026-09-13T01:50:10.254Z","schemaVersion":2},"datasetVersion":"2026-09-14T16:17:12.679Z"}