You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
.Net: Bug: ChatHistoryTruncationReducer and ChatHistorySummarizationReducer throw ArgumentOutOfRangeException when targetCount is 1 and the history has a system message #14654
Describe the bug ChatHistoryTruncationReducer and ChatHistorySummarizationReducer throw ArgumentOutOfRangeException from ReduceAsync when they are created with targetCount: 1 and the chat history contains a system message. Both constructors accept targetCount: 1.
ChatHistoryReducerExtensions.LocateSafeReductionIndex subtracts one from targetCount for the system message. With targetCount: 1 the value becomes 0, so the truncation index is chatHistory.Count, and the function-content check reads chatHistory[chatHistory.Count].
To Reproduce
ChatHistoryhistory=new("You are a helpful assistant.");history.AddUserMessage("Hello");history.AddAssistantMessage("Hi");history.AddUserMessage("How are you?");varreducer=newChatHistoryTruncationReducer(targetCount:1);awaitreducer.ReduceAsync(history);// throws
System.ArgumentOutOfRangeException : Index was out of range. Must be non-negative and less than the size of the collection. (Parameter 'index')
at Microsoft.SemanticKernel.ChatCompletion.ChatHistoryReducerExtensions.LocateSafeReductionIndex(...)
at Microsoft.SemanticKernel.ChatCompletion.ChatHistoryTruncationReducer.ReduceAsync(...)
ChatHistorySummarizationReducer with targetCount: 1 fails the same way. From the code, a reducer used through ChatHistoryAgent.HistoryReducer would hit the same exception once the history grows past the target.
Expected behavior
The reducer keeps the system message and the most recent message (plus the summary for the summarization reducer) instead of throwing.
Activity