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

Showing 3 changed files with 59 additions and 0 deletions.

x
0 of 3 viewed, 3 left
5 additions and 0 deletions.changeset/omitted-thinking-block-order.mdAdded
View file
@@ -0,0 +1,5 @@
+---
+'@tanstack/ai': patch
+---
+​
+Keep a turn's block order when a thinking block arrives as an empty delta and then its signature, as Anthropic streams thinking whose text is omitted. The empty delta noted the step's position a second time, which dropped the order. A thinking block that followed a provider-executed tool (web_search, web_fetch) was then replayed before that tool, and Anthropic refused its signature with a 400.
4 additions and 0 deletionspackages/ai/src/activities/chat/index.tsModified
View file
@@ -2853,6 +2853,10 @@
private handleReasoningMessageContentEvent(
chunk: Extract<StreamChunk, { type: 'REASONING_MESSAGE_CONTENT' }>,
): void {
+ // An empty delta (a block whose text the provider omits, then signs) must
+ // not note a position: the signature that follows would note a second one,
+ // and the unfilled placeholder drops the turn's block order.
+ if (chunk.delta === '') return
this.noteThinkingStepPosition()
this.currentThinkingContent += chunk.delta
50 additions and 0 deletionspackages/ai/tests/chat-provider-executed-thinking-order.test.tsModified
View file
@@ -447,6 +447,56 @@
​
+ it('keeps the provider block order when the thinking text is omitted', async () => {
+ // Anthropic with `display: "omitted"` (the default of adaptive thinking on
+ // the newest models) streams each thinking block as one empty delta and