OpenAI Responses: chat() waits for EOF after response.completed #1445
TanStack AI version#
@tanstack/ai:0.58.0@tanstack/ai-openai:0.23.1@tanstack/openai-base:0.10.15- Underlying
openaiSDK:6.41.0
Framework/Library version#
No UI framework. Reproduced with Node.js 24.14.1 and Bun 1.3.12 on Linux.
Describe the bug and the steps to reproduce it#
An OpenAI Responses request made through chat() remains pending after receiving response.completed if the upstream HTTP response body remains open. The public RUN_FINISHED event and completion of the async iterator are delayed until EOF, even though the provider has already reported the response as completed.
This concerns the end of the HTTP response body, not closing a keep-alive TCP connection.
Steps to reproduce
The reproduction injects a custom fetch into createOpenaiChat() and returns a controlled SSE Response. It requires no network requests or real API key and uses only the public chat() API.
- Run
bun install --frozen-lockfilein the reproduction directory. - Run
node repro.mjs(orbun run repro.mjs). - The control case sends
response.completedwithstatus: "completed", a completed text output, and usage, then immediately closes the response body. - The second case sends the identical event but leaves the body open for one second. It records whether
RUN_FINISHEDhas been received and whether iteration has finished, then closes the body to allow clean exit.
Actual behavior
control: completed + EOF { finished: true, runFinished: true, bodyCancelled: false }
completed, body still open (after 1s) { finished: false, runFinished: false, bodyCancelled: false }
after releasing EOF { finished: true, runFinished: true, bodyCancelled: false }
Both runtimes reproduce this result. Holding the response body open delays completion of an otherwise completed model request. A caller awaiting the end of chat() therefore depends on transport EOF in addition to the provider's completion event.
Expected behavior
For this single model request with no tools, receiving response.completed should allow chat() to emit its terminal event and finish without waiting for a later HTTP body EOF. The upstream reader should also be released appropriately.
If waiting for additional data after response.completed is required by the adapter contract, could you clarify what data is expected and how callers should determine that this request has finished?
Relevant source
In OpenAIBaseResponsesTextAdapter.processStreamChunks(), the response.completed branch yields RUN_FINISHED and sets runFinishedEmitted = true, but continues the upstream for await loop.
The chat() layer defers the model's RUN_FINISHED, so the completion event does not reach the public stream while the adapter is waiting for another upstream chunk or EOF.
I searched existing issues, PRs, and discussions. Discussion #375 concerns general mid-stream idle timeouts; this report specifically covers an already-received response.completed event.
Your Minimal, Reproducible Example - (Sandbox Highly Recommended)#
https://gist.github.com/L-1ngg/7b93e8d6254bc5a9b19c69ef1d9fc29a
Screenshots or Videos (Optional)#
Not applicable; deterministic console output is included above.
Do you intend to try to help solve this bug with your own PR?#
Maybe, I'll investigate and start debugging
Terms & Code of Conduct#
- I agree to follow this project's Code of Conduct
- I understand that if my bug cannot be reliable reproduced in a debuggable environment, it will probably not be fixed and this issue may even be closed.
github-actions[bot](GitHub profile, opens in a new tab) assignedtombeckenham
github-actions[bot](GitHub profile, opens in a new tab) added labelswaiting-on: maintainer
AlemTuzlak closed this
Sign in to join this conversation.
A follow-up after checking the SDK behavior and running a fuller event sequence:
The underlying
openaiSDK also waits for the HTTP response body to end. Inopenai-nodev6.41.0, the SSE iterator does not treatresponse.completedas the end of iteration. So this is not a TanStack-only transport behavior, and I am not claiming that it violates an explicit OpenAI protocol requirement.The question is whether the TanStack Responses adapter should finish that model request after processing
response.completed, including its final output and usage and appropriate upstream cleanup. I understand that the outerchat()may still need to execute tools or perform structured-output finalization; forwarding every internalRUN_FINISHEDimmediately would not be a general solution.I also repeated the test with preceding
response.created,response.in_progress, output-item and content-part events, text delta/done events, and finallyresponse.completed. Both Node.js 24.14.1 and Bun 1.3.12 showed the same result while the body remained open:OK.RUN_FINISHED, including usage.chat()had neither emittedRUN_FINISHEDnor finished iteration.One additional observation: when the controlled body errors after
response.completed, the public stream ends withRUN_ERRORinstead ofRUN_FINISHED. Continued reading can therefore affect the final outcome as well as completion latency.These are controlled transport tests, not evidence that the production OpenAI endpoint itself leaves completed response bodies open. The report concerns completion semantics and robustness when body EOF is delayed or followed by a transport error. I have not found a documented Responses event that must arrive after
response.completedto supply missing output or usage; please correct me if the adapter needs to preserve such a case.