Maintaining conversation history with OpenAI .NET SDK Assistant API

I’m working with the OpenAI .NET SDK and trying to implement conversation memory using the Assistant API. The goal is to have the assistant remember what we talked about in previous messages.

My approach: When a user logs in, I create an assistant and thread, then save the thread ID in the session. For follow-up questions, I pull the thread ID from session storage and use it for the next API call.

Current implementation

ExecutionSettings executionSettings = new ExecutionSettings()
{
    MessageLimit = RunTruncationStrategy.CreateLastMessagesStrategy(10)
};

if (maintainChatHistory)
{
    if (_sessionAccessor.HttpContext.Session.GetString("ChatThreadId") != null)
    {
        currentThreadId = _sessionAccessor.HttpContext.Session.GetString("ChatThreadId");
        var existingThread = chatClient.GetThread(currentThreadId);
        responseStream = chatClient.CreateRunStreaming(existingThread.Value.Id, chatAssistant.Id, executionSettings);
    }
    else
    {
        result = "";
        var newThread = chatClient.CreateThread(threadConfiguration);
        responseStream = chatClient.CreateRunStreaming(newThread.Value.Id, chatAssistant.Id, executionSettings);
        
        _sessionAccessor.HttpContext.Session.SetString("ChatThreadId", newThread.Value.Id);
        currentThreadId = newThread.Value.Id;
    }
}
else
{
    result = "";
    var freshThread = chatClient.CreateThread(threadConfiguration);
    responseStream = chatClient.CreateRunStreaming(freshThread.Value.Id, chatAssistant.Id, executionSettings);
    currentThreadId = freshThread.Value.Id;
}

The problem is that the assistant keeps acting like each message is the first one and asks “how can I help you” instead of continuing our conversation. What am I missing here?

Had this exact problem a few months ago. Your thread management looks fine - that’s not the issue. You’re missing a step: you need to add the user’s current message to the thread before creating the run. When you pull an existing thread, it doesn’t automatically include the new input. You have to explicitly add it with chatClient.CreateMessage() before calling CreateRunStreaming(). After getting your existing thread, add this:

chatClient.CreateMessage(existingThread.Value.Id, MessageRole.User, userMessage);

Then create the run. Without this, the assistant only sees old messages but not what the user just asked - that’s why it keeps greeting you instead of answering the actual question.

Hold up - are you recreating the assistant every time? I had the same weird issue and turned out I was spinning up new assistant instances instead of reusing the same one. The assistant has to stay consistent across requests, not just the thread. Also double-check your threadConfiguration for system messages that might be overriding context.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.