langchain-anthropic: `_format_messages` drops `caller` from `server_tool_use` and `*_tool_result` blocks

当你在使用 langchain-anthropic 回放包含 Anthropic 服务端嵌套工具(如 dynamic web search 在 code_execution 内调用)的多轮对话历史时, _format_messages 会把 server_tool_use 和 *_tool_resu

快速结论:当你在使用 langchain-anthropic 回放包含 Anthropic 服务端嵌套工具(如 dynamic web search 在 code_execution 内调用)的多轮对话历史时,_format_messages 会把 server_tool_use 和 *_tool_result 块中的 caller 字段静默丢弃,导致下一轮请求被 API 以 400 拒绝。优先排查:完整历史里是否出现嵌套 server tool 块,以及当前 langchain-anthropic 是否为含修复的版本(1.7.10 仍受此问题影响)。

适用环境:Python 项目中使用 langchain-anthropic(Issue 中报告时最新发布版为 1.7.10),调用 Anthropic 模型(示例模型 claude-opus-5-5)并启用程序化 / 嵌套服务端工具(dynamic web search、web_search_20260318 在 code_execution 内执行搜索)。Issue 未提供操作系统、CUDA、显卡等信息。

最快修复方案:该缺陷已由 PR #41678 在 master 修复(2026-11-05 合并),但晚于 langchain-anthropic 1.7.10 发布(2026-11-04),尚未进入任何正式版本。在下一个版本发布前,可优先尝试从 master 安装该集成包:pip install "git+https://github.com/langchain-ai/langchain.git@master#subdirectory=libs/partners/anthropic"。

注意事项:从 master 安装属于未发布代码,可能不稳定或后续变更,不适合生产环境长期依赖;修复后,caller.type 为 "direct" 的顶层调用仍会被有意去掉 caller,这与 API 对顶层调用的返回一致,属于预期行为而非漏修复;tool_search_tool_result 仍不在本次修复覆盖范围内(跟踪于 #41947,修复于 #41958)。

问题场景

用户在用 langchain-anthropic 调用 Anthropic 模型,并启用了 Anthropic 的程序化 / 嵌套服务端工具:例如 web_search_20260318 在 code_execution 内部执行搜索。模型返回的 server_tool_use 块及其对应的 web_search_tool_result 等结果块上带有 caller 字段。下一轮把这段历史重新发给 API 时,请求被拒。用 _format_messages 单独复现即可看到,所有块中的 caller 都已消失。

报错原文

server_tool_use srvtoolu_code False
server_tool_use srvtoolu_search False
web_search_tool_result srvtoolu_search False
code_execution_tool_result srvtoolu_code False

When this history is replayed to the API on the next turn (dynamic web search, web_search_20260318, which runs searches inside code_execution), the request is rejected with a 400 invalid_request_error because the nested blocks lose the link to the code_execution call that issued them.

原因分析

根因在 langchain_anthropic.chat_models._format_messages:它按白名单复制块内字段。server_tool_use / mcp_tool_use 只保留 type, id, input, name, server_name, cache_control;code_execution_tool_result、web_search_tool_result、web_fetch_tool_result 等结果块只保留 type, content, tool_use_id, is_error, cache_control, retrieved_at。caller 不在任一白名单中,因此被静默丢弃。客户端 tool_use 块已有 if caller := block.get("caller") 分支保留该字段,唯独服务端工具分支遗漏,属于实现疏漏。

在修复后的 master 上(提交 92c7def),server_tool_use / mcp_tool_use 分支会主动保留 caller,*_tool_result 白名单也增加了 or (k == "caller" and v and v.get("type") != "direct") 条件;两者在 caller.type 为 "direct" 时都去掉 caller,与 API 对顶层调用的行为一致。

环境排查

  • 确认 langchain-anthropic 版本:1.7.10 仍使用旧白名单,缺陷存在;修复在 2026-11-05 合并于 master,晚于 1.7.10 的 2026-11-04 发布,因此 1.7.10 及之前版本都不包含修复。
  • 确认实际安装来源:若曾从 master 或某个 commit 安装,检查安装到的 commit 是否包含 PR #41678 的改动。
  • 确认问题历史中是否存在嵌套 server tool:server_tool_use 或 *_tool_result 块上原本应带 caller(形如 {"type": "code_execution_...", "tool_id": "srvtoolu_..."})。
  • 确认模型与工具组合是否为 dynamic web search(web_search_20260318)在 code_execution 内执行;这是 Issue 中触发 400 的具体场景。

解决步骤

  1. 先确认当前问题:用 Issue 中的最小复现脚本,对 [HumanMessage, AIMessage, HumanMessage] 调用 _format_messages,观察输出块中 caller 是否丢失;若输出全为 False,与 Issue 一致。
  2. 检查 langchain-anthropic 版本,确认是否仍为 1.7.10 等修复前的发布版本。
  3. 若需要立即绕过,可优先尝试从 master 安装集成包:pip install "git+https://github.com/langchain-ai/langchain.git@master#subdirectory=libs/partners/anthropic"。
  4. 安装后重新运行第 1 步的复现,对比 master 上的预期结果:嵌套的 server_tool_use(srvtoolu_search)与 web_search_tool_result 应保留 caller;原本就没有 caller 的 code_execution 块仍为 False,属于正常。
  5. 关注官方正式版本发布;等包含 #41678 的版本发布后,升级到正式版并卸载 master 安装源,恢复常规依赖管理。

验证方法

重新运行复现脚本,预期仅嵌套块输出 True:

server_tool_use srvtoolu_code False
server_tool_use srvtoolu_search True
web_search_tool_result srvtoolu_search True
code_execution_tool_result srvtoolu_code False

然后在真实业务中把带嵌套 server tool 的多轮历史重新发送给 API,确认不再出现 400 invalid_request_error;若仍报错,可检查是否涉及尚未覆盖的 tool_search_tool_result 等块类型(相关跟踪见 #41947 / #41958)。

参考来源

langchain-ai/langchain #41897

GamsGo AI

AI 工具推荐

想把多个 AI 模型放在一个入口?

GamsGo AI 集成 ChatGPT、DeepSeek、Gemini、Claude、Midjourney、Veo 等常用模型,适合写作、绘图、视频和日常 AI 工作流。

了解 GamsGo AI

推广链接:通过此链接购买,我可能获得佣金,不影响你的价格。

这个方案解决了吗?

celebrityanime
celebrityanime
文章: 36045

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注