microsoft/semantic-kernel · error · ArgumentException

{nameof(executionParameters.OperationSelectionPredicate)} an

Error message

{nameof(executionParameters.OperationSelectionPredicate)} and {nameof(executionParameters.OperationsToExclude)} cannot be used together.

What it means

SelectOperations refuses an OpenApiFunctionExecutionParameters instance that sets BOTH OperationSelectionPredicate and a non-empty OperationsToExclude. They are mutually exclusive selection mechanisms (a custom predicate vs. an exclusion list), and both are marked obsolete (the surrounding #pragma disables CS0618). The parser will not guess which wins, so it throws ArgumentException at plugin-creation time.

Source

Thrown at dotnet/src/Functions/Functions.OpenApi/OpenApiKernelPluginFactory.cs:456

        }

        logger.LogInformation("""Operation name "{OperationId}" converted to "{Result}" to comply with SK Function name requirements. Use "{Result}" when invoking function.""", operationId, result, result);

        return result;
    }

    /// <summary>
    /// Selects operations to parse and import.
    /// </summary>
    /// <param name="context">Operation selection context.</param>
    /// <param name="executionParameters">Execution parameters.</param>
    /// <returns>True if the operation should be selected; otherwise, false.</returns>
    private static bool SelectOperations(OperationSelectionPredicateContext context, OpenApiFunctionExecutionParameters? executionParameters)
    {
#pragma warning disable CS0618 // Type or member is obsolete
        if (executionParameters?.OperationSelectionPredicate is not null && executionParameters?.OperationsToExclude is { Count: > 0 })
        {
            throw new ArgumentException($"{nameof(executionParameters.OperationSelectionPredicate)} and {nameof(executionParameters.OperationsToExclude)} cannot be used together.");
        }

        if (executionParameters?.OperationSelectionPredicate is { } predicate)
        {
            return predicate(context);
        }

        return !executionParameters?.OperationsToExclude.Contains(context.Id ?? string.Empty) ?? true;
#pragma warning restore CS0618 // Type or member is obsolete
    }

    /// <summary>
    /// Converts the parameter type to a C# <see cref="Type"/> object.
    /// </summary>
    /// <param name="parameter">The REST API parameter.</param>
    private static Type? ConvertParameterDataType(RestApiParameter parameter)
    {
        return parameter.Type switch

View on GitHub (pinned to c028a0c7dc)

Solutions

  1. Keep only ONE selection mechanism: if you have a predicate, clear OperationsToExclude (set to null/empty); if you only need to exclude, remove the predicate.
  2. Prefer OperationSelectionPredicate and fold the exclusion list into it (e.g. predicate returns false for excluded ids), since OperationsToExclude is obsolete.
  3. Re-run plugin creation after removing one of the two to confirm the exception is gone.

Example fix

// before
var execParams = new OpenApiFunctionExecutionParameters
{
    OperationsToExclude = new[] { "deprecatedOp" },
    OperationSelectionPredicate = ctx => ctx.Id != "secretOp",
};
// after - single mechanism, exclusion folded into the predicate
var execParams = new OpenApiFunctionExecutionParameters
{
    OperationSelectionPredicate = ctx =>
        ctx.Id != "secretOp" && ctx.Id != "deprecatedOp",
};
Defensive patterns

Strategy: validation

Validate before calling

static void ValidateExecParams(OpenApiFunctionExecutionParameters p)
{
#pragma warning disable CS0618
    bool hasPredicate = p?.OperationSelectionPredicate is not null;
    bool hasExclude  = p?.OperationsToExclude is { Count: > 0 };
#pragma warning restore CS0618
    if (hasPredicate && hasExclude)
        throw new ArgumentException(
            "Set either OperationSelectionPredicate or OperationsToExclude, not both.");
}

Try / catch

try
{
    var plugin = await kernel.CreatePluginFromOpenApiAsync("api", spec, execParams);
}
catch (ArgumentException ex) when (ex.Message.Contains("cannot be used together"))
{
    // clear one of the two fields on execParams and retry
}

Prevention

When it happens

Trigger: Constructing OpenApiFunctionExecutionParameters with OperationSelectionPredicate = somePredicate AND OperationsToExclude = { "op1", ... }, then passing it to OpenApiKernelPluginFactory.CreateFromOpenApiAsync / KernelPluginFactory.CreateFromOpenApiAsync.

Common situations: Migrating code that used OperationsToExclude and then adding a custom OperationSelectionPredicate without removing the old list; copy-paste from two samples; or a refactor that left both fields populated.

Related errors


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