Skip to content
Source
Sign inSign up
TanStack/ai

ai-anthropic: truncated tool-call arguments are sent as a string tool_use.input, which breaks the conversation #1582

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

Hi! We hit a case where one interrupted tool call permanently breaks a chat thread on the Anthropic adapter.

Versions: @tanstack/ai 0.58.0, @tanstack/ai-anthropic 0.18.11 (also present on main at packages/ai-anthropic/src/adapters/text.ts)

What happens

When a stream ends while a tool call's arguments are still streaming (the user stops generation, the connection drops, or the model hits max tokens), the stream processor marks that tool call input-complete and keeps the partial arguments string (stream/processor.js, completeToolCall). Because the state is input-complete, the tool call is included when the history is converted back into model messages (messages.js, isToolCallIncluded).

On the next request, AnthropicTextAdapter.formatMessages tries to JSON.parse those arguments, fails, and falls back to the raw string:

let parsedInput: unknown = {}
try {
  const parsed = toolCall.function.arguments
    ? JSON.parse(toolCall.function.arguments)
    : {}
  parsedInput = parsed && typeof parsed === 'object' ? parsed : {}
} catch {
  parsedInput = toolCall.function.arguments // string
}

Anthropic rejects the request with 400 messages.N.content.M.tool_use.input: Input should be an object. Since the bad message stays in history, every later turn in that conversation fails the same way.

Repro

import { AnthropicTextAdapter } from '@tanstack/ai-anthropic'

const adapter = new AnthropicTextAdapter({ apiKey: 'x' }, 'claude-sonnet-4-5')
const out = adapter.formatMessages([
  { role: 'user', content: 'hi' },
  {
    role: 'assistant',
    content: '',
    toolCalls: [{ id: 'toolu_1', type: 'function', function: { name: 'get_agent', arguments: '{"agentId": "abc' } }],
  },
  { role: 'tool', toolCallId: 'toolu_1', content: 'ok' },
])
// tool_use.input === '{"agentId": "abc'   (string)

For comparison, valid JSON, an empty string and a non-object JSON value ("hello") all produce an object.

Expected

tool_use.input should always be an object. The catch branch could fall back to {}, the same as the non-object branch just above it. Alternatively, tool calls whose arguments never finished parsing could be excluded from history, or marked error instead of input-complete, when a stream is finalized.

Related: #265 / #266 fixed the null input case in the same area.

Happy to open a PR for the {} fallback if that's the preferred fix.

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

  2. tombeckenham closed this

Sign in to join this conversation.