Skip to content
Source
Sign inSign up
TanStack/ai

ai-openrouter: reasoning is not sent back to the model between tool-call iterations #2191

Send to my agent
Opencuevacelis(GitHub profile, opens in a new tab) opened this issue 0 comments

Summary

@tanstack/ai-openrouter (1.1.2, and main at packages/ai-openrouter/src/adapters/text.ts, convertMessage) builds previous assistant messages with only content and toolCalls. The thinking blocks that @tanstack/ai stores on the assistant ModelMessage (thinking?: Array<{ content, signature?, redacted? }>) are dropped, so reasoning is never sent back to the provider in later iterations of the agent loop.

The OpenRouter SDK already accepts it on ChatAssistantMessage (reasoning, reasoningDetails).

Why it matters

  • OpenRouter recommends preserving reasoning during tool use: "Preserving reasoning blocks is useful specifically for tool calling" — https://openrouter.ai/docs/guides/best-practices/reasoning-tokens#preserving-reasoning-blocks
  • DeepSeek V4's thinking mode requires it when tools are present ("the reasoning_content must be fully passed back to the API in all subsequent requests"); their first-party API returns 400 without it — https://api-docs.deepseek.com/guides/thinking_mode
  • Anthropic models need their signed thinking (reasoning_details with signatures) passed back unmodified when using tools.
  • The TanStack AI docs (chat/thinking-content) list the Anthropic, Gemini, Mistral and Bedrock adapters as sending reasoning back; OpenRouter isn't mentioned.

Repro

  1. chat({ adapter: openRouterText('deepseek/deepseek-v4.1-flash'), tools: [someTool], messages }) with a prompt that triggers a tool call.
  2. Inspect the second request of the loop (e.g. via a gateway log): the assistant message with tool_calls has no reasoning / reasoning_details.

Expected

convertMessage forwards reasoning for assistant messages, e.g. the plaintext reasoning field (OpenRouter accepts reasoning / reasoning_content as plaintext), or the original reasoning_details array (unmodified, including signatures) so that Anthropic/encrypted reasoning can round-trip too. The latter probably needs the adapter to keep reasoning_details from the stream, since thinking only keeps text + signature.

Data point (honest caveat)

We tried a minimal override that adds reasoning: thinking.map(t => t.content).join('') for DeepSeek and measured ~1 900 full tool-call turns (streaming), A/B paired. We saw no measurable change in answer quality for our case (≈2.2 % vs 2.3 % of answers with a visible self-correction, p ≈ 0.88). So this is reported as a protocol-compliance gap rather than a confirmed quality bug — but it does mean DeepSeek receives a conversation shape it doesn't expect, and it will hard-fail on providers that enforce it.

Happy to open a PR if a direction is agreed (plaintext reasoning vs. preserving reasoning_details).

Sign in to join this conversation.