microsoft / microsoft/agent-framework

.NET: [Feature]: Allow customizing partition key strategy in `CosmosChatHistoryProvider`

Open
#5,601 0 comments 0 reactions 1 assignee View on GitHub

@westey-m is already working on this.

Since May 5, 2026.

.NET
Dominant language
Python
Stars
13.6k
Forks
2.3k
Avg merge
2d 45m
Merged PRs (30d)
358

Description

### Description

### Problem

`CosmosChatHistoryProvider` couples two concerns: the partition key shape and which fields land on the document. Setting `State.TenantId` and `State.UserId` is what triggers hierarchical partitioning *and* what causes those values to be persisted on documents — they can't be decoupled. So there's no way to use flat `/conversationId` partitioning while still storing tenant/user metadata for filtering, RBAC scoping, or change-feed routing.

### Motivation

Multi-tenant chatbots that support unauthenticated → authenticated user transitions need both: flat PK for continuity across the auth boundary, *and* tenant/user fields on documents for tenant isolation and admin queries. Currently you have to give up one.

### Proposal

Two separable, backward-compatible changes:

1. **Always persist `TenantId`/`UserId`/`SessionId` on documents when set on `State`**, regardless of partition key shape.
2. **Add an optional `Func` constructor parameter** to override `BuildPartitionKey`. Defaults to current behavior when null.

Smaller interim alternative: remove `sealed` and mark `BuildPartitionKey`, `UseHierarchicalPartitioning`, and `CreateMessageDocument` as `protected virtual`.

### Alternatives considered

- Storing tenant/user inside `ChatMessage` metadata — values get buried in the serialized `message` blob; no efficient filtering.
- Writing a fully custom `ChatHistoryProvider` — viable but requires reimplementing transactional batching, request-too-large splitting, TTL, and batch validation.

Source: `dotnet/src/Microsoft.Agents.AI.CosmosNoSql/CosmosChatHistoryProvider.cs`

### Language/SDK

.NET

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.