Skip to content

fix(models): terminate OpenAI stream on [DONE] sentinel#2104

Open
logicwu0 wants to merge 1 commit into
agentscope-ai:mainfrom
logicwu0:fix/openai-stream-done-terminator
Open

fix(models): terminate OpenAI stream on [DONE] sentinel#2104
logicwu0 wants to merge 1 commit into
agentscope-ai:mainfrom
logicwu0:fix/openai-stream-done-terminator

Conversation

@logicwu0

Copy link
Copy Markdown

AgentScope-Java Version

2.0.1-SNAPSHOT (main)

Description

Background. OpenAIClient#stream treats the SSE [DONE] sentinel as a data line to be discarded (.filter(data -> !data.equals("[DONE]"))) rather than as the stream-termination signal. Nothing completes the Flux when [DONE] arrives, so it only completes when the underlying HTTP response body reaches EOF — i.e. when the server actually closes the connection.

Impact. Many OpenAI-compatible gateways (LiteLLM-style proxies, corporate model gateways, nginx-fronted vLLM, …) send data: [DONE] and then keep the connection open (HTTP keep-alive) until an idle timeout (~60s). As a result all tokens arrive in real time, but the model call's reactive completion is delayed by up to a minute: the ReAct turn does not advance, AgentResultEvent is not emitted, and agent.streamEvents(...)'s onComplete fires ~60s after the last token — a finished agent looks "still running". Direct api.openai.com connections mask this because the server closes promptly.

Change. Replace filter with takeWhile so [DONE] completes the stream deterministically, regardless of connection lifecycle. This matches the OpenAI Python/JS SDKs, which both treat [DONE] as the terminator. One-line logic change in OpenAIClient#stream.

How to test. Added OpenAIStreamTerminationTest: a fake HttpTransport emits two content chunks + [DONE], then Flux.never() (simulating a gateway that never closes the connection). StepVerifier asserts the stream completes within 5s.

  • Before the fix: the stream never completes → the test times out (~5.4s, VerifySubscriber timed out).
  • After the fix: it completes in ~0.4s.

Full module suite: Tests run: 379, Failures: 0, Errors: 0, Skipped: 10.

Closes #2073

Checklist

  • Code has been formatted with mvn spotless:apply
  • All tests are passing (mvn test)
  • Javadoc comments are complete and follow project conventions
  • Related documentation has been updated (e.g. links, examples, etc.) — N/A (behavioral bug fix, no doc surface)
  • Code is ready for review

OpenAIClient#stream filtered out the SSE `[DONE]` line instead of using it
to complete the Flux, so completion was deferred until the underlying HTTP
connection reached EOF. Against OpenAI-compatible gateways that keep the
connection alive after `[DONE]` (LiteLLM-style proxies, nginx-fronted vLLM,
etc.), the reactive stream stalled until the idle timeout (~60s): all tokens
arrived promptly but the ReAct turn never advanced and AgentResultEvent /
onComplete fired ~60s late, making a finished agent look "still running".

Replace filter with takeWhile so `[DONE]` completes the stream
deterministically, matching the OpenAI Python/JS SDK behaviour. Add
OpenAIStreamTerminationTest with a fake transport that emits chunks + `[DONE]`
then never closes; it times out before the fix and completes promptly after.

Closes agentscope-ai#2073
@logicwu0 logicwu0 requested a review from a team July 10, 2026 08:41
@CLAassistant

CLAassistant commented Jul 10, 2026

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@codecov

codecov Bot commented Jul 10, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] OpenAI streaming: [DONE] is filtered out instead of terminating the stream — completion delayed until upstream closes the connection

2 participants