-
-
Notifications
You must be signed in to change notification settings - Fork 126
fix: anthropic tool call issues #275
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
Draft
jherr
wants to merge
7
commits into
main
Choose a base branch
from
patch/anthropic-tool-calls
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+3,128
β479
Draft
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
35652a0
fix: anthropic tool call issues
jherr 9cd7717
fixing pnpm lock
jherr 6842032
ci: apply automated fixes
autofix-ci[bot] 4ccfe30
reworking model to uimessage conversions
jherr f74105d
simplifying the message conversion handling
jherr a2002d9
merging in auto-fixes
jherr 713b661
ci: apply automated fixes
autofix-ci[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| --- | ||
| '@tanstack/ai': patch | ||
| '@tanstack/ai-client': patch | ||
| '@tanstack/ai-anthropic': patch | ||
| '@tanstack/ai-gemini': patch | ||
| --- | ||
|
|
||
| fix(ai, ai-client, ai-anthropic, ai-gemini): fix multi-turn conversations failing after tool calls | ||
|
|
||
| **Core (@tanstack/ai):** | ||
|
|
||
| - Lazy assistant message creation: `StreamProcessor` now defers creating the assistant message until the first content-bearing chunk arrives (text, tool call, thinking, or error), eliminating empty `parts: []` messages from appearing during auto-continuation when the model returns no content | ||
| - Add `prepareAssistantMessage()` (lazy) alongside deprecated `startAssistantMessage()` (eager, backwards-compatible) | ||
| - Add `getCurrentAssistantMessageId()` to check if a message was created | ||
| - **Rewrite `uiMessageToModelMessages()` to preserve part ordering**: the function now walks parts sequentially instead of separating by type, producing correctly interleaved assistant/tool messages (text1 + toolCall1 β toolResult1 β text2 + toolCall2 β toolResult2) instead of concatenating all text and batching all tool calls. This fixes multi-round tool flows where the model would see garbled conversation history and re-call tools unnecessarily. | ||
| - Deduplicate tool result messages: when a client tool has both a `tool-result` part and a `tool-call` part with `output`, only one `role: 'tool'` message is emitted per tool call ID | ||
|
|
||
| **Client (@tanstack/ai-client):** | ||
|
|
||
| - Update `ChatClient.processStream()` to use lazy assistant message creation, preventing UI flicker from empty messages being created then removed | ||
|
|
||
| **Anthropic:** | ||
|
|
||
| - Fix consecutive user-role messages violating Anthropic's alternating role requirement by merging them in `formatMessages` | ||
| - Deduplicate `tool_result` blocks with the same `tool_use_id` | ||
| - Filter out empty assistant messages from conversation history | ||
| - Suppress duplicate `RUN_FINISHED` event from `message_stop` when `message_delta` already emitted one | ||
| - Fix `TEXT_MESSAGE_END` incorrectly emitting for `tool_use` content blocks | ||
| - Add Claude Opus 4.6 model support with adaptive thinking and effort parameter | ||
|
|
||
| **Gemini:** | ||
|
|
||
| - Fix consecutive user-role messages violating Gemini's alternating role requirement by merging them in `formatMessages` | ||
| - Deduplicate `functionResponse` parts with the same name (tool call ID) | ||
| - Filter out empty model messages from conversation history |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
TEXT_MESSAGE_ENDmay emit spuriously for non-text, non-tool_use block types (e.g.,thinking).The
elsebranch fires for anycontent_block_stopwherecurrentBlockType !== 'tool_use', which includesthinkingblocks. If a text block preceded the thinking block, this would emit a spuriousTEXT_MESSAGE_ENDwhen the thinking block stops β becausehasEmittedTextMessageStartandaccumulatedContentare both still truthy.In practice Anthropic puts thinking blocks before text blocks, so this isn't currently triggered. But if the response ordering changes or new block types are added, it could surface.
Suggested guard
} else { - // Emit TEXT_MESSAGE_END only for text blocks (not tool_use blocks) - if (hasEmittedTextMessageStart && accumulatedContent) { + // Emit TEXT_MESSAGE_END only when a text block ends + if ( + currentBlockType === 'text' && + hasEmittedTextMessageStart && + accumulatedContent + ) { yield { type: 'TEXT_MESSAGE_END', messageId, model, timestamp, } } }π€ Prompt for AI Agents