Skip to content

fix(openai): fail a streamed response that never proves it finished - #4100

Closed
wahllllll wants to merge 1 commit into
agentscope-ai:mainfrom
wahllllll:fix/issue-3808-truncated-stream-failure
Closed

wahllllll wants to merge 1 commit into
agentscope-ai:mainfrom
wahllllll:fix/issue-3808-truncated-stream-failure

Conversation

@wahllllll

Copy link
Copy Markdown
Contributor

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 carrying finish_reason — the whole stack treated the partial content as a normal completion:

  • JdkHttpTransport.processStreamResponse reads the body through reader.lines(), so a clean server-side FIN is an onComplete, not an onError;
  • OpenAIClient consumed [DONE] via takeWhile(...) and never checked whether it had arrived;
  • no chunk was checked for a 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.stream now tracks the two markers that prove the provider finished the response:

  • the SSE [DONE] sentinel, and
  • a chunk whose choice carries a non-null finish_reason.

When a stream that did deliver chunks completes with neither, the flux fails with an OpenAIException instead of completing:

Streaming response ended before completion: the stream closed without the [DONE]
sentinel and no chunk reported a finish_reason, so the partial response is truncated

The error carries an EOFException cause, so ExecutionConfig.RETRYABLE_ERRORS classifies 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 paths finish_reason is the signal that actually fires. The [DONE] check covers custom HttpTransport implementations that pass the marker through.

Tests

OpenAIStreamTerminationTest (5 cases, agentscope-extensions-model-openai):

Case On main With this PR
[DONE] then a keep-alive connection stays open completes unchanged
content chunks, connection closes, no [DONE], no finish_reason completes silently error, retryable
content chunks then a finish_reason chunk, no [DONE] completes unchanged
content then [DONE], no finish_reason chunk completes unchanged
same truncation over the real JdkHttpTransport against MockWebServer completes silently error

The two truncation cases fail on main with

expectation "expectErrorSatisfies" failed (expected: onError(); actual: onComplete())

which is the reported symptom in one line: the stream completes where it should fail.

OpenAIChatModelStreamingRetryTest.successStreamResponse sent a content chunk and [DONE] with no finish_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.

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.
@juruoO-o

Copy link
Copy Markdown
Collaborator

请和 #4042 收敛成一个修复。JDK 和 OkHttp 的内置 transport 都会在 OpenAIClient 看到之前消费 [DONE]。我用本地 HTTP mock 验证过,收到正文和 [DONE]、但没有 finish_reason 的流,在 main 上两种 transport 均正常完成,在这个 PR 上均被判成截断。

请分别用 JDK 和 OkHttp 的真实 transport 覆盖三种情况。仅有 [DONE] 时正常完成,仅有 finish_reason 时正常完成,已经收到正文且两者都缺失时报告截断。

@wahllllll

Copy link
Copy Markdown
Contributor Author

谢谢 @juruoO-o 的 mock 实验,把设计问题定性了:内置 JDK/OkHttp transport 都会在 OpenAIClient 之前消费 [DONE],客户端层无法区分「正常结束」和「连接被切断」,这层检测走不通。同意收敛,关闭本 PR,修复以 #4042 为准(在 marker 真正被观察到的 transport 层执行,且已统一 [DONE]/finish_reason 完成语义)。

关闭前把本 PR 验证过的一个缺口移交到 #4042:映射后的截断错误目前不可重试(详见 #4042 里的评论),rebase 时可顺手收编。

@wahllllll wahllllll closed this Nov 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

P0 重要紧急

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Streaming response terminated without [DONE]/finish_reason is silently accepted as a complete answer

3 participants