Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Jan 5, 2026

  • Understand the issue: ToChatResponse{Async} should merge ChatResponseUpdate.AdditionalProperties into the corresponding ChatMessage.AdditionalProperties rather than ChatResponse.AdditionalProperties
  • Modify ProcessUpdate method in ChatResponseExtensions.cs to add AdditionalProperties based on MessageId presence
  • Update existing tests in ChatResponseUpdateExtensionsTests.cs to expect the new behavior
  • Add new test ToChatResponse_AdditionalPropertiesGoToMessages to verify the correct behavior
  • Build and run tests to verify the changes
  • Fix test failure in OpenTelemetryChatClientTests
  • Add dedicated test ToChatResponse_AdditionalPropertiesRoutingBasedOnMessageId for MessageId-based routing

Summary

This PR updates ToChatResponse and ToChatResponseAsync methods to use MessageId as the discriminator for routing AdditionalProperties:

Routing logic:

  • Updates with MessageIdAdditionalProperties go to message.AdditionalProperties (message-scoped)
  • Updates without MessageIdAdditionalProperties go to response.AdditionalProperties (response-scoped)

Benefits:

  1. Properties from distinct messages don't collide with each other when using MessageId
  2. Response-level metadata (like system_fingerprint) is preserved correctly for telemetry
  3. Backward compatible with existing code that doesn't use MessageId
Original prompt

See microsoft/agent-framework#3086. Please update ToChatResponse{Async} so that a ChatResponseUpdate's AdditionalProperties contribute to the ChatMessage it gets merged into rather than contributing into the top-level ChatResponse. That way, properties from distinct messages don't collide with each other and more information from the updates can be preserved.


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Microsoft Reviewers: Open in CodeFlow

@stephentoub stephentoub marked this pull request as ready for review January 5, 2026 15:47
@stephentoub stephentoub requested a review from a team as a code owner January 5, 2026 15:47
Copilot AI review requested due to automatic review settings January 5, 2026 15:47
Copilot AI changed the title [WIP] Update ToChatResponse{Async} to merge AdditionalProperties correctly ToChatResponse: Merge AdditionalProperties into ChatMessage instead of ChatResponse Jan 5, 2026
Copilot AI requested a review from stephentoub January 5, 2026 15:51
@stephentoub stephentoub enabled auto-merge (squash) January 5, 2026 15:51
@stephentoub stephentoub added the area-ai Microsoft.Extensions.AI libraries label Jan 5, 2026
Copy link
Contributor

Copilot AI left a comment

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 bug where AdditionalProperties from ChatResponseUpdate objects were incorrectly being merged into the top-level ChatResponse.AdditionalProperties instead of the individual ChatMessage.AdditionalProperties. This caused properties from different messages to collide when they had the same keys.

Key Changes:

  • Moved AdditionalProperties merging logic from ChatResponse level to ChatMessage level in ProcessUpdate method
  • Updated existing test expectations to reflect that properties now belong to messages
  • Added comprehensive new test to verify properties are correctly isolated per message

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
src/Libraries/Microsoft.Extensions.AI.Abstractions/ChatCompletion/ChatResponseExtensions.cs Relocated the AdditionalProperties merging logic (lines 538-548) to apply to message instead of response, enabling per-message property isolation
test/Libraries/Microsoft.Extensions.AI.Abstractions.Tests/ChatCompletion/ChatResponseUpdateExtensionsTests.cs Updated existing test to expect AdditionalProperties on messages (lines 60-65) and added new test ToChatResponse_AdditionalPropertiesGoToMessages (lines 449-496) to verify the corrected behavior with multiple messages

@stephentoub
Copy link
Member

@copilot, there's a test failure, e.g.

Failed	Microsoft.Extensions.AI.OpenTelemetryChatClientTests.ExpectedInformationLogged_Async(enableSensitiveData: True, streaming: True)	Microsoft.Extensions.AI.OpenTelemetryChatClientTests.Microsoft.Extensions.AI.OpenTelemetryChatClientTests.ExpectedInformationLogged_Async(enableSensitiveData: True, streaming: True)	Assert.Equal() Failure: Values differ

Expected: abcdefgh
Actual:   null
...	

auto-merge was automatically disabled January 5, 2026 16:57

Head branch was pushed to by a user without write access

Copy link
Contributor Author

Copilot AI commented Jan 5, 2026

@copilot, there's a test failure, e.g.

Failed	Microsoft.Extensions.AI.OpenTelemetryChatClientTests.ExpectedInformationLogged_Async(enableSensitiveData: True, streaming: True)	Microsoft.Extension...

Fixed in 04ddd18. The issue was that my initial change unconditionally moved AdditionalProperties to the message. The fix now uses MessageId as the discriminator: updates with MessageId route properties to message.AdditionalProperties, while updates without MessageId (like the trailing metadata update with system_fingerprint) route to response.AdditionalProperties.

@stephentoub
Copy link
Member

cc: @westey-m

@stephentoub stephentoub enabled auto-merge (squash) January 6, 2026 20:49
@stephentoub stephentoub merged commit 9938323 into main Jan 8, 2026
6 checks passed
@stephentoub stephentoub deleted the copilot/update-tochatresponse-async branch January 8, 2026 18:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-ai Microsoft.Extensions.AI libraries

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants