feat(anthropic): add AnthropicToolIdSanitizationMiddleware#37096
Open
Mason Daugherty (mdrxy) wants to merge 1 commit into
Open
feat(anthropic): add AnthropicToolIdSanitizationMiddleware#37096Mason Daugherty (mdrxy) wants to merge 1 commit into
AnthropicToolIdSanitizationMiddleware#37096Mason Daugherty (mdrxy) wants to merge 1 commit into
Conversation
| return candidate | ||
|
|
||
|
|
||
| class AnthropicToolIdSanitizationMiddleware(AgentMiddleware): |
Collaborator
There was a problem hiding this comment.
Any chance we want a more general version of this middleware in LangChain?
Something like StandardizationMiddleware and it'll handle some logic for standardizing things across providers?
OK if we think we want to keep it specific to Anthropic
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Add
AnthropicToolIdSanitizationMiddlewareso cross-provider conversation histories replay cleanly against Anthropic. Anthropic enforcestool_use.idmatching^[a-zA-Z0-9_-]+$, but providers like Kimi-K2 emit IDs of the formfunctions.<name>:<idx>— replaying such a thread raises a 400. The middleware rewrites only the outgoingModelRequest, leaving graph state and persisted checkpoints untouched, so it composes cleanly with HITL resume.Changes
bad_id -> safe_idmap per request: substitute illegal characters with_, then fall back to a truncated sha256 suffix (with a counter for further collisions) when the base would shadow an already-valid id or another rewritten id (_make_safe_id).AIMessage.tool_calls[*].id,AIMessage.contentblocks (tool_use,server_tool_use,mcp_tool_use),ToolMessage.tool_call_id, and*_tool_resultcontent blocks (tool_result,web_search_tool_result,code_execution_tool_result,bash_tool_result,mcp_tool_result). Suffix matching on*_tool_use/*_tool_resultkeeps future block kinds covered.tool_calls[i]and the i-th clienttool_useblock onto one canonical id, then propagate that as an alias to the immediately-followingToolMessageresults so cross-message linkage survives even when the input list disagreed._should_run: non-ChatAnthropicmodels follow theunsupported_model_behaviorknob —'ignore'(default; logs a debug breadcrumb so the bypass is observable),'warn', or'raise'. The constructor runtime-validates the literal so typos raiseValueErrorinstead of silently degrading to ignore semantics. Thelangchainimport is guarded with a narrowedModuleNotFoundErrorso genuine import-time failures insidelangchain.agents.middlewarearen't masked by a misleading "install langchain" message.wrap_model_calland asyncawrap_model_callshare the same sanitization path.