Skip to content

Conversation

@PederHP
Copy link
Contributor

@PederHP PederHP commented Dec 13, 2025

When FunctionApprovalResponseContent items from the same original message (with MessageId = null) are processed, they incorrectly get split into separate FunctionCallContent messages instead of being grouped together.

Root Cause: Key mismatch in ConvertToFunctionCallContentMessages:

  1. When creating messages from approvals, ConvertToFunctionCallContentMessage sets MessageId = fallbackMessageId if the original was null
  2. Dictionary storage used currentMessage.MessageId (which is fallbackMessageId)
  3. Dictionary lookup used RequestMessage?.MessageId ?? "" (which is "")
  4. Keys don't match → messages aren't grouped

Fix (2 changes in FunctionInvokingChatClient.cs):

  1. Line 1455-1458: When seeding the dictionary, use the effective key that accounts for fallbackMessageId substitution
  2. Line 1464-1473: Use RequestMessage?.MessageId ?? "" consistently for both lookup and storage

This ensures all approvals from the same original message (whether MessageId is set or null) get properly grouped into a single FunctionCallContent message.

Microsoft Reviewers: Open in CodeFlow

Copilot AI review requested due to automatic review settings December 13, 2025 16:03
@PederHP PederHP requested a review from a team as a code owner December 13, 2025 16:03
@github-actions github-actions bot added the area-ai Microsoft.Extensions.AI libraries label Dec 13, 2025
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 in the FunctionInvokingChatClient where FunctionApprovalResponseContent items from messages with null MessageId were incorrectly split into separate FunctionCallContent messages instead of being grouped together. The issue stemmed from inconsistent dictionary key usage when tracking messages during the conversion process.

Key Changes:

  • Added regression test to verify correct grouping behavior when MessageId is null
  • Fixed dictionary key mismatch by using RequestMessage.MessageId consistently for lookup and storage
  • Added logic to handle fallback MessageId substitution when seeding the dictionary

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
test/Libraries/Microsoft.Extensions.AI.Tests/ChatCompletion/FunctionInvokingChatClientApprovalsTests.cs Adds comprehensive regression test with detailed documentation explaining the null MessageId grouping bug and expected behavior
src/Libraries/Microsoft.Extensions.AI/ChatCompletion/FunctionInvokingChatClient.cs Fixes dictionary key inconsistency by computing effective keys that account for fallback MessageId substitution and using RequestMessage.MessageId consistently

@PederHP
Copy link
Contributor Author

PederHP commented Dec 13, 2025

@dotnet-policy-service agree

@PederHP
Copy link
Contributor Author

PederHP commented Dec 13, 2025

Fixes #7153

Copy link
Member

@stephentoub stephentoub left a comment

Choose a reason for hiding this comment

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

LGTM. Thanks for the fix.

@stephentoub stephentoub enabled auto-merge (squash) December 15, 2025 04:34
@stephentoub stephentoub merged commit 35b0c86 into dotnet:main Dec 15, 2025
6 checks passed
This was referenced Jan 14, 2026
@github-actions github-actions bot locked and limited conversation to collaborators Jan 14, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

area-ai Microsoft.Extensions.AI libraries

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants