ai-anthropic: truncated tool-call arguments are sent as a string tool_use.input, which breaks the conversation
维护者通常 1 天内回复
评估
- 难度
- 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 内容生成。
描述
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
环境准备
- 没有 Dockerfile 或 Docker Compose 文件
- 有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
TanStack/ai 的其他 Issue
-
Durable resume through withPersistence never fills ctx.inputResponse for mcp_input interrupts可能已有人在做 @tombeckenham 于 2 天前认领。 未关闭waiting-on: maintainer
维护者通常 1 天内回复
-
ai, ai-persistence: a nested subagent that waits runs again from the start under withPersistence可能已有人在做 @tombeckenham 于 3 天前认领。 未关闭waiting-on: maintainer
维护者通常 1 天内回复
-
ai-ollama: streamed chat() reports finishReason "stop" when done_reason is "length"可能已有人在做 @AlemTuzlak 于 3 天前认领。 未关闭waiting-on: maintainer
维护者通常 1 天内回复
-
Discriminate status-dependent record and option types (RunRecord, SessionIndexEntry, InputState, durable-only options)可能已有人在做 @jherr 于 9 天前认领。 未关闭enhancement waiting-on: maintainer
维护者通常 1 天内回复
-
move to modelschemas可能已有人在做 @jherr 于 10 天前认领。 未关闭has-pr waiting-on: maintainer
维护者通常 1 天内回复
相似的 Issue
-
难度 2/5 1-3 小时 新手友好度 68/100
awesome-dsh-plugin/awesome-dsh-plugin#9184 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 75/100
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 85/100
farion1231/cc-switch#8829 ·
维护者通常 1 天内回复
-
[core-client] Remove unused hand-maintained transport helpers after the definition-table cutover未关闭doc-audit enhancement
难度 2/5 1-3 小时 新手友好度 88/100
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 85/100