semantic-kernel icon indicating copy to clipboard operation
semantic-kernel copied to clipboard

.Net: Bug: AgentActor.InvokeAsync returns empty since response is not assigned.

Open hanhsia opened this issue 1 year ago • 1 comments

Describe the bug AgentActor.InvokeAsync returns empty which cause agent returns empty.

Expected behavior AgentActor.InvokeAsync should return the response of InvokeAsync or InvokeStreamingAsync

Platform

  • Language: [e.g. C#, Python] C#
  • Source: [e.g. NuGet package version 0.1.0, pip package version 0.1.0, main branch of repository]
  • AI model: [e.g. OpenAI:GPT-4o-mini(2024-07-18)]
  • IDE: [e.g. Visual Studio, VS Code]
  • OS: [e.g. Windows, Mac]

Additional context the latest change #12455 raised the issue that response isn't assigned any value: current code: protected async ValueTask InvokeAsync(IList input, CancellationToken cancellationToken) { this.Context.Cancellation.ThrowIfCancellationRequested();

 **_ChatMessageContent? response = null;_**

 AgentInvokeOptions options = this.GetInvokeOptions(HandleMessage);
 if (this.Context.StreamingResponseCallback == null)
 {
     // No need to utilize streaming if no callback is provided
     **_await this.InvokeAsync(input, options, cancellationToken).ConfigureAwait(false);_**
 }
 else
 {
     **_await this.InvokeStreamingAsync(input, options, cancellationToken).ConfigureAwait(false);_**
 }

 **_return response ?? new ChatMessageContent(AuthorRole.Assistant, string.Empty);_**

 async Task HandleMessage(ChatMessageContent message)
 {
     response = message; // Keep track of most recent response for both invocation modes

     if (this.Context.ResponseCallback is not null && !this.ResponseCallbackFilter(message))
     {
         await this.Context.ResponseCallback.Invoke(message).ConfigureAwait(false);
     }
 }

}

expected code: protected async ValueTask InvokeAsync(IList input, CancellationToken cancellationToken) { this.Context.Cancellation.ThrowIfCancellationRequested();

    ChatMessageContent? response = null;

    AgentInvokeOptions options = this.GetInvokeOptions(HandleMessage);
    if (this.Context.StreamingResponseCallback == null)
    {
        // Handle non-streaming: directly await last response and capture it
        var lastResponseItem = await this.Agent.InvokeAsync(input, this.Thread, options, cancellationToken)
            .LastOrDefaultAsync(cancellationToken).ConfigureAwait(false);
        this.Thread ??= lastResponseItem?.Thread;
        response = lastResponseItem?.Message;
    }
    else
    {
         //not sure how to handle it correctly.
        _await this.InvokeStreamingAsync(input, options, cancellationToken).ConfigureAwait(false);_
    }

    return response ?? new ChatMessageContent(AuthorRole.Assistant, string.Empty);

    async Task HandleMessage(ChatMessageContent message)
    {
        response = message; // Keep track of most recent response for both invocation modes

        if (this.Context.ResponseCallback is not null && !this.ResponseCallbackFilter(message))
        {
            await this.Context.ResponseCallback.Invoke(message).ConfigureAwait(false);
        }
    }
}

hanhsia avatar Jun 30 '25 08:06 hanhsia

Thank you for identifying this issue. I'm unclear on why the the assignment in the local function closure reverts to null afterwards, but I was able to find a work-around. I appreciate your suggestion, but this needs to work for the streaming case as well.

crickman avatar Jul 25 '25 05:07 crickman