microsoft / microsoft/agent-framework
.NET: [Feature]: Allow customizing partition key strategy in `CosmosChatHistoryProvider`
@westey-m is already working on this.
Since May 5, 2026.
- 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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Assessment
This issue has not been assessed yet.