{"record":{"id":"0ed6edc25f2e03cf","repo":"microsoft/semantic-kernel","slug":"invalid-parameter-count-for-function-functionmeta","errorCode":null,"errorMessage":"Invalid parameter count for function {functionMetadata.Name}. {handlebarsArguments.Length} were specified but {functionMetadata.Parameters.Count} are required.","messagePattern":"Invalid parameter count for function (.+?)\\. (.+?) were specified but (.+?) are required\\.","errorType":"exception","errorClass":"KernelException","httpStatus":null,"severity":"error","filePath":"dotnet/src/Extensions/PromptTemplates.Handlebars/Helpers/KernelHelpers/KernelFunctionHelpers.cs","lineNumber":194,"sourceCode":"        {\n            var argIndex = 0;\n            var arguments = KernelHelpersUtils.ProcessArguments(handlebarsArguments, executionContext);\n            foreach (var arg in arguments)\n            {\n                var param = functionMetadata.Parameters[argIndex++];\n                if (IsExpectedParameterType(param, arg))\n                {\n                    executionContext[param.Name] = arg;\n                }\n                else\n                {\n                    throw new KernelException($\"Invalid parameter type for function {functionMetadata.Name}. Parameter {param.Name} expects type {param.ParameterType ?? (object?)param.Schema} but received {arg?.GetType().ToString() ?? \"<null>\"}.\");\n                }\n            }\n        }\n        else\n        {\n            throw new KernelException($\"Invalid parameter count for function {functionMetadata.Name}. {handlebarsArguments.Length} were specified but {functionMetadata.Parameters.Count} are required.\");\n        }\n    }\n\n    /// <summary>\n    /// Invokes an SK function and returns a typed result, if specified.\n    /// </summary>\n    private static object? InvokeKernelFunction(\n        Kernel kernel,\n        KernelFunction function,\n        KernelArguments executionContext,\n        CancellationToken cancellationToken)\n    {\n#pragma warning disable VSTHRD002 // Avoid problematic synchronous waits\n        FunctionResult result = function.InvokeAsync(kernel, executionContext, cancellationToken: cancellationToken).GetAwaiter().GetResult();\n#pragma warning restore VSTHRD002 // Avoid problematic synchronous waits\n\n        return ParseResult(result);\n    }","sourceCodeStart":176,"sourceCodeEnd":212,"githubUrl":"https://github.com/microsoft/semantic-kernel/blob/c028a0c7dc4f0814cdcbaba9d998f187a41197bf/dotnet/src/Extensions/PromptTemplates.Handlebars/Helpers/KernelHelpers/KernelFunctionHelpers.cs#L176-L212","documentation":"Thrown during positional-argument processing when the number of arguments supplied does not equal the number of parameters the function requires. The helper enforces exact arity for positional calls.","triggerScenarios":"ProcessPositionalArguments compares handlebarsArguments.Length to functionMetadata.Parameters.Count; a mismatch (too few or too many) throws before type checking.","commonSituations":"Template calls the function with the wrong number of positional arguments after a signature change, or copy/paste errors dropping or adding an argument.","solutions":["Count the function's parameters from its metadata and supply exactly that many positional arguments.","Prefer hash arguments to avoid positional-arity fragility.","Update templates whenever the function signature changes parameter count."],"exampleFix":"// before (function takes 3 params, only 2 given)\n{{Plugin-Function arg1 arg2}}\n// after\n{{Plugin-Function arg1 arg2 arg3}}","handlingStrategy":"validation","validationCode":"if (handlebarsArguments.Length != functionMetadata.Parameters.Count)\n    throw new InvalidOperationException($\"Function {functionMetadata.Name} expects {functionMetadata.Parameters.Count} args, got {handlebarsArguments.Length}.\");","typeGuard":"public static bool HasCorrectArity(KernelFunctionMetadata meta, int argCount) =>\n    argCount == meta.Parameters.Count;","tryCatchPattern":"try\n{\n    var result = await template.RenderAsync(kernel, arguments).ConfigureAwait(false);\n}\ncatch (KernelException ex) when (ex.Message.Contains(\"Invalid parameter count\"))\n{\n    _logger.LogError(ex, \"Positional arity mismatch; supply exactly the required number of args.\");\n    throw;\n}","preventionTips":["Count function parameters and supply exactly that many positional arguments.","Use hash arguments to avoid positional-arity fragility.","Update templates when function parameter counts change."],"tags":["handlebars","prompt-templates","function-invocation","arity-mismatch","positional-arguments"],"backgroundTag":null,"analyzedSha":"c028a0c7dc4f0814cdcbaba9d998f187a41197bf","analyzedAt":"2026-08-13T13:48:05.040Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}