Fix GetResponseAsync<T> to look at the last message only#6721
Fix GetResponseAsync<T> to look at the last message only#6721shyamnamboodiripad merged 2 commits intodotnet:mainfrom
Conversation
It was looking at all messages in the whole response rather than just the last, which is where we expect the JSON to be.
There was a problem hiding this comment.
Pull Request Overview
Fixes the GetResponseAsync<T> method to properly extract JSON from the last message in a chat response instead of concatenating text from all messages. This ensures structured output parsing works correctly when chat responses contain multiple messages (e.g., function calls followed by the final JSON result).
- Changed JSON extraction logic to use only the last message's text content
- Added comprehensive test coverage for multi-message scenarios with function calls
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/ChatCompletion/ChatResponse{T}.cs |
Updated JSON extraction logic to use last message only instead of all message text |
test/Libraries/Microsoft.Extensions.AI.Tests/ChatCompletion/ChatClientStructuredOutputExtensionsTests.cs |
Added test case verifying correct behavior with multi-message responses containing function calls |
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
|
@dotnet/dotnet-extensions-ai Could I please get one more approval on this fix from someone on the MEAI side? The bug was reported by an internal partner team, and it would be great to have the fix merged sooner rather than later. Thanks! |
It was looking at all messages in the whole response rather than just the last, which is where we expect the JSON to be.
Microsoft Reviewers: Open in CodeFlow