Skip to content

SummarizingChatReducer removes all FunctionCallContents and FunctionResultContents #6890

@jooooel

Description

@jooooel

Description

Not sure if you would classify this as a bug, but I would be surprised if it was by design 😊

A call to SummarizingChatReducer.ReduceAsync will remove any messages with FunctionResultContent or FunctionCallContent in their content list. This is done even is the message list isn't summarized at all. The returned message list never contain those messages.

This makes it very hard to store the correct history after a a call to ReduceAsync:

  1. We don't know if summarization happened or not. We just get a list of fewer messages back.
  2. Even if older history was summarized, we have lost more recent tool call/response messages.

Reproduction Steps

            var chatClient = new AzureOpenAIClient(new Uri("https://example.com"), new ApiKeyCredential("123")).GetChatClient("model-name").AsIChatClient();
            var summarizingChatReducer = new SummarizingChatReducer(chatClient, targetCount: 99, threshold: 99);
            IEnumerable<ChatMessage> messages =
            [
                new()
                {
                    Role = ChatRole.User,
                    Contents = new List<AIContent>
                    {
                        new TextContent("Who is the current user?")
                    }
                },
                new()
                {
                    Role = ChatRole.Assistant,
                    Contents = new List<AIContent>
                    {
                        new FunctionCallContent("123", "GetCurrentUser"),
                    }
                },
                new()
                {
                    Role = ChatRole.Tool,
                    Contents = new List<AIContent>
                    {
                        new FunctionResultContent("123", "User is Bob")
                    }
                },
                new()
                {
                    Role = ChatRole.Assistant,
                    Contents = new List<AIContent>
                    {
                        new TextContent("The current user is Bob")
                    }
                }
            ];
            
            var summarizedMessages = await summarizingChatReducer.ReduceAsync(messages, cancellationToken);
            var messageCount = summarizedMessages.Count();

Expected behavior

I would expect it to behave like the Semantic Kernels ChatHistorySummarizationReducer (https://github.com/microsoft/semantic-kernel/blob/90d158cbf8bd4598159a6fe64df745e56d9cbdf4/dotnet/src/SemanticKernel.Core/AI/ChatCompletion/ChatHistorySummarizationReducer.cs#L20) where only messages older than the threshold are summarized.

In this case all four messages should be returned (messageCount should be 4 but is 2).

I guess it's fine to remove tool calls and results from the history being summarized, but not from recent history. And certainly not if there isn't any summarization being done.

Actual behavior

All tool calls and tool result messages are removed from the message list, even if nothing was summarized.

Regression?

No response

Known Workarounds

No response

Configuration

dotnet 9.0.304
Microsoft.Extensions.AI 9.9.1

Other information

Metadata

Metadata

Assignees

Labels

area-aiMicrosoft.Extensions.AI librariesbugThis issue describes a behavior which is not expected - a bug.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions