AI & Agent Dev Bug Sandbox logo
AI & Agent Dev Bug Sandbox
Back to Radar

KeyError On Rename In Langchain-Anthropic Middleware Due To Missing 'Old_path' In Args

In langchain-anthropic middleware, the file_tool callback constructs an args dictionary for tool calls but omits the 'old_path' key when handling the rename command. The _handle_rename method expects 'old_path', causing a KeyError on every rename operation across all four middleware subclasses (state/filesystem text editor and memory).

highConfidence 95%LangChain

Origin Analysis

The producer functions in _StateClaudeFileToolMiddleware and _FilesystemClaudeFileToolMiddleware build an args dict with keys like 'path', 'new_path', 'file_text', etc., but do not include 'old_path' for the rename command. The consumer _handle_rename accesses args['old_path'] unconditionally, leading to a KeyError.
1. Instantiate any of StateClaudeTextEditorMiddleware, StateClaudeMemoryMiddleware, FilesystemClaudeTextEditorMiddleware, or FilesystemClaudeMemoryMiddleware. 2. Prepare a valid state or filesystem with an existing file. 3. Invoke the rename operation through the middleware (e.g., call _handle_rename directly with a path and new_path, or simulate a tool call with command='rename'). 4. The call raises KeyError: 'old_path'.

Fixing Code Block

Edge Case Audit

This fix addresses the immediate crash, but future key drift between producers and consumers may still occur. It is recommended to also add KeyError to the except block's exception whitelist to catch similar mismatches gracefully. Rollback: if this change causes unexpected behavior (e.g., incorrect old_path when path is not the old path), revert to original code. Test rename operations on both state and filesystem backends after applying.

Ecosystem Topology