microsoft/semantic-kernel · error · KernelException

Invalid parameter count for function {functionMetadata.Name}

Error message

Invalid parameter count for function {functionMetadata.Name}. {handlebarsArguments.Length} were specified but {functionMetadata.Parameters.Count} are required.

What it means

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.

Source

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

        {
            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)
    {
#pragma warning disable VSTHRD002 // Avoid problematic synchronous waits
        FunctionResult result = function.InvokeAsync(kernel, executionContext, cancellationToken: cancellationToken).GetAwaiter().GetResult();
#pragma warning restore VSTHRD002 // Avoid problematic synchronous waits

        return ParseResult(result);
    }

View on GitHub (pinned to c028a0c7dc)

Solutions

  1. Count the function's parameters from its metadata and supply exactly that many positional arguments.
  2. Prefer hash arguments to avoid positional-arity fragility.
  3. Update templates whenever the function signature changes parameter count.

Example fix

// before (function takes 3 params, only 2 given)
{{Plugin-Function arg1 arg2}}
// after
{{Plugin-Function arg1 arg2 arg3}}
Defensive patterns

Strategy: validation

Validate before calling

if (handlebarsArguments.Length != functionMetadata.Parameters.Count)
    throw new InvalidOperationException($"Function {functionMetadata.Name} expects {functionMetadata.Parameters.Count} args, got {handlebarsArguments.Length}.");

Type guard

public static bool HasCorrectArity(KernelFunctionMetadata meta, int argCount) =>
    argCount == meta.Parameters.Count;

Try / catch

try
{
    var result = await template.RenderAsync(kernel, arguments).ConfigureAwait(false);
}
catch (KernelException ex) when (ex.Message.Contains("Invalid parameter count"))
{
    _logger.LogError(ex, "Positional arity mismatch; supply exactly the required number of args.");
    throw;
}

Prevention

When it happens

Trigger: ProcessPositionalArguments compares handlebarsArguments.Length to functionMetadata.Parameters.Count; a mismatch (too few or too many) throws before type checking.

Common situations: Template calls the function with the wrong number of positional arguments after a signature change, or copy/paste errors dropping or adding an argument.

Related errors


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