microsoft/semantic-kernel · error · KernelException

Invalid parameter type for function

Error message

Invalid parameter type for function {functionMetadata.Name}. Parameter {param.Name} expects type {param.ParameterType ?? (object?)param.Schema} but received {arg?.GetType().ToString() ?? "<null>"}.

What it means

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.

Solutions

  1. Reorder or convert the positional arguments so each matches its parameter's declared type.
  2. Pre-coerce values to the correct type before they reach the template helper.
  3. Use hash arguments with explicit parameter names for clarity and type safety.

Example fix

// before (first param expects bool)
{{Plugin-Function "true"}}
// after
{{Plugin-Function true}}
Defensive patterns

Strategy: validation

Validate before calling

if (handlebarsArguments.Length != functionMetadata.Parameters.Count)
    throw new InvalidOperationException("Argument count mismatch; cannot validate types.");
for (int i = 0; i < handlebarsArguments.Length; i++)
{
    if (!IsExpectedParameterType(functionMetadata.Parameters[i], arguments[i]))
        throw new InvalidOperationException($"Positional arg {i} type mismatch for {functionMetadata.Name}.");
}

Type guard

public static bool PositionalArgsMatchTypes(KernelFunctionMetadata meta, IList<object?> args)
{
    if (args.Count != meta.Parameters.Count) return false;
    return meta.Parameters.Zip(args).All(pair =>
        pair.First.ParameterType is null || (pair.Second is not null && pair.First.ParameterType.IsAssignableFrom(pair.Second.GetType())));
}

Try / catch

try
{
    var result = await template.RenderAsync(kernel, arguments).ConfigureAwait(false);
}
catch (KernelException ex) when (ex.Message.Contains("Invalid parameter type"))
{
    _logger.LogError(ex, "Positional argument type mismatch; convert the value.");
    throw;
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of microsoft/semantic-kernel@c028a0c7dc (2026-08-13). Data as JSON: /api/errors/23eaca5400490318. Report an issue: GitHub.

Appendix: source

Thrown at dotnet/src/Extensions/PromptTemplates.Handlebars/Helpers/KernelHelpers/KernelFunctionHelpers.cs:188

    /// <exception cref="KernelException">Thrown when a required parameter is missing.</exception>
    private static void ProcessPositionalArguments(KernelFunctionMetadata functionMetadata, KernelArguments executionContext, Arguments handlebarsArguments)
    {
        var requiredParameters = functionMetadata.Parameters.Where(p => p.IsRequired).ToList();

        if (requiredParameters.Count <= handlebarsArguments.Length && handlebarsArguments.Length <= functionMetadata.Parameters.Count)
        {
            var argIndex = 0;
            var arguments = KernelHelpersUtils.ProcessArguments(handlebarsArguments, executionContext);
            foreach (var arg in arguments)
            {
                var param = functionMetadata.Parameters[argIndex++];
                if (IsExpectedParameterType(param, arg))
                {
                    executionContext[param.Name] = arg;
                }
                else
                {
                    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>"}.");
                }
            }
        }
        else
        {
            throw new KernelException($"Invalid parameter count for function {functionMetadata.Name}. {handlebarsArguments.Length} were specified but {functionMetadata.Parameters.Count} are required.");
        }
    }

    /// <summary>
    /// Invokes an SK function and returns a typed result, if specified.
    /// </summary>
    private static object? InvokeKernelFunction(
        Kernel kernel,
        KernelFunction function,
        KernelArguments executionContext,
        CancellationToken cancellationToken)
    {

View on GitHub (pinned to c028a0c7dc)