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

ToolRetryMiddleware Defaults Retry Non-Idempotent Tools On Ambiguous Transport Errors, Causing Duplicate Irreversible Effects

With default settings, ToolRetryMiddleware retries all tools on ambiguous transport errors (e.g., httpx.ReadTimeout) that may have already committed external effects, leading to duplicate irreversible side effects. After exhausting retries, it instructs the model to 'Please try again,' compounding the risk.

criticalConfidence 95%LangchainAffected V1.4.2

Origin Analysis

The default_retry_on policy returns True for every exception except ModelError, not distinguishing ambiguous transport failures (which may have side effects) from determinate no-effect failures. Combined with tools=None applying to all tools and on_failure='continue' prompting another attempt, non-idempotent tools are retried without safeguards.
Define a tool that appends to a global list (simulating an irreversible external effect) and then raises httpx.ReadTimeout. Create an agent with ToolRetryMiddleware() using all defaults and a scripted model that calls the tool once. Invoke the agent; observe that the tool executes multiple times (e.g., 3 executions) and the model receives an error message ending with 'Please try again.'

Fixing Code Block

Edge Case Audit

This change reduces automatic retries for ambiguous transport errors. Tools that are truly idempotent and previously relied on these retries will now fail earlier; users should explicitly set retry_on to include such exceptions or list those tools. Existing subclasses that override default_retry_on or _format_failure_message may need adjustments. The helper uses class name checks, which may miss custom ambiguous exceptions; extend the registry for comprehensive coverage. In concurrent or multi-threaded scenarios, even with this fix, tools without idempotency keys may still experience duplicates if retried manually; use idempotency keys at the tool level. Rolling back: revert the patch to restore previous behavior.

Ecosystem Topology