Skip to content

.NET: [Bug]: Function call and tool result messages are not stored in the session when using ChatReducer #4494

@marcominerva

Description

@marcominerva

Description

Consider the following code:

#!/usr/bin/env dotnet

#:sdk Microsoft.NET.Sdk

#:property OutputType=Exe
#:property TargetFramework=net10.0
#:property ImplicitUsings=enable
#:property Nullable=enable
#:property NoWarn=$(NoWarn);MEAI001
#:property PublishAot=false

#:package Azure.AI.OpenAI@2.1.0
#:package Azure.Identity@1.18.0
#:package Microsoft.Agents.AI.OpenAI@1.0.0-rc3

using System.ComponentModel;
using Azure.AI.OpenAI;
using Azure.Identity;
using Microsoft.Agents.AI;
using Microsoft.Extensions.AI;
using OpenAI.Chat;

var endpoint = Environment.GetEnvironmentVariable("AZURE_OPENAI_ENDPOINT") ?? throw new InvalidOperationException("AZURE_OPENAI_ENDPOINT is not set.");
var deploymentName = Environment.GetEnvironmentVariable("AZURE_OPENAI_DEPLOYMENT_NAME") ?? "gpt-4.1";

[Description("Get the weather for a given location.")]
static string GetWeather([Description("The location to get the weather for.")] string location)
    => $"The weather in {location} is cloudy with a high of 15°C.";

AIAgent agent = new AzureOpenAIClient(
    new Uri(endpoint),
    new DefaultAzureCredential())
    .GetChatClient(deploymentName).AsIChatClient()
    .AsAIAgent(new ChatClientAgentOptions
    {
        Name = "Assistant",
        //ChatHistoryProvider = new InMemoryChatHistoryProvider(new()
        //{
        //    ChatReducer = new MessageCountingChatReducer(20),
        //    ReducerTriggerEvent = InMemoryChatHistoryProviderOptions.ChatReducerTriggerEvent.AfterMessageAdded,
        //}),
        ChatOptions = new ChatOptions
        {
            Tools = [AIFunctionFactory.Create(GetWeather)]
        }
    });

var session = await agent.CreateSessionAsync();
var response = await agent.RunAsync("What is the weather like in Taggia?", session);
Console.WriteLine(response);

session.TryGetInMemoryChatHistory(out var messages);
Console.WriteLine($"Messages in session: {messages.Count}");

In particular, the setting of ChatHistoryProvider has been commented out. With this configuration, after calling RunAsync, I have 4 messages in the session:

{
   "messages":[
      {
         "role":"user",
         "contents":[
            {
               "$type":"text",
               "text":"What is the weather like in Taggia?"
            }
         ]
      },
      {
         "authorName":"Assistant",
         "createdAt":"2026-03-05T09:14:51+00:00",
         "role":"assistant",
         "contents":[
            {
               "$type":"functionCall",
               "callId":"call_19muVFBQeEth4xsPu3OAckUP",
               "name":"_Main_g_GetWeather_0_0",
               "arguments":{
                  "location":"Taggia"
               },
               "informationalOnly":true
            }
         ],
         "messageId":"chatcmpl-DFz59TbFUrm2bR3ZeRogTiO4W0vpb"
      },
      {
         "authorName":"Assistant",
         "role":"tool",
         "contents":[
            {
               "$type":"functionResult",
               "callId":"call_19muVFBQeEth4xsPu3OAckUP",
               "result":"The weather in Taggia is cloudy with a high of 15°C."
            }
         ]
      },
      {
         "authorName":"Assistant",
         "createdAt":"2026-03-05T09:14:52+00:00",
         "role":"assistant",
         "contents":[
            {
               "$type":"text",
               "text":"The weather in Taggia is currently cloudy with a high temperature of 15°C."
            }
         ],
         "messageId":"chatcmpl-DFz5A3EVZ0egQlwhVFv11oHYgqnXP"
      }
   ]
}

However, if I uncomment the setting of ChatHistoryProvider to use a ChatReducer, when I call RunAsync, I get only 2 messages:

{
   "messages":[
      {
         "role":"user",
         "contents":[
            {
               "$type":"text",
               "text":"What is the weather like in Taggia?"
            }
         ]
      },
      {
         "authorName":"Assistant",
         "createdAt":"2026-03-05T09:16:15+00:00",
         "role":"assistant",
         "contents":[
            {
               "$type":"text",
               "text":"The weather in Taggia is currently cloudy, with a high temperature of 15°C."
            }
         ],
         "messageId":"chatcmpl-DFz6VO7mqr1FNCQLTgJpYROGGolJo"
      }
   ]
}

Function call and Function result messages aren't present in the session. I have also tried to explicitly set StorageInputRequestMessageFilter and StorageInputResponseMessageFilter properties:

ChatHistoryProvider = new InMemoryChatHistoryProvider(new()
{
    ChatReducer = new MessageCountingChatReducer(20),
    ReducerTriggerEvent = InMemoryChatHistoryProviderOptions.ChatReducerTriggerEvent.AfterMessageAdded,
    StorageInputRequestMessageFilter = messages => messages,
    StorageInputResponseMessageFilter = messages => messages,
}),

In this case, I see that messages collection in StorageInputResponseMessageFilter effectively contains all the response messages, included the ones from the tool call. However, they are not persisted in the session.

Package Versions

Microsoft.Agents.AI.OpenAI: 1.0.0-rc3

.NET Version

.NET 10.0.3

Metadata

Metadata

Assignees

Labels

.NETagentsIssues related to single agentsbugSomething isn't working

Type

Projects

Status

No status

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions