TanStack AI version#
v0.6.1
Framework/Library version#
N/A
Describe the bug and the steps to reproduce it#
When using the openRouterText adapter with any model, the stream terminates immediately upon receiving a TOOL_END event, but before the tool is actually called or executed.
const stream = chat({
adapter: openRouterText("minimax/minimax-m2.5"),
messages,
conversationId,
tools: [serverTool],
});
Steps to reproduce:
- Send a prompt that triggers serverTool.
- Monitor the SSE stream.
- Observe TOOL_START followed immediately by TOOL_END.
- Result: The serverTool function is never invoked, and the connection closes.
- Send a second message (e.g., "Why didn't you run the tool?").
- The model acknowledges the previous state and execute the tool
NOTE:
I fixed this in a fork by debugging the stream handling logic. The issue was that the for (const choice of chunk.choices) loop was skipping the final message chunk entirely because the choices array was empty, preventing the tool execution from being triggered.
Your Minimal, Reproducible Example - (Sandbox Highly Recommended)#
https://github.com/andorep/openrouter-error
Screenshots or Videos (Optional)#
No response
Do you intend to try to help solve this bug with your own PR?#
Yes, I think I know how to fix it and will discuss it in the comments of this issue
Terms & Code of Conduct#
Drive-by note, not a maintainer: this no longer reproduces on
main.The failure described here is a stream whose tool call never gets a
finishReasonon a choice and ends with a usage-only chunk carrying an emptychoicesarray, so the loop skipped it viaif (!choice) continueand the tool call was never closed.createOpenRouterText's stream loop inpackages/ai-openrouter/src/adapters/text.tsnow has a post-loop pass that closes any tool call that was started but never got afinishReason.I checked it with the OpenRouter SDK mocked, feeding exactly that shape — two
delta.toolCallschunks withfinishReason: null, then{ choices: [], usage: {...} }:and driving the same stream through
chat()with a tool that has anexecute: the tool ran once and the adapter issued the second request, rather than closing afterTOOL_CALL_END.Worth noting the OpenRouter adapter has its own stream loop rather than extending
OpenAIBaseChatCompletionsTextAdapter, so the coverage for the same shape inpackages/openai-base/tests/chat-completions-empty-choices.test.ts(one of its cases is named for this issue) does not actually exercise this adapter — but the behaviour above is from the OpenRouter adapter directly.