.NET: Fix CosmosChatHistoryProvider: omit ttl when MessageTtlSeconds is nul…#7030
Merged
westey-m merged 1 commit intoJul 10, 2026
Merged
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR fixes a persistence bug in the .NET CosmosChatHistoryProvider where setting MessageTtlSeconds = null (documented as “disable TTL”) previously caused writes to fail because ttl: null was serialized into Cosmos DB items.
Changes:
- Update
CosmosMessageDocument.TtlJSON metadata to omit thettlfield when the value isnull, preventing Cosmos DB from rejecting the write. - Add an emulator-backed unit test to verify that
MessageTtlSeconds = nullpersists successfully and that the stored document does not contain attlproperty.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| dotnet/src/Microsoft.Agents.AI.CosmosNoSql/CosmosChatHistoryProvider.cs | Omits ttl during JSON serialization when MessageTtlSeconds is null to match documented behavior and avoid Cosmos validation errors. |
| dotnet/tests/Microsoft.Agents.AI.CosmosNoSql.UnitTests/CosmosChatHistoryProviderTests.cs | Adds a Cosmos emulator test ensuring null TTL persists and the stored item omits the ttl property. |
westey-m
approved these changes
Jul 10, 2026
rogerbarreto
approved these changes
Jul 10, 2026
This was referenced Jul 21, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation & Context
CosmosChatHistoryProvider.MessageTtlSecondsis documented as "Set to null to disable TTL.", but setting it tonullbroke persistence. The provider's internalCosmosMessageDocument.Ttlproperty was annotated with a plain[Newtonsoft.Json.JsonProperty("ttl")], so anullvalue serialized tottl: nullin the document. Cosmos DB rejects that:So the documented "disable TTL" behavior was impossible — any write with
MessageTtlSeconds = nullfailed.Description & Review Guide
What are the major changes?
NullValueHandling = NullValueHandling.Ignoreto thettlJSON property onCosmosMessageDocument, so the field is omitted from the document entirely whenMessageTtlSecondsisnull. Cosmos then leaves TTL unset (disabled), which is the documented behavior. Cosmos still receives a valid positive integer (or-1) when a TTL is configured.InvokedAsync_WithNullMessageTtl_ShouldPersistWithoutTtlPropertyAsync) that setsMessageTtlSeconds = null, persists a message, and asserts the write succeeds and the stored document contains nottlkey.What is the impact of these changes?
MessageTtlSeconds = nullnow works as documented and no longer throws. No behavior change when a TTL value is set. No public API change.Related Issue
Fixes #6992
Contribution Checklist