.Net: Thread handling issues with Response Agent streaming response #12705
-
I am using the The issue I am seeing is that the thread only has the user messages after running a streaming response and does not contain any assistant or tool call messages, however, if I invoke a non-streaming response it will have the assistant and tool call messages as well as the user messages. Do I need to manually add the streamed responses to the agent thread when using using System.ClientModel;
using ConsoleApp4;
using Microsoft.Extensions.Configuration;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.Agents;
using Microsoft.SemanticKernel.Agents.OpenAI;
using Microsoft.SemanticKernel.ChatCompletion;
using OpenAI.Responses;
#pragma warning disable SKEXP0110
#pragma warning disable OPENAI001
var configuration = new ConfigurationBuilder()
.AddJsonFile("appsettings.json")
.Build();
await AddPluginsAsync();
async Task AddPluginsAsync()
{
// Set vars
string openAiKey = configuration["openAIKey"] ?? throw new InvalidOperationException("OpenAI key is not set in appsettings.json");
// Create the responses agent
OpenAIResponseClient client = new OpenAIResponseClient("gpt-4.1", new ApiKeyCredential(openAiKey));
OpenAIResponseAgent responseAgent = new(client);
// Create a plugin that defines the tools to be used by the agent.
KernelPlugin plugin = KernelPluginFactory.CreateFromType<MenuPlugin>();
responseAgent.Kernel.Plugins.Add(plugin);
ICollection<ChatMessageContent> messages =
[
new ChatMessageContent(AuthorRole.User, "What is the special soup and its price?"),
new ChatMessageContent(AuthorRole.User, "What is the special drink and its price?"),
];
Console.WriteLine("Messages to be sent to the agent:");
foreach (ChatMessageContent message in messages)
{
Console.WriteLine(message);
}
// Creat agent thread
// OpenAIResponseAgentThread agentThread = new OpenAIResponseAgentThread(client); //<------------This will throw an error
ChatHistoryAgentThread? agentThread = new ChatHistoryAgentThread();
// Invoke the agent and output the response
var responseItems = responseAgent.InvokeStreamingAsync(messages, agentThread);
string result = "";
await foreach (StreamingChatMessageContent content in responseItems)
{
result += content.Content;
}
Console.WriteLine("Response from the agent:");
Console.WriteLine(result);
// Print the thread messages
Console.WriteLine("Messages in the agent thread:");
var messageList = await agentThread.GetMessagesAsync().ToListAsync();
foreach (ChatMessageContent message in messageList)
{
Console.WriteLine(message.Role + ": " + message.Content);
}
} This responds with the following:
Note that the tool calls and assistant messages are not present in the thread after streaming the response. This example was run with the following packages: |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
@markwallace-microsoft - Do you have insight into the dynamics here? |
Beta Was this translation helpful? Give feedback.
-
Hi @thomasjlittle Also to understand how Response manages threads take a look here: https://platform.openai.com/docs/guides/conversation-state?api-mode=responses for more information |
Beta Was this translation helpful? Give feedback.
Hi @thomasjlittle
I have updated the conversation state samples here: #12777
Also to understand how Response manages threads take a look here: https://platform.openai.com/docs/guides/conversation-state?api-mode=responses for more information