Skip to content

.NET: [Bug]: CosmosDBChatExtensions does not restore ChatHistoryProvider state on deserialize #3640

@rcoccia

Description

@rcoccia

Description

When deserializing a session with ChatClientAgent.DeserializeSessionAsync, the framework passes the serialized ChatHistoryProviderState to ChatHistoryProviderFactory. However, the official CosmosDBChatExtensions factory ignores this state and always creates a new CosmosChatHistoryProvider, which generates a new ConversationId. As a result, conversation history is lost on each deserialize.

Expected:
The factory should use context.SerializedState when present and call CosmosChatHistoryProvider.CreateFromSerializedState(...) to preserve the original ConversationId

Actual:
The factory always creates new CosmosChatHistoryProvider(...), losing the previous ConversationId

Suggested Fix:

options.ChatHistoryProviderFactory = (context, ct) =>

{
    if (context.SerializedState.ValueKind == JsonValueKind.Object)
    {
        return new ValueTask<ChatHistoryProvider>(
            CosmosChatHistoryProvider.CreateFromSerializedState(
                new CosmosClient(accountEndpoint, tokenCredential),
                context.SerializedState,
                databaseId,
                containerId,
                context.JsonSerializerOptions));
    }

    return new ValueTask<ChatHistoryProvider>(
        new CosmosChatHistoryProvider(accountEndpoint, tokenCredential, databaseId, containerId));
};

Code Sample

Error Messages / Stack Traces

Package Versions

Microsoft.Agents.AI 1.0.0-preview.251113.1

.NET Version

.NET 10

Additional Context

No response

Metadata

Metadata

Assignees

Labels

.NETbugSomething isn't workingv1.0Features being tracked for the version 1.0 GA

Type

Projects

Status

No status

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions