Skip to content
Source
Sign inSign up
TanStack/ai

OpenRouter adapter stops before executing tools #371

Closedandorep(GitHub profile, opens in a new tab) opened this issue 2 comments

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:

  1. Send a prompt that triggers serverTool.
  2. Monitor the SSE stream.
  3. Observe TOOL_START followed immediately by TOOL_END.
  4. Result: The serverTool function is never invoked, and the connection closes.
  5. Send a second message (e.g., "Why didn't you run the tool?").
  6. 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.

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

  • 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. github-actions[bot](GitHub profile, opens in a new tab) added labelswaiting-on: maintainer

  2. 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 finishReason on a choice and ends with a usage-only chunk carrying an empty choices array, so the loop skipped it via if (!choice) continue and the tool call was never closed. createOpenRouterText's stream loop in packages/ai-openrouter/src/adapters/text.ts now has a post-loop pass that closes any tool call that was started but never got a finishReason.

    I checked it with the OpenRouter SDK mocked, feeding exactly that shape — two delta.toolCalls chunks with finishReason: null, then { choices: [], usage: {...} }:

    RUN_STARTED | TOOL_CALL_START | TOOL_CALL_ARGS | TOOL_CALL_ARGS | TOOL_CALL_END | RUN_FINISHED

    and driving the same stream through chat() with a tool that has an execute: the tool ran once and the adapter issued the second request, rather than closing after TOOL_CALL_END.

    Worth noting the OpenRouter adapter has its own stream loop rather than extending OpenAIBaseChatCompletionsTextAdapter, so the coverage for the same shape in packages/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.

  3. yeah this has been long fixed!

  4. AlemTuzlak closed this

Sign in to join this conversation.