-
Notifications
You must be signed in to change notification settings - Fork 849
Allow FunctionResultContent in User messages to produce ToolChatMessage #7190
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
src/Libraries/Microsoft.Extensions.AI.OpenAI/OpenAIChatClient.cs
Outdated
Show resolved
Hide resolved
There was a problem hiding this 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 extends the OpenAIChatClient.ToOpenAIChatMessages method to recognize FunctionResultContent not only in Tool messages, but also in User, System, and Developer messages. When processing these message types, the implementation now extracts any FunctionResultContent items and yields them as separate ToolChatMessage objects before yielding the original message type with remaining content.
Key Changes
- Added logic to extract
FunctionResultContentfrom User/System/Developer messages and yield them asToolChatMessageobjects - Refactored duplicate code by extracting a
CreateToolChatMessagehelper method to handle the conversion ofFunctionResultContenttoToolChatMessage - Added comprehensive test coverage for the new functionality with three test cases covering single, multiple, and content-only scenarios
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| src/Libraries/Microsoft.Extensions.AI.OpenAI/OpenAIChatClient.cs | Adds FunctionResultContent extraction logic for User/System/Developer messages (lines 157-165), creates helper method CreateToolChatMessage to eliminate code duplication (lines 756-773), and refactors Tool message handling to use the new helper |
| test/Libraries/Microsoft.Extensions.AI.OpenAI.Tests/OpenAIConversionTests.cs | Adds three new test methods validating that FunctionResultContent in User messages correctly produces ToolChatMessage objects, covering scenarios with mixed content, multiple results, and result-only messages |
test/Libraries/Microsoft.Extensions.AI.OpenAI.Tests/OpenAIConversionTests.cs
Outdated
Show resolved
Hide resolved
test/Libraries/Microsoft.Extensions.AI.OpenAI.Tests/OpenAIConversionTests.cs
Show resolved
Hide resolved
…nly content - When User/System/Developer message contains only FunctionResultContent, produce only ToolChatMessage without an empty User/System/Developer message - Add test coverage for System messages with FunctionResultContent Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
Understand the issue:
OpenAIChatClient.ToOpenAIChatMessagesonly processesFunctionResultContentwhen the message role isChatRole.Tool. The feature request is to also supportFunctionResultContentinsideUsermessages.Implement the fix in
OpenAIChatClient.cs:User(orSystem/Developer) messages, check forFunctionResultContentitems and yieldToolChatMessageobjects for each oneCreateToolChatMessageto avoid duplicationAdd tests in
OpenAIConversionTests.cs:AsOpenAIChatMessages_FunctionResultContentInUserMessage_ProducesToolMessageAsOpenAIChatMessages_MultipleFunctionResultContentInUserMessage_ProducesMultipleToolMessagesAsOpenAIChatMessages_FunctionResultContentOnlyInUserMessage_ProducesOnlyToolMessage(fixed: no longer produces empty UserChatMessage)AsOpenAIChatMessages_FunctionResultContentInSystemMessage_ProducesToolMessage(new)AsOpenAIChatMessages_FunctionResultContentOnlyInSystemMessage_ProducesOnlyToolMessage(new)Build and test the changes
Run code review and address feedback
Original prompt
💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.
Microsoft Reviewers: Open in CodeFlow