Skip to content

.NET: Fix CosmosChatHistoryProvider: omit ttl when MessageTtlSeconds is nul…#7030

Merged
westey-m merged 1 commit into
microsoft:mainfrom
TheovanKraay:fix/cosmos-chat-history-ttl-null
Jul 10, 2026
Merged

.NET: Fix CosmosChatHistoryProvider: omit ttl when MessageTtlSeconds is nul…#7030
westey-m merged 1 commit into
microsoft:mainfrom
TheovanKraay:fix/cosmos-chat-history-ttl-null

Conversation

@TheovanKraay

Copy link
Copy Markdown
Contributor

Motivation & Context

CosmosChatHistoryProvider.MessageTtlSeconds is documented as "Set to null to disable TTL.", but setting it to null broke persistence. The provider's internal CosmosMessageDocument.Ttl property was annotated with a plain [Newtonsoft.Json.JsonProperty("ttl")], so a null value serialized to ttl: null in the document. Cosmos DB rejects that:

The input ttl 'null' is invalid. Ensure to provide a nonzero positive integer less than or equal to '2147483647', or '-1' which means never expire.

So the documented "disable TTL" behavior was impossible — any write with MessageTtlSeconds = null failed.

Description & Review Guide

  • What are the major changes?

    • Added NullValueHandling = NullValueHandling.Ignore to the ttl JSON property on CosmosMessageDocument, so the field is omitted from the document entirely when MessageTtlSeconds is null. 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.
    • Added an emulator-backed unit test (InvokedAsync_WithNullMessageTtl_ShouldPersistWithoutTtlPropertyAsync) that sets MessageTtlSeconds = null, persists a message, and asserts the write succeeds and the stored document contains no ttl key.
  • What is the impact of these changes?

    • MessageTtlSeconds = null now 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

  • The code builds clean without any errors or warnings
  • All unit tests pass, and I have added new tests where possible
  • The PR follows the Contribution Guidelines
  • This PR is linked to an issue and there is no other open PR for this issue (see Related Issue above).
  • This is not a breaking change.

Copilot AI review requested due to automatic review settings July 9, 2026 19:54
@giles17 giles17 added the .NET Usage: [Issues, PRs], Target: .Net label Jul 9, 2026
@github-actions github-actions Bot changed the title Fix CosmosChatHistoryProvider: omit ttl when MessageTtlSeconds is nul… .NET: Fix CosmosChatHistoryProvider: omit ttl when MessageTtlSeconds is nul… Jul 9, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.Ttl JSON metadata to omit the ttl field when the value is null, preventing Cosmos DB from rejecting the write.
  • Add an emulator-backed unit test to verify that MessageTtlSeconds = null persists successfully and that the stored document does not contain a ttl property.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

.NET Usage: [Issues, PRs], Target: .Net

Projects

None yet

Development

Successfully merging this pull request may close these issues.

.NET: [Bug]: CosmosChatHistoryProvider.MessageTtlSeconds = null

5 participants