fix(models): terminate OpenAI stream on [DONE] sentinel#2104
Open
logicwu0 wants to merge 1 commit into
Open
Conversation
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
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
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.
AgentScope-Java Version
2.0.1-SNAPSHOT (main)
Description
Background.
OpenAIClient#streamtreats 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 theFluxwhen[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,AgentResultEventis not emitted, andagent.streamEvents(...)'sonCompletefires ~60s after the last token — a finished agent looks "still running". Directapi.openai.comconnections mask this because the server closes promptly.Change. Replace
filterwithtakeWhileso[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 inOpenAIClient#stream.How to test. Added
OpenAIStreamTerminationTest: a fakeHttpTransportemits two content chunks +[DONE], thenFlux.never()(simulating a gateway that never closes the connection).StepVerifierasserts the stream completes within 5s.VerifySubscriber timed out).Full module suite:
Tests run: 379, Failures: 0, Errors: 0, Skipped: 10.Closes #2073
Checklist
mvn spotless:applymvn test)