-
-
Notifications
You must be signed in to change notification settings - Fork 186
fix(ai, ai-anthropic): thinking blocks missing on turn 2+ in tool loops #391
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
ea96c3d
c042c55
2019809
c31d45d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| --- | ||
| '@tanstack/ai': patch | ||
| '@tanstack/ai-anthropic': patch | ||
| '@tanstack/ai-client': patch | ||
| --- | ||
|
|
||
| **Fix thinking blocks getting merged across steps and lost on turn 2+ of Anthropic tool loops.** | ||
|
|
||
| Each thinking step emitted by the adapter now produces its own `ThinkingPart` on the `UIMessage` instead of being merged into a single part, and thinking content + Anthropic signatures are preserved in server-side message history so multi-turn tool flows with extended thinking work correctly. | ||
|
|
||
| `@tanstack/ai`: | ||
|
|
||
| - `ThinkingPart` gains optional `stepId` and `signature` fields. | ||
| - `ModelMessage` gains an optional `thinking?: Array<{ content; signature? }>` field so prior thinking can be replayed in subsequent turns. | ||
| - `StepFinishedEvent` gains an optional `signature` field for provider-supplied thinking signatures. | ||
| - `StreamProcessor` tracks thinking per-step via `stepId` and keeps step ordering. `getState().thinking` / `getResult().thinking` concatenate step contents in order. | ||
| - The `onThinkingUpdate` callback on `StreamProcessorEvents` now receives `(messageId, stepId, content)` — consumers implementing it directly must add the `stepId` parameter. | ||
| - `TextEngine` accumulates thinking + signatures per iteration and includes them in assistant messages with tool calls so the next turn can replay them. | ||
|
|
||
| `@tanstack/ai-anthropic`: | ||
|
|
||
| - Captures `signature_delta` stream events and emits the final `STEP_FINISHED` with the signature on `content_block_stop`. | ||
| - Includes thinking blocks with signatures in `formatMessages` for multi-turn history. | ||
| - Passes `betas: ['interleaved-thinking-2025-05-14']` to the `beta.messages.create` call site when a thinking budget is configured. The beta flag is scoped to the streaming path only, so `structuredOutput` (which uses the non-beta `messages.create` endpoint) is unaffected. | ||
|
|
||
| `@tanstack/ai-client`: | ||
|
|
||
| - `ChatClient`'s internal `onThinkingUpdate` wiring is updated for the new `stepId` parameter. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -165,6 +165,7 @@ function isToolCallIncluded(part: ToolCallPart): boolean { | |
| function buildAssistantMessages(uiMessage: UIMessage): Array<ModelMessage> { | ||
| const messageList: Array<ModelMessage> = [] | ||
| let current = createSegment() | ||
| let pendingThinking: Array<{ content: string; signature?: string }> = [] | ||
|
|
||
|
Comment on lines
+168
to
169
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This still loses thinking/tool-call order within a single turn.
Also applies to: 233-239 🤖 Prompt for AI Agents |
||
| // Track emitted tool result IDs to avoid duplicates. | ||
| // A tool call can have BOTH an explicit tool-result part AND an output | ||
|
|
@@ -181,7 +182,9 @@ function buildAssistantMessages(uiMessage: UIMessage): Array<ModelMessage> { | |
| role: 'assistant', | ||
| content, | ||
| ...(hasToolCalls && { toolCalls: current.toolCalls }), | ||
| ...(pendingThinking.length > 0 && { thinking: pendingThinking }), | ||
| }) | ||
| pendingThinking = [] | ||
| } | ||
| current = createSegment() | ||
| } | ||
|
|
@@ -227,7 +230,15 @@ function buildAssistantMessages(uiMessage: UIMessage): Array<ModelMessage> { | |
| } | ||
| break | ||
|
|
||
| // thinking parts are skipped - they're UI-only | ||
| case 'thinking': | ||
| if (part.content) { | ||
| pendingThinking.push({ | ||
| content: part.content, | ||
| ...(part.signature && { signature: part.signature }), | ||
| }) | ||
| } | ||
| break | ||
|
|
||
| default: | ||
| break | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Replay signed thinking for non-tool assistant messages too.
Line 407 only preserves thinking inside the
assistant && toolCallsbranch. Assistant responses that contain signed thinking but no tool calls fall through to lines 468-478, so theirmessage.thinkingis dropped from future Anthropic context.🐛 Proposed fix
Also applies to: 468-478
🤖 Prompt for AI Agents