Repository navigation
Conversation
A gateway or provider that closes the connection mid-generation produces onComplete on the SSE body, not onError: a server-side FIN is a normal completion to the HTTP client. Nothing downstream could tell the prefix apart from a full answer, so truncated text was persisted and shown as final. Track the two markers that prove a response completed -- the `[DONE]` sentinel or a chunk carrying `finish_reason` -- and fail the flux when a stream that did deliver chunks ends with neither. The error carries an EOFException cause so the existing retry classification treats it as an I/O truncation and retries it instead of surfacing a permanent failure. The built-in transports terminate on `[DONE]` themselves, so in practice `finish_reason` is the signal; the `[DONE]` check covers custom transports that pass the marker through.
1 task done
Collaborator
|
请和 #4042 收敛成一个修复。JDK 和 OkHttp 的内置 transport 都会在 请分别用 JDK 和 OkHttp 的真实 transport 覆盖三种情况。仅有 |
Contributor
Author
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.
Problem
Reported in #3808. When a streaming chat completion is cut mid-generation — a gateway or provider closes the connection early, so the SSE body simply ends without
[DONE]and without any chunk carryingfinish_reason— the whole stack treated the partial content as a normal completion:JdkHttpTransport.processStreamResponsereads the body throughreader.lines(), so a clean server-side FIN is anonComplete, not anonError;OpenAIClientconsumed[DONE]viatakeWhile(...)and never checked whether it had arrived;finish_reason.The result: no error, no warning, no retry, and the truncated prefix persisted and delivered as the assistant's answer. In a multi-tenant gateway deployment that surfaces as users silently receiving half answers, indistinguishable from a model that "just said less".
Change
OpenAIClient.streamnow tracks the two markers that prove the provider finished the response:[DONE]sentinel, andfinish_reason.When a stream that did deliver chunks completes with neither, the flux fails with an
OpenAIExceptioninstead of completing:The error carries an
EOFExceptioncause, soExecutionConfig.RETRYABLE_ERRORSclassifies it as an I/O truncation and the existing streaming-retry machinery re-issues the request rather than surfacing a permanent failure.Nothing else moves: the same chunks are delivered, in the same order, with the same parsing and the same MiniMax-style summary-event handling; and a stream that ends with zero chunks is left alone, because that is a different failure mode (empty body) and this check is about a truncated prefix being mistaken for a complete answer.
One implementation note worth stating plainly: both built-in transports (
JdkHttpTransport,OkHttpTransport) already terminate their flux on[DONE]and never hand the marker to the client, so on the standard pathsfinish_reasonis the signal that actually fires. The[DONE]check covers customHttpTransportimplementations that pass the marker through.Tests
OpenAIStreamTerminationTest(5 cases,agentscope-extensions-model-openai):main[DONE]then a keep-alive connection stays open[DONE], nofinish_reasonfinish_reasonchunk, no[DONE][DONE], nofinish_reasonchunkJdkHttpTransportagainst MockWebServerThe two truncation cases fail on
mainwithwhich is the reported symptom in one line: the stream completes where it should fail.
OpenAIChatModelStreamingRetryTest.successStreamResponsesent a content chunk and[DONE]with nofinish_reason, which a real OpenAI stream always has on its last chunk; the fixture now emits that terminating chunk, and that is also what the check relies on.Verification
mvn -B -pl agentscope-extensions/agentscope-extensions-model/agentscope-extensions-model-openai test: 595 tests, 0 failures, 0 errors, 11 skipped.spotless:check: clean.Fixes #3808.