Handle multi-block sampling responses in SampleStructuredAsync#52
Merged
Conversation
The client can split a sampling answer across multiple content blocks, sometimes a truncated false-start followed by the complete answer. Joining them blindly produced invalid JSON. Try each block individually (last first), then fall back to the joined text. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
This PR fixes a structured-sampling parsing failure when the MCP sampling backend returns assistant output across multiple content blocks (e.g., a truncated false start followed by a complete JSON answer). The update changes SampleStructuredAsync to try deserializing each block individually (preferring the last block) before falling back to deserializing the joined text, preventing invalid JSON formed by naive concatenation.
Changes:
- Update
SampleStructuredAsyncto attempt JSON deserialization per content block (last→first), then fall back to joined blocks when needed. - Refactor sampling helpers (
SampleRawAsync,ExtractTextBlocks,TryDeserialize<T>) and addblocks=Ndiagnostics. - Add tests covering multi-block behaviors and extend
FakeSamplingMcpServerto simulate multi-block responses.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| PrCopilot/tests/PrCopilot.Tests/SamplingHelperTests.cs | Adds coverage for multi-block structured sampling responses (complete block preference + join fallback). |
| PrCopilot/tests/PrCopilot.Tests/FakeSamplingMcpServer.cs | Enhances the test MCP server to emit multiple TextContentBlocks via a params string[] constructor. |
| PrCopilot/src/PrCopilot/Tools/SamplingHelper.cs | Implements block-aware structured parsing with per-block deserialization attempts and a join fallback for split objects. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes a sampling failure where monitoring a PR errors with
Sampling failed to explain comment after 2 attempts/'e' is invalid after a value.The Copilot sampling backend can return the assistant answer across multiple content blocks — sometimes a truncated false-start block followed by the complete answer.
ExtractTextjoined all blocks with\n, fusing them into invalid JSON (e.g.{"explanation": "...ind\n{"explanation": ...}), which then failed to deserialize.Changes
SampleStructuredAsyncnow tries each content block individually (last→first) — the final block is usually the complete answer — then falls back to the joined text for providers that split one object across blocks. First candidate that deserializes intoTwins.SampleRawAsync,ExtractTextBlocks, and aTryDeserialize<T>helper. Addedblocks=Ndiagnostic logging.SampleTextAsync/ExtractText(plain-text path) keep the existing join behavior.FakeSamplingMcpServergains aparams string[]constructor to simulate multi-block responses.Tests
Three new tests in
SamplingHelperTests:MultipleBlocks_UsesCompleteBlock— truncated first + complete second (the exact failure)MultipleBlocks_CompleteBlockFirst— either ordering worksMultipleBlocks_OneObjectSplitAcrossBlocks_FallsBackToJoin— back-compatAll 350 tests pass; the two multi-block tests are verified to fail against the previous code. Also validated live locally via a dev build before opening this PR.