Skip to content

fix(gateway): preserve complete streamed chat completions - #115652

Merged
steipete merged 1 commit into
mainfrom
codex/s04-chat-sse-tag-flush-81991
Jul 29, 2026
Merged

steipete merged 1 commit into
mainfrom
codex/s04-chat-sse-tag-flush-81991

Conversation

@steipete

Copy link
Copy Markdown
Contributor

Closes #81991
Related: #82355

What Problem This Solves

Fixes an issue where users streaming Chat Completions from the Gateway receive incomplete assistant responses: a leading character buffered at an XML-style tag boundary can disappear, and the final assistant text can arrive after the stream has already sent its terminal [DONE] frame.

Why This Change Was Made

Preserve the existing sanitized cumulative assistant snapshot when deriving OpenAI-compatible incremental chunks. Schedule stream completion after the current microtask so the agent's same-turn final text is emitted before the single finish and [DONE] frames. Preserve tool-call finish precedence, usage ordering, disconnect cleanup, replacement behavior, and intentional reasoning/final-tag suppression.

The root cause, proposed repair, and contributor-equivalent regression originate in @honor2030's #82355; the original report is #81991 by @boxiao-qd. The Gateway remains a JSON/SSE transport: literal custom tags and script-like text remain unmodified text, not HTML or executable markup.

User Impact

Streaming OpenAI Chat Completions clients receive the complete assistant response in order, including literal leading < characters and the final text, before the normal stop and [DONE] frames. Existing structured tool calls, usage frames, authentication, suppressed reasoning, and non-streaming responses remain unchanged.

Evidence

  • Current-main fail-first on a real loopback Gateway and the official [email protected] SDK: cumulative output loses its leading <, and same-turn final content is emitted after the stream closes; both regressions fail before the production change.
  • The exact pinned SDK streaming contract is Stream<ChatCompletionChunk>, including choices[].delta.content and terminal finish_reason: https://github.com/openai/openai-node/blob/v6.48.0/src/resources/chat/completions/completions.ts. SSE framing is verified against https://html.spec.whatwg.org/multipage/server-sent-events.html.
  • Real official SDK requests prove both split custom tags and literal script-like text are delivered byte-for-byte without changing markup, rendering, or HTML policy.
  • Actual raw Gateway SSE proves the final assistant fragment precedes a single stop frame and terminal [DONE].
  • Contributor-equivalent agent regression proves arbitrary split literal tags survive while the existing 23 adjacent reasoning/final-tag suppression tests remain green.
  • Blacksmith Testbox: pnpm test src/gateway/openai-http.test.ts src/gateway/openresponses-http.test.ts src/gateway/openresponses-parity.test.ts src/gateway/openresponses-phase.test.ts src/agents/embedded-agent-subscribe.subscribe-embedded-agent-session.filters-final-suppresses-output-without-start-tag.test.ts — 110 tests passed.
  • Blacksmith Testbox: pnpm check:changed — formatting, both core typecheck lanes, lint, and repository safety guards.
  • Independent gpt-5.6-sol review with xhigh reasoning.
  • AI-assisted; original report and solution credit preserved for @boxiao-qd and @honor2030.

Preserve cumulative assistant snapshot prefixes and flush same-turn text before terminating OpenAI-compatible Gateway SSE.

Fixes #81991

Refs: #82355

Co-authored-by: honor2030 <[email protected]>
@openclaw-barnacle openclaw-barnacle Bot added gateway Gateway runtime agents Agent runtime and tooling size: S maintainer Maintainer-authored PR labels Jul 29, 2026
@steipete
steipete marked this pull request as ready for review July 29, 2026 06:06
@steipete
steipete merged commit 1425a6c into main Jul 29, 2026
182 of 188 checks passed
@steipete
steipete deleted the codex/s04-chat-sse-tag-flush-81991 branch July 29, 2026 06:33
@steipete

Copy link
Copy Markdown
Contributor Author

Merged via squash.

vincentkoc added a commit that referenced this pull request Jul 29, 2026
…ifecycle-hooks

* origin/main:
  fix(mattermost): key private channels as group on outbound routing (#96645)
  fix(ci): grant Telegram QA runtime scratch access (#115659)
  fix(telegram): stop retrying blocked recipients (#115640)
  fix(tts): persist generated speech in media store (#115647)
  fix(gateway): preserve complete streamed chat completions (#115652)
  fix(mac): prevent app auth from blocking node-host startup (#115533)
  fix(heartbeat): keep message-tool final replies private (#115629)
  fix(gateway): drain root work continuations before direct-stop process exit (#105848)
  fix(ollama): keep active streams within their idle timeout (#115648)
  fix(delivery): keep session keys out of inferred message targets (#115657)
  fix(ci): type code mode matrix catch callback (#115674)
  refactor(agents): centralize bundled CLI backend auth policy (#115633)
  refactor: remove 3,527 lines of runtime and test duplication (#115626)
  [AI] fix(doctor): stop repeated bundledDiscovery migration noise on beta (#115208)

# Conflicts:
#	scripts/code-mode-model-matrix.ts
github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Jul 30, 2026
…15652)

Preserve cumulative assistant snapshot prefixes and flush same-turn text before terminating OpenAI-compatible Gateway SSE.

Fixes openclaw#81991

Refs: openclaw#82355

Co-authored-by: honor2030 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agents Agent runtime and tooling gateway Gateway runtime maintainer Maintainer-authored PR size: S

Projects

None yet

Development

Successfully merging this pull request may close these issues.

HTTP SSE (/v1/chat/completions) drops leading '<' from streamed content and closes connection before final chunk

1 participant