Hacktoberfest 2026:维护者为十月标记出来的 issue,仍然开放、适合新手。 浏览 Hacktoberfest issue

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

已关闭 适合新手
#1,582 0 条评论 0 个 reaction 已指派 1 人 在 GitHub 查看

维护者通常 1 天内回复

@tombeckenham 已经在做这个了。

开始于 2026年10月1日。

  • #1583 来自 @Tetnofa —— 未关闭

评估

难度
2/5
预计耗时
1-3 小时
新手友好度
78/100
Issue 类型
缺陷
描述清晰度
基本清楚
活跃度
活跃
技术栈
typescript
领域
api

调研方向

从 packages/ai-anthropic/src/adapters/text.ts 中的 AnthropicTextAdapter.formatMessages 开始,检查格式错误的工具调用参数如何变成 tool_use.input。使用提供的复现来比较截断的、有效的、空的以及非对象参数。完成的标准是 Anthropic 始终为 tool_use.input 接收一个对象,并且相关的适配器测试覆盖该回归问题。

由索引模型根据 Issue 内容生成。

描述

has-pr waiting-on: maintainer

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.

主要语言
TypeScript
星标
3.2k
派生
372
平均合并
1 天 12 小时
30 天内合并 PR
266

环境准备

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

TanStack/ai 的其他 Issue

查看 TanStack/ai 的全部 Issue

相似的 Issue

更多 TypeScript Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。