Skip to content
Source
Sign inSign up
TanStack/ai

OpenAI Responses: chat() waits for EOF after response.completed #1445

ClosedL-1ngg(GitHub profile, opens in a new tab) opened this issue 1 comment

TanStack AI version

  • @tanstack/ai: 0.58.0
  • @tanstack/ai-openai: 0.23.1
  • @tanstack/openai-base: 0.10.15
  • Underlying openai SDK: 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.

  1. Run bun install --frozen-lockfile in the reproduction directory.
  2. Run node repro.mjs (or bun run repro.mjs).
  3. The control case sends response.completed with status: "completed", a completed text output, and usage, then immediately closes the response body.
  4. The second case sends the identical event but leaves the body open for one second. It records whether RUN_FINISHED has 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.

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.
  1. A follow-up after checking the SDK behavior and running a fuller event sequence:

    The underlying openai SDK also waits for the HTTP response body to end. In openai-node v6.41.0, the SSE iterator does not treat response.completed as 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 outer chat() may still need to execute tools or perform structured-output finalization; forwarding every internal RUN_FINISHED immediately 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 finally response.completed. Both Node.js 24.14.1 and Bun 1.3.12 showed the same result while the body remained open:

    • The public stream had received the complete text OK.
    • The adapter had already yielded its internal RUN_FINISHED, including usage.
    • The public chat() had neither emitted RUN_FINISHED nor finished iteration.

    One additional observation: when the controlled body errors after response.completed, the public stream ends with RUN_ERROR instead of RUN_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.completed to supply missing output or usage; please correct me if the adapter needs to preserve such a case.

  2. github-actions[bot](GitHub profile, opens in a new tab) added labelswaiting-on: maintainer

  3. AlemTuzlak closed this

Sign in to join this conversation.