Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changeset/fix-infinite-tool-loop.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@tanstack/ai-client': patch
---

fix: prevent infinite tool call loop when server tool finishes with stop

When the server-side agent loop executes a tool and the model finishes with `finishReason: 'stop'`, the client no longer auto-sends another request. Previously this caused infinite loops with non-OpenAI providers that respond minimally after tool execution.
9 changes: 8 additions & 1 deletion packages/typescript/ai-client/src/chat-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -657,11 +657,18 @@ export class ChatClient {
await this.drainPostStreamActions()

// Continue conversation if the stream ended with a tool result (server tool completed)
// but ONLY if the model indicated it wants to continue (finishReason !== 'stop').
// When finishReason is 'stop', the model is done — don't re-send.
if (streamCompletedSuccessfully) {
const messages = this.processor.getMessages()
const lastPart = messages.at(-1)?.parts.at(-1)
const { finishReason } = this.processor.getState()

if (lastPart?.type === 'tool-result' && this.shouldAutoSend()) {
if (
lastPart?.type === 'tool-result' &&
finishReason !== 'stop' &&
this.shouldAutoSend()
) {
try {
await this.checkForContinuation()
} catch (error) {
Expand Down
Loading