Skip to content
Source
Sign inSign up
TanStack/ai

`reload()` after a failed `addToolResult` follow-up run drops the client tool result #2272

OpenSuzii(GitHub profile, opens in a new tab) opened this issue 0 comments

TanStack AI version

@tanstack/ai-client 1.1.0, @tanstack/ai 1.2.0. reload() is unchanged on main (checked 2026-11-27).

Framework/Library version

Node script, no framework involved (the app uses @tanstack/ai-react 1.0.2).

Describe the bug and the steps to reproduce it

reload() is documented as removing the answer after the last user message and running that message again. When the run that failed was the follow-up run started by addToolResult (a client tool result), that cut also removes the assistant message holding the tool result, so the retried request no longer contains it. The failed run's input is not resent.

Steps:

  1. The user sends a message. The run ends with a client tool call (for example an approval).
  2. The tool is answered with addToolResult, which starts the follow-up run. Its input ends with the tool message.
  3. That follow-up run fails before the server accepted it (a network error or a 5xx). The processor adds the empty assistant message with stopReason: 'error'.
  4. The app offers a retry and calls reload(). It removes everything after the last user message (the assistant message holding the tool call and its result, and the error row) and streams again.
  5. The retried request is [user]. It does not contain the tool result.

A server that relies on the request input for new client results never receives it, so the model sees the call as unanswered: it asks again, or, for a client tool with a side effect that already ran in the browser, it may run it a second time.

Expected: a way to retry a failed follow-up run with the same input it had, for example a retry that only drops the trailing error row. reload() can keep its current meaning.

Reproduction, a Node script against the real ChatClient with a connection that records what each run sends (no server needed):

import { ChatClient } from '@tanstack/ai-client'
import { uiMessagesToWire } from '@tanstack/ai/client'

const sent = []
let call = 0
const connection = {
  hydrate: async () => ({ messages: [], activeRun: null, interrupts: null }),
  async *connect(messages, _data, _signal, ctx) {
    call++
    sent.push(uiMessagesToWire(messages).map((m) => `${m.role}:${m.id}`))
    if (call === 1) {
      // run 1 ends with a client tool call
      yield { type: 'RUN_STARTED', runId: ctx.runId, threadId: ctx.threadId }
      yield { type: 'TOOL_CALL_START', toolCallId: 'tc1', toolCallName: 'approve', parentMessageId: 'a1' }
      yield { type: 'TOOL_CALL_ARGS', toolCallId: 'tc1', delta: '{}' }
      yield { type: 'TOOL_CALL_END', toolCallId: 'tc1' }
      yield { type: 'RUN_FINISHED', runId: ctx.runId, threadId: ctx.threadId, finishReason: 'tool_calls' }
      return
    }
    if (call === 2) {
      // the follow-up run carrying the tool result fails before the server accepted it
      yield { type: 'RUN_ERROR', message: 'transport failure' }
      return
    }
    yield { type: 'RUN_STARTED', runId: ctx.runId, threadId: ctx.threadId }
    yield { type: 'RUN_FINISHED', runId: ctx.runId, threadId: ctx.threadId, finishReason: 'stop' }
  },
}

const client = new ChatClient({
  connection,
  persistence: true,
  threadId: 't1',
  tools: [{ name: 'approve', description: 'approve', __toolSide: 'client' }],
})
client.attach()

await client.sendMessage('do it')
await client.addToolResult({ toolCallId: 'tc1', tool: 'approve', output: { approved: true }, state: 'output-available' })
await client.reload()

console.log(sent)
// run 1: [ 'user:…' ]
// run 2: [ 'user:…', 'assistant:a1', 'tool:tool-tc1' ]   <- carries the tool result; this run fails
// run 3 (reload): [ 'user:…' ]                          <- the tool result is gone

Possible fix: I would not change what reload() does for a normal answer. A narrow option: when the last message is the empty stopReason: 'error' row directly after a client tool result, retry without cutting (drop only the error row), either behind reload({ truncate: false }) or as a separate method. Cutting after the last tool-result part in general would change regenerate for every successful tool-using answer.

Current workaround: remove the error row with setMessagesManually and call addToolResult again with the same result.

See the script in the description (runs in Node against the published packages).

Screenshots or Videos (Optional)

No response

Do you intend to try to help solve this bug with your own PR?

No, because I do not have time to dig into it

Terms & Code of Conduct

  • I agree to follow this project's Code of Conduct
  • I understand that if my bug cannot be reliable reproduced in a debuggable environment, it will probably not be fixed and this issue may even be closed.
  1. tanstack-source added labelsbug

  2. github-actions[bot](GitHub profile, opens in a new tab) added labelswaiting-on: maintainer

Sign in to join this conversation.