langchain
8734c05f - feat(langchain_v1): tool retry middleware (#33503)

Commit
198 days ago
feat(langchain_v1): tool retry middleware (#33503) Adds `ToolRetryMiddleware` to automatically retry failed tool calls with configurable exponential backoff, exception filtering, and error handling. ## Example ```python from langchain.agents import create_agent from langchain.agents.middleware import ToolRetryMiddleware from langchain_openai import ChatOpenAI # Retry up to 3 times with exponential backoff retry = ToolRetryMiddleware( max_retries=3, initial_delay=1.0, backoff_factor=2.0, ) agent = create_agent( model=ChatOpenAI(model="gpt-4"), tools=[search_tool, database_tool], middleware=[retry], ) # Tool failures are automatically retried result = agent.invoke({"messages": [{"role": "user", "content": "Search for AI news"}]}) ``` For advanced usage with specific exception handling: ```python from requests.exceptions import Timeout, HTTPError def should_retry(exc: Exception) -> bool: # Only retry on 5xx errors or timeouts if isinstance(exc, HTTPError): return 500 <= exc.response.status_code < 600 return isinstance(exc, Timeout) retry = ToolRetryMiddleware( max_retries=4, retry_on=should_retry, tools=["search_database"], # Only apply to specific tools ) ```
Author
Parents
Loading