Skip to content

.Net: Possible Bug: context.Terminate Ignored When Using JSON Schema in .NET Semantic Kernel (1.39.0) #10787

Description

@sophialagerkranspandey

Discussed in #10721

Originally posted by StefandewitSPS February 27, 2025
I have a solution in .NET Semantic Kernel (1.39.0) with multiple agents and plugins. I want to return a result to the user based on a kernel function, where the user first needs to make a choice before other tools can be invoked.

An example of this is: "Search for information about employee: X."
In my database, multiple names with the name X are found. I want to show the user a list of options, requiring them to select one before this result is passed to other kernel functions.

I have applied an IAutoFunctionInvocationFilter to achieve this. This works when I return only text from the agents(ResponseFormat = Text). However, when I define a JSON schema on the agent, other tool calls are still invoked, even though I set context.Terminate = true in my function.

It seems that when a JSON schema is defined on the agent, context.Terminate does nothing. Below, I have added my sample code.

Is this the correct way to implement this, or are there better approaches to achieve this?

Example code
public async Task OnAutoFunctionInvocationAsync(AutoFunctionInvocationContext context, Func<AutoFunctionInvocationContext, Task> next)
{

        await next(context);

        if (context.Function.Name.Equals(nameof(SharedPlugin.GetEmplooyesByName), StringComparison.OrdinalIgnoreCase))
        {
            var result = context.Result.GetValue<List<Employees>>();

            if (result.Count > 1)
            {
                string text = $"Multiple items were found. Please select one : ))}";

                var newObject = new
                {
                    ids = result.Select(e => e.EmployeeID),
                    Type = Action.CHOOSE,
                    Message = "Multiple items found, select one before continue ",
                };

                var functionResult = new FunctionResult(context.Function, newObject);

                context.Result = functionResult;

                context.Terminate = true;

            }
        }
    }

In my agent i have an class AssistantCreationOptions with ResponseFormat = BaseModel class (json object)

public enum Action
{
CHOOSE,
None
}

public class BaseModel
{

    public int[] Ids
    {
        get; set;
    }

    public string Message
    {
        get; set;
    }

    public Action Type
    {
        get; set;
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    .NETIssue or Pull requests regarding .NET codetriage

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions