{"record":{"id":"23eaca5400490318","repo":"microsoft/semantic-kernel","slug":"invalid-parameter-type-for-function-functionmetad","errorCode":null,"errorMessage":"Invalid parameter type for function {functionMetadata.Name}. Parameter {param.Name} expects type {param.ParameterType ?? (object?)param.Schema} but received {arg?.GetType().ToString() ?? \"<null>\"}.","messagePattern":"Invalid parameter type for function (.+?)\\. Parameter (.+?) expects type (.+?) but received (.+?)\\.","errorType":"exception","errorClass":"KernelException","httpStatus":null,"severity":"error","filePath":"dotnet/src/Extensions/PromptTemplates.Handlebars/Helpers/KernelHelpers/KernelFunctionHelpers.cs","lineNumber":188,"sourceCode":"    /// <exception cref=\"KernelException\">Thrown when a required parameter is missing.</exception>\n    private static void ProcessPositionalArguments(KernelFunctionMetadata functionMetadata, KernelArguments executionContext, Arguments handlebarsArguments)\n    {\n        var requiredParameters = functionMetadata.Parameters.Where(p => p.IsRequired).ToList();\n\n        if (requiredParameters.Count <= handlebarsArguments.Length && handlebarsArguments.Length <= functionMetadata.Parameters.Count)\n        {\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    {","sourceCodeStart":170,"sourceCodeEnd":206,"githubUrl":"https://github.com/microsoft/semantic-kernel/blob/c028a0c7dc4f0814cdcbaba9d998f187a41197bf/dotnet/src/Extensions/PromptTemplates.Handlebars/Helpers/KernelHelpers/KernelFunctionHelpers.cs#L170-L206","documentation":"Thrown during positional-argument processing when an argument at a given position does not match the expected type of the corresponding parameter. Each positional argument is checked against the function metadata in order.","triggerScenarios":"ProcessPositionalArguments iterates the positional arguments list and checks IsExpectedParameterType(param, arg) for each; a type mismatch at any index throws with the parameter name and the received type.","commonSituations":"Calling a function helper with positional arguments where a value's runtime type differs from the declared parameter type, e.g., passing a string where a bool is expected, or a null for a value type.","solutions":["Reorder or convert the positional arguments so each matches its parameter's declared type.","Pre-coerce values to the correct type before they reach the template helper.","Use hash arguments with explicit parameter names for clarity and type safety."],"exampleFix":"// before (first param expects bool)\n{{Plugin-Function \"true\"}}\n// after\n{{Plugin-Function true}}","handlingStrategy":"validation","validationCode":"if (handlebarsArguments.Length != functionMetadata.Parameters.Count)\n    throw new InvalidOperationException(\"Argument count mismatch; cannot validate types.\");\nfor (int i = 0; i < handlebarsArguments.Length; i++)\n{\n    if (!IsExpectedParameterType(functionMetadata.Parameters[i], arguments[i]))\n        throw new InvalidOperationException($\"Positional arg {i} type mismatch for {functionMetadata.Name}.\");\n}","typeGuard":"public static bool PositionalArgsMatchTypes(KernelFunctionMetadata meta, IList<object?> args)\n{\n    if (args.Count != meta.Parameters.Count) return false;\n    return meta.Parameters.Zip(args).All(pair =>\n        pair.First.ParameterType is null || (pair.Second is not null && pair.First.ParameterType.IsAssignableFrom(pair.Second.GetType())));\n}","tryCatchPattern":"try\n{\n    var result = await template.RenderAsync(kernel, arguments).ConfigureAwait(false);\n}\ncatch (KernelException ex) when (ex.Message.Contains(\"Invalid parameter type\"))\n{\n    _logger.LogError(ex, \"Positional argument type mismatch; convert the value.\");\n    throw;\n}","preventionTips":["Coerce positional argument values to the declared parameter types before rendering.","Prefer hash arguments for type clarity.","Update templates when parameter types change."],"tags":["handlebars","prompt-templates","function-invocation","type-mismatch","positional-arguments"],"backgroundTag":null,"analyzedSha":"c028a0c7dc4f0814cdcbaba9d998f187a41197bf","analyzedAt":"2026-08-13T13:48:05.040Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}