Skip to content

Fix GetResponseAsync<T> to look at the last message only#6721

Merged
shyamnamboodiripad merged 2 commits intodotnet:mainfrom
stephentoub:fixresponsetext
Aug 19, 2025
Merged

Fix GetResponseAsync<T> to look at the last message only#6721
shyamnamboodiripad merged 2 commits intodotnet:mainfrom
stephentoub:fixresponsetext

Conversation

@stephentoub
Copy link
Copy Markdown
Member

@stephentoub stephentoub commented Aug 15, 2025

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

It was looking at all messages in the whole response rather than just the last, which is where we expect the JSON to be.
@stephentoub stephentoub requested a review from a team as a code owner August 15, 2025 03:45
@github-actions github-actions bot added the area-ai Microsoft.Extensions.AI libraries label Aug 15, 2025
Copy link
Copy Markdown
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

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.

@shyamnamboodiripad
Copy link
Copy Markdown
Contributor

@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!

@shyamnamboodiripad shyamnamboodiripad merged commit 736edc6 into dotnet:main Aug 19, 2025
6 checks passed
@stephentoub stephentoub deleted the fixresponsetext branch September 5, 2025 14:10
@github-actions github-actions bot locked and limited conversation to collaborators Oct 6, 2025
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.

3 participants