Skip to content
Source
Sign inSign up
TanStack/ai

fix(ai): keep block order when thinking arrives as an empty delta then a signature #2260

MergedAlexDemzz(GitHub profile, opens in a new tab)merged 2 commits intomain fromrefs/pull/2260/headmerged by AlemTuzlak View on GitHub

AlexDemzz(GitHub profile, opens in a new tab)commented

Anthropic streams a thinking block whose text is omitted (display: "omitted", the default of adaptive thinking on the newest models) as one empty REASONING_MESSAGE_CONTENT delta, then its signature. The engine noted that step's position twice, lost the turn's block order, and replayed a thinking block that followed a provider-executed web_search before that search. Anthropic then refuses the next request with a 400.

Summary

 handleReasoningMessageContentEvent(chunk)
+  if chunk.delta is empty
+    return              // the signature that follows notes the position
   noteThinkingStepPosition()
   currentThinkingContent += chunk.delta

How the order got lost:

REASONING_MESSAGE_CONTENT  delta ""     → noteThinkingStepPosition → placeholder #1 (content and signature still empty)
REASONING_ENCRYPTED_VALUE  signature    → noteThinkingStepPosition → placeholder #2 (content still empty)
finalizeCurrentThinkingStep            → fills #2 only; #1 stays at index -1
buildOrderedAssistantSegments          → accumulatedThinking[-1] is undefined → returns null
singleMessageBlockOrder                → thinking count mismatch → {}
→ one assistant message: [thinking a, thinking b, srvtoolu_1, call_1]

Anthropic then answers:

400 invalid_request_error: messages.N.content.1: Invalid `signature` in `thinking` block.
The block is bound to a different conversation. … Content that preceded this block
when it was created is missing from this request

The same step also fixes a step whose only content is an empty delta and that never gets a signature: it used to leave a placeholder no step would fill.

Files:

  • packages/ai/src/activities/chat/index.ts: handleReasoningMessageContentEvent returns early on an empty delta.
  • packages/ai/tests/chat-provider-executed-thinking-order.test.ts: the server-tool continuation case with omitted thinking (reasoning(step, '', sig)).
  • Changeset: @tanstack/ai patch.

This is a different gap from the drop_block changeset already on main for @tanstack/ai-anthropic. That one only applies when thinking is set on the request. A request that sends no thinking field, so the model's adaptive default applies, still gets the misordered replay.

Evidence

  • Before (main): the new test fails. Both signatures and both calls land in one assistant message:
    expected [{ thinking: ['sig-a'], toolCalls: ['srvtoolu_1'] }, { thinking: ['sig-b'], toolCalls: ['call_1'] }]
    received [{ thinking: ['sig-a', 'sig-b'], toolCalls: ['srvtoolu_1', 'call_1'] }]
    Seen in production with @tanstack/ai 1.8.0 + @tanstack/ai-anthropic 1.2.1 on claude-sonnet-5-5: thinking, 4 × web_search, thinking, then a tool call. Every retry of the same chat failed with the 400 above.
  • After: vitest run in packages/ai: 168 files, 2826 tests passed. tsc is clean. oxlint src --type-aware reports no errors, only the warnings already on main. oxfmt --check passes on the changed files.

Merge Danger

Door: two-way

A one-line early return. Reverting it restores the old behavior.

Blast Radius: narrow

Only REASONING_MESSAGE_CONTENT chunks with an empty delta change behavior. They added nothing to the content, so the only effect is that they no longer reserve a block-order slot. Thinking that streams real text is unchanged.

✅ Checklist

  • I have followed the steps in the Contributing guide.
  • I have tested code changes locally with pnpm run test:pr, or these tests do not apply to this pull request. (Ran test:lib, test:types and test:oxlint for @tanstack/ai only.)
  • I fully understand the code in this pull request, including any code generated with AI assistance.
  • Docs: I updated docs/ for this change, or this change is not user-facing.
  • Changeset: I added a changeset (pnpm changeset), or this PR does not change a published package.

🚀 Release Impact

  • This change affects published code, and I have generated a changeset.
  • This change is docs/CI/dev-only (no release).

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes
    • Fixed an issue where empty reasoning updates could disrupt the order of tool calls and thinking blocks in provider history, causing requests to be rejected when thinking text was omitted. Thinking signatures and their associated tool-call ordering are now preserved in subsequent model requests, including when reasoning blocks contain no visible text.
    1. Pushedee86ba6

    2. nx-cloud[bot](GitHub profile, opens in a new tab)commented

      View your CI Pipeline Execution ↗ for commit ee86ba6aefca891e75cbfa26acfc6613c12f5873

      CommandStatusDurationResult
      nx run-many --targets=build --exclude=examples/...✅ Succeeded4sView ↗

      💡 With Ultracache enabled, Nx Cloud learns each task's file inputs in CI and uses them on later runs. Turn on Ultracache in workspace settings ↗ · Read docs ↗


      ☁️ Nx Cloud last updated this comment at 2026-11-30 09:35:47 UTC

    3. AlemTuzlak approved these changes

    4. AlemTuzlak merged this7cfd902

    5. AlemTuzlak closed this

Sign in to join this conversation.