Repository navigation
Commit 563eb3a
.NET: terminate chat completions SSE stream with data: [DONE] (#8532)
* .NET: terminate chat completions SSE stream with data: [DONE]
The agent chat completions endpoint ended its SSE stream by simply closing the
connection. OpenAI-compatible clients treat a final `data: [DONE]` frame as the
completion signal, so they waited for a timeout or hung instead.
GetStreamingChunksAsync now yields a null-payload sentinel after the agent
stream completes, and the item formatter writes it as `[DONE]`. Routing the
terminator through SseFormatter keeps that component the single owner of the
`data: ` prefix and the blank-line frame terminator.
The sentinel is yielded after the loop, so it only follows a stream that ran to
completion: if the agent throws or the caller aborts, the exception propagates
out of the enumerator and no terminator is written. A client must never read a
truncated stream as a complete one.
The Responses endpoint is deliberately unchanged -- that API signals completion
with typed `response.*` events, and the recorded trace for it contains no
[DONE] frame.
The existing streaming conformance test could not catch this: it asserts
through ParseChatCompletionChunksFromSse, which skips the `[DONE]` line because
it is not JSON. The new test asserts on the raw SSE body instead.
Fixes #8526
* .NET: address review — pin the failure-path invariant and correct the rationale comment
Two review comments from Copilot on #8532.
The sentinel comment asserted that clients "wait for the connection to drop,
which reads as a hang or a timeout". Live testing disproved that: the response
is chunked, so the zero-length terminating chunk ends the body definitively and
the official OpenAI client completes without the sentinel. The comment now
states the actual interoperability problem -- consumers that treat the sentinel
rather than end-of-body as the completion signal cannot recognize the stream as
finished -- and names the one this repo ships, SseResponseIdCapture.
The failure-path invariant is now covered. An earlier attempt at this test was
discarded because a client-side assertion has no teeth: when the agent throws,
the server aborts and the client receives no body, so the test passed even
against a try/finally refactor that writes the terminator unconditionally. This
version tees the response body into a buffer through test middleware, so it
observes what the server actually wrote. Verified to fail against that refactor.
The failing chat client and the tee stream are private to the test file rather
than added to the shared TestHelpers, keeping the change to one test file.
* .NET: tidy the failure-path test after a review pass
Review findings on the test added in 2240252. No behaviour change; the test
still fails against the try/finally append refactor it exists to block.
- Dispose the capture buffer (`using var recorded`), which was leaked.
- Hoist the trace load into Arrange, matching the Arrange/Act/Assert split the
rest of the file uses; it was sitting in the Act block.
- Record why the host is built inline instead of through
ConformanceTestBase.CreateTestServerAsync: this test needs response-body
middleware and a throwing chat client, neither of which belongs in the shared
harness for one caller. Without the note the duplication reads as an
oversight and invites a refactor back into the base class.
---------
Co-authored-by: westey <[email protected]>1 parent 4964bdd commit 563eb3a
2 files changed
Lines changed: 211 additions & 4 deletions
File tree
- dotnet
- src/Microsoft.Agents.AI.Hosting.OpenAI/ChatCompletions
- tests/Microsoft.Agents.AI.Hosting.OpenAI.UnitTests
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | 3 | | |
| 4 | + | |
4 | 5 | | |
5 | 6 | | |
6 | 7 | | |
| |||
79 | 80 | | |
80 | 81 | | |
81 | 82 | | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
82 | 96 | | |
83 | | - | |
| 97 | + | |
84 | 98 | | |
85 | 99 | | |
86 | 100 | | |
87 | 101 | | |
88 | 102 | | |
89 | | - | |
| 103 | + | |
90 | 104 | | |
91 | 105 | | |
92 | 106 | | |
| |||
167 | 181 | | |
168 | 182 | | |
169 | 183 | | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
170 | 196 | | |
171 | 197 | | |
172 | 198 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
| 3 | + | |
3 | 4 | | |
| 5 | + | |
4 | 6 | | |
5 | 7 | | |
| 8 | + | |
| 9 | + | |
6 | 10 | | |
| 11 | + | |
7 | 12 | | |
| 13 | + | |
8 | 14 | | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
9 | 18 | | |
| 19 | + | |
| 20 | + | |
10 | 21 | | |
11 | 22 | | |
12 | 23 | | |
| |||
230 | 241 | | |
231 | 242 | | |
232 | 243 | | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
233 | 281 | | |
234 | 282 | | |
235 | 283 | | |
| |||
343 | 391 | | |
344 | 392 | | |
345 | 393 | | |
346 | | - | |
| 394 | + | |
347 | 395 | | |
348 | 396 | | |
349 | 397 | | |
| |||
598 | 646 | | |
599 | 647 | | |
600 | 648 | | |
601 | | - | |
| 649 | + | |
602 | 650 | | |
603 | 651 | | |
604 | 652 | | |
| |||
622 | 670 | | |
623 | 671 | | |
624 | 672 | | |
| 673 | + | |
| 674 | + | |
| 675 | + | |
| 676 | + | |
| 677 | + | |
| 678 | + | |
| 679 | + | |
| 680 | + | |
| 681 | + | |
| 682 | + | |
| 683 | + | |
| 684 | + | |
| 685 | + | |
| 686 | + | |
| 687 | + | |
| 688 | + | |
| 689 | + | |
| 690 | + | |
| 691 | + | |
| 692 | + | |
| 693 | + | |
| 694 | + | |
| 695 | + | |
| 696 | + | |
| 697 | + | |
| 698 | + | |
| 699 | + | |
| 700 | + | |
| 701 | + | |
| 702 | + | |
| 703 | + | |
| 704 | + | |
| 705 | + | |
| 706 | + | |
| 707 | + | |
| 708 | + | |
| 709 | + | |
| 710 | + | |
| 711 | + | |
| 712 | + | |
| 713 | + | |
| 714 | + | |
| 715 | + | |
| 716 | + | |
| 717 | + | |
| 718 | + | |
| 719 | + | |
| 720 | + | |
| 721 | + | |
| 722 | + | |
| 723 | + | |
| 724 | + | |
| 725 | + | |
| 726 | + | |
| 727 | + | |
| 728 | + | |
| 729 | + | |
| 730 | + | |
| 731 | + | |
| 732 | + | |
| 733 | + | |
| 734 | + | |
| 735 | + | |
| 736 | + | |
| 737 | + | |
| 738 | + | |
| 739 | + | |
| 740 | + | |
| 741 | + | |
| 742 | + | |
| 743 | + | |
| 744 | + | |
| 745 | + | |
| 746 | + | |
| 747 | + | |
| 748 | + | |
| 749 | + | |
| 750 | + | |
| 751 | + | |
| 752 | + | |
| 753 | + | |
| 754 | + | |
| 755 | + | |
| 756 | + | |
| 757 | + | |
| 758 | + | |
| 759 | + | |
| 760 | + | |
| 761 | + | |
| 762 | + | |
| 763 | + | |
| 764 | + | |
| 765 | + | |
| 766 | + | |
| 767 | + | |
| 768 | + | |
| 769 | + | |
| 770 | + | |
| 771 | + | |
| 772 | + | |
| 773 | + | |
| 774 | + | |
| 775 | + | |
| 776 | + | |
| 777 | + | |
| 778 | + | |
| 779 | + | |
| 780 | + | |
| 781 | + | |
| 782 | + | |
| 783 | + | |
| 784 | + | |
| 785 | + | |
| 786 | + | |
| 787 | + | |
| 788 | + | |
| 789 | + | |
| 790 | + | |
| 791 | + | |
| 792 | + | |
| 793 | + | |
| 794 | + | |
| 795 | + | |
| 796 | + | |
| 797 | + | |
| 798 | + | |
| 799 | + | |
| 800 | + | |
| 801 | + | |
| 802 | + | |
| 803 | + | |
| 804 | + | |
| 805 | + | |
625 | 806 | | |
0 commit comments