
TypeError: ShellToolMiddleware.__init__() got an unexpected keyword argument ‘tool_name’
快速结论:此错误发生在 langchain-anthropic 1.1.0 构建测试环境中(如 Nixpkgs、Pytest),原因是 ClaudeBashToolMiddleware.__init__ 向 ShellToolMiddleware 父类传入了 tool_name 参数,而该版本的父类 __init__ 方法不接受此参数。优先排查方法:升级到 langchain-anthropic 1.2.0 或更高版本。
问题场景
在构建 langchain-anthropic 1.1.0 时,运行 pytest 执行单元测试 test_creates_bash_tool 时触发。Nixpkgs 构建系统比普通 Python 构建系统更严格,因此该错误在 Nixpkgs 环境中更容易暴露。
报错原文
____________________________ test_creates_bash_tool ____________________________
monkeypatch = <_pytest.monkeypatch.MonkeyPatch object at 0x7ffff2b13bb0>
def test_creates_bash_tool(monkeypatch: pytest.MonkeyPatch) -> None:
"""Test that ClaudeBashToolMiddleware creates a tool named 'bash'."""
> middleware = ClaudeBashToolMiddleware()
^^^^^^^^^^^^^^^^^^^^^^^^^^
tests/unit_tests/middleware/test_bash.py:16:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
self = <langchain_anthropic.middleware.bash.ClaudeBashToolMiddleware object at 0x7ffff29a5be0>
workspace_root = None
def __init__(
self,
workspace_root: str | None = None,
*,
startup_commands: tuple[str, ...] | list[str] | str | None = None,
shutdown_commands: tuple[str, ...] | list[str] | str | None = None,
execution_policy: Any | None = None,
redaction_rules: tuple[Any, ...] | list[Any] | None = None,
tool_description: str | None = None,
env: dict[str, Any] | None = None,
) -> None:
"""Initialize middleware for Claude's native bash tool.
Args:
workspace_root: Base directory for the shell session.
If omitted, a temporary directory is created.
startup_commands: Optional commands executed after the session starts.
shutdown_commands: Optional commands executed before session shutdown.
execution_policy: Execution policy controlling timeouts and limits.
redaction_rules: Optional redaction rules to sanitize output.
tool_description: Optional override for tool description.
env: Optional environment variables for the shell session.
"""
> super().__init__(
workspace_root=workspace_root,
startup_commands=startup_commands,
shutdown_commands=shutdown_commands,
execution_policy=execution_policy,
redaction_rules=redaction_rules,
tool_description=tool_description,
tool_name=BASH_TOOL_NAME,
shell_command=("/bin/bash",),
env=env,
)
E TypeError: ShellToolMiddleware.__init__() got an unexpected keyword argument 'tool_name'
langchain_anthropic/middleware/bash.py:45: TypeError
原因分析
在 langchain-anthropic 1.1.0 中,ClaudeBashToolMiddleware.__init__ 方法(位于 langchain_anthropic/middleware/bash.py 第 45 行)在调用父类 ShellToolMiddleware.__init__ 时,传递了 tool_name=BASH_TOOL_NAME 参数。但 ShellToolMiddleware.__init__ 在该版本中不接受 tool_name 参数,导致 TypeError。这是一个跨版本兼容性 bug,在更严格的构建系统(如 Nixpkgs)中会暴露。
环境排查
langchain-anthropic版本:1.1.0(确认)- 运行环境:Nixpkgs 构建系统 或运行
pytest的 Python 项目 langchain核心包版本(可能影响ShellToolMiddleware的签名)- 升级至 1.2.0 或更高版本后问题是否消失
解决步骤
- 首选方案:升级包版本。将
langchain-anthropic升级到 1.2.0 或更高版本。Issue 确认在 1.3.0 下测试通过。 - 如果无法升级,可以暂时跳过此测试(如在 Nixpkgs 中禁用),但官方建议升级。
验证方法
升级后,重新运行 pytest tests/unit_tests/middleware/test_bash.py。确认 test_creates_bash_tool 和 test_replaces_tool_with_claude_descriptor 测试均通过,不再抛出 TypeError。也可简单运行 make test 确认整体测试套件正常。



