langchain 1.0.0a15__py3-none-any.whl → 1.0.0rc2__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of langchain might be problematic. Click here for more details.
- langchain/__init__.py +1 -1
- langchain/agents/__init__.py +7 -1
- langchain/agents/factory.py +57 -32
- langchain/agents/middleware/__init__.py +9 -1
- langchain/agents/middleware/context_editing.py +1 -1
- langchain/agents/middleware/human_in_the_loop.py +11 -7
- langchain/agents/middleware/model_call_limit.py +4 -5
- langchain/agents/middleware/model_fallback.py +0 -2
- langchain/agents/middleware/pii.py +7 -7
- langchain/agents/middleware/tool_call_limit.py +1 -1
- langchain/agents/middleware/tool_emulator.py +2 -2
- langchain/agents/middleware/tool_retry.py +384 -0
- langchain/agents/middleware/types.py +67 -58
- langchain/agents/structured_output.py +29 -25
- langchain/chat_models/__init__.py +7 -1
- langchain/chat_models/base.py +98 -108
- langchain/embeddings/__init__.py +7 -1
- langchain/embeddings/base.py +1 -1
- langchain/messages/__init__.py +10 -1
- langchain/tools/__init__.py +9 -2
- langchain/tools/tool_node.py +257 -94
- {langchain-1.0.0a15.dist-info → langchain-1.0.0rc2.dist-info}/METADATA +2 -2
- langchain-1.0.0rc2.dist-info/RECORD +30 -0
- langchain-1.0.0a15.dist-info/RECORD +0 -29
- {langchain-1.0.0a15.dist-info → langchain-1.0.0rc2.dist-info}/WHEEL +0 -0
- {langchain-1.0.0a15.dist-info → langchain-1.0.0rc2.dist-info}/licenses/LICENSE +0 -0
|
@@ -0,0 +1,384 @@
|
|
|
1
|
+
"""Tool retry middleware for agents."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import asyncio
|
|
6
|
+
import random
|
|
7
|
+
import time
|
|
8
|
+
from typing import TYPE_CHECKING, Literal
|
|
9
|
+
|
|
10
|
+
from langchain_core.messages import ToolMessage
|
|
11
|
+
|
|
12
|
+
from langchain.agents.middleware.types import AgentMiddleware
|
|
13
|
+
|
|
14
|
+
if TYPE_CHECKING:
|
|
15
|
+
from collections.abc import Awaitable, Callable
|
|
16
|
+
|
|
17
|
+
from langgraph.types import Command
|
|
18
|
+
|
|
19
|
+
from langchain.tools import BaseTool
|
|
20
|
+
from langchain.tools.tool_node import ToolCallRequest
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
class ToolRetryMiddleware(AgentMiddleware):
|
|
24
|
+
"""Middleware that automatically retries failed tool calls with configurable backoff.
|
|
25
|
+
|
|
26
|
+
Supports retrying on specific exceptions and exponential backoff.
|
|
27
|
+
|
|
28
|
+
Examples:
|
|
29
|
+
Basic usage with default settings (2 retries, exponential backoff):
|
|
30
|
+
```python
|
|
31
|
+
from langchain.agents import create_agent
|
|
32
|
+
from langchain.agents.middleware import ToolRetryMiddleware
|
|
33
|
+
|
|
34
|
+
agent = create_agent(model, tools=[search_tool], middleware=[ToolRetryMiddleware()])
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
Retry specific exceptions only:
|
|
38
|
+
```python
|
|
39
|
+
from requests.exceptions import RequestException, Timeout
|
|
40
|
+
|
|
41
|
+
retry = ToolRetryMiddleware(
|
|
42
|
+
max_retries=4,
|
|
43
|
+
retry_on=(RequestException, Timeout),
|
|
44
|
+
backoff_factor=1.5,
|
|
45
|
+
)
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
Custom exception filtering:
|
|
49
|
+
```python
|
|
50
|
+
from requests.exceptions import HTTPError
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
def should_retry(exc: Exception) -> bool:
|
|
54
|
+
# Only retry on 5xx errors
|
|
55
|
+
if isinstance(exc, HTTPError):
|
|
56
|
+
return 500 <= exc.status_code < 600
|
|
57
|
+
return False
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
retry = ToolRetryMiddleware(
|
|
61
|
+
max_retries=3,
|
|
62
|
+
retry_on=should_retry,
|
|
63
|
+
)
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
Apply to specific tools with custom error handling:
|
|
67
|
+
```python
|
|
68
|
+
def format_error(exc: Exception) -> str:
|
|
69
|
+
return "Database temporarily unavailable. Please try again later."
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
retry = ToolRetryMiddleware(
|
|
73
|
+
max_retries=4,
|
|
74
|
+
tools=["search_database"],
|
|
75
|
+
on_failure=format_error,
|
|
76
|
+
)
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
Apply to specific tools using BaseTool instances:
|
|
80
|
+
```python
|
|
81
|
+
from langchain_core.tools import tool
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
@tool
|
|
85
|
+
def search_database(query: str) -> str:
|
|
86
|
+
'''Search the database.'''
|
|
87
|
+
return results
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
retry = ToolRetryMiddleware(
|
|
91
|
+
max_retries=4,
|
|
92
|
+
tools=[search_database], # Pass BaseTool instance
|
|
93
|
+
)
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
Constant backoff (no exponential growth):
|
|
97
|
+
```python
|
|
98
|
+
retry = ToolRetryMiddleware(
|
|
99
|
+
max_retries=5,
|
|
100
|
+
backoff_factor=0.0, # No exponential growth
|
|
101
|
+
initial_delay=2.0, # Always wait 2 seconds
|
|
102
|
+
)
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
Raise exception on failure:
|
|
106
|
+
```python
|
|
107
|
+
retry = ToolRetryMiddleware(
|
|
108
|
+
max_retries=2,
|
|
109
|
+
on_failure="raise", # Re-raise exception instead of returning message
|
|
110
|
+
)
|
|
111
|
+
```
|
|
112
|
+
"""
|
|
113
|
+
|
|
114
|
+
def __init__(
|
|
115
|
+
self,
|
|
116
|
+
*,
|
|
117
|
+
max_retries: int = 2,
|
|
118
|
+
tools: list[BaseTool | str] | None = None,
|
|
119
|
+
retry_on: tuple[type[Exception], ...] | Callable[[Exception], bool] = (Exception,),
|
|
120
|
+
on_failure: (
|
|
121
|
+
Literal["raise", "return_message"] | Callable[[Exception], str]
|
|
122
|
+
) = "return_message",
|
|
123
|
+
backoff_factor: float = 2.0,
|
|
124
|
+
initial_delay: float = 1.0,
|
|
125
|
+
max_delay: float = 60.0,
|
|
126
|
+
jitter: bool = True,
|
|
127
|
+
) -> None:
|
|
128
|
+
"""Initialize ToolRetryMiddleware.
|
|
129
|
+
|
|
130
|
+
Args:
|
|
131
|
+
max_retries: Maximum number of retry attempts after the initial call.
|
|
132
|
+
Default is 2 retries (3 total attempts). Must be >= 0.
|
|
133
|
+
tools: Optional list of tools or tool names to apply retry logic to.
|
|
134
|
+
Can be a list of `BaseTool` instances or tool name strings.
|
|
135
|
+
If `None`, applies to all tools. Default is `None`.
|
|
136
|
+
retry_on: Either a tuple of exception types to retry on, or a callable
|
|
137
|
+
that takes an exception and returns `True` if it should be retried.
|
|
138
|
+
Default is to retry on all exceptions.
|
|
139
|
+
on_failure: Behavior when all retries are exhausted. Options:
|
|
140
|
+
- `"return_message"` (default): Return a ToolMessage with error details,
|
|
141
|
+
allowing the LLM to handle the failure and potentially recover.
|
|
142
|
+
- `"raise"`: Re-raise the exception, stopping agent execution.
|
|
143
|
+
- Custom callable: Function that takes the exception and returns a string
|
|
144
|
+
for the ToolMessage content, allowing custom error formatting.
|
|
145
|
+
backoff_factor: Multiplier for exponential backoff. Each retry waits
|
|
146
|
+
`initial_delay * (backoff_factor ** retry_number)` seconds.
|
|
147
|
+
Set to 0.0 for constant delay. Default is 2.0.
|
|
148
|
+
initial_delay: Initial delay in seconds before first retry. Default is 1.0.
|
|
149
|
+
max_delay: Maximum delay in seconds between retries. Caps exponential
|
|
150
|
+
backoff growth. Default is 60.0.
|
|
151
|
+
jitter: Whether to add random jitter (±25%) to delay to avoid thundering herd.
|
|
152
|
+
Default is `True`.
|
|
153
|
+
|
|
154
|
+
Raises:
|
|
155
|
+
ValueError: If max_retries < 0 or delays are negative.
|
|
156
|
+
"""
|
|
157
|
+
super().__init__()
|
|
158
|
+
|
|
159
|
+
# Validate parameters
|
|
160
|
+
if max_retries < 0:
|
|
161
|
+
msg = "max_retries must be >= 0"
|
|
162
|
+
raise ValueError(msg)
|
|
163
|
+
if initial_delay < 0:
|
|
164
|
+
msg = "initial_delay must be >= 0"
|
|
165
|
+
raise ValueError(msg)
|
|
166
|
+
if max_delay < 0:
|
|
167
|
+
msg = "max_delay must be >= 0"
|
|
168
|
+
raise ValueError(msg)
|
|
169
|
+
if backoff_factor < 0:
|
|
170
|
+
msg = "backoff_factor must be >= 0"
|
|
171
|
+
raise ValueError(msg)
|
|
172
|
+
|
|
173
|
+
self.max_retries = max_retries
|
|
174
|
+
|
|
175
|
+
# Extract tool names from BaseTool instances or strings
|
|
176
|
+
self._tool_filter: list[str] | None
|
|
177
|
+
if tools is not None:
|
|
178
|
+
self._tool_filter = [tool.name if not isinstance(tool, str) else tool for tool in tools]
|
|
179
|
+
else:
|
|
180
|
+
self._tool_filter = None
|
|
181
|
+
|
|
182
|
+
self.tools = [] # No additional tools registered by this middleware
|
|
183
|
+
self.retry_on = retry_on
|
|
184
|
+
self.on_failure = on_failure
|
|
185
|
+
self.backoff_factor = backoff_factor
|
|
186
|
+
self.initial_delay = initial_delay
|
|
187
|
+
self.max_delay = max_delay
|
|
188
|
+
self.jitter = jitter
|
|
189
|
+
|
|
190
|
+
def _should_retry_tool(self, tool_name: str) -> bool:
|
|
191
|
+
"""Check if retry logic should apply to this tool.
|
|
192
|
+
|
|
193
|
+
Args:
|
|
194
|
+
tool_name: Name of the tool being called.
|
|
195
|
+
|
|
196
|
+
Returns:
|
|
197
|
+
`True` if retry logic should apply, `False` otherwise.
|
|
198
|
+
"""
|
|
199
|
+
if self._tool_filter is None:
|
|
200
|
+
return True
|
|
201
|
+
return tool_name in self._tool_filter
|
|
202
|
+
|
|
203
|
+
def _should_retry_exception(self, exc: Exception) -> bool:
|
|
204
|
+
"""Check if the exception should trigger a retry.
|
|
205
|
+
|
|
206
|
+
Args:
|
|
207
|
+
exc: The exception that occurred.
|
|
208
|
+
|
|
209
|
+
Returns:
|
|
210
|
+
`True` if the exception should be retried, `False` otherwise.
|
|
211
|
+
"""
|
|
212
|
+
if callable(self.retry_on):
|
|
213
|
+
return self.retry_on(exc)
|
|
214
|
+
return isinstance(exc, self.retry_on)
|
|
215
|
+
|
|
216
|
+
def _calculate_delay(self, retry_number: int) -> float:
|
|
217
|
+
"""Calculate delay for the given retry attempt.
|
|
218
|
+
|
|
219
|
+
Args:
|
|
220
|
+
retry_number: The retry attempt number (0-indexed).
|
|
221
|
+
|
|
222
|
+
Returns:
|
|
223
|
+
Delay in seconds before next retry.
|
|
224
|
+
"""
|
|
225
|
+
if self.backoff_factor == 0.0:
|
|
226
|
+
delay = self.initial_delay
|
|
227
|
+
else:
|
|
228
|
+
delay = self.initial_delay * (self.backoff_factor**retry_number)
|
|
229
|
+
|
|
230
|
+
# Cap at max_delay
|
|
231
|
+
delay = min(delay, self.max_delay)
|
|
232
|
+
|
|
233
|
+
if self.jitter and delay > 0:
|
|
234
|
+
jitter_amount = delay * 0.25
|
|
235
|
+
delay = delay + random.uniform(-jitter_amount, jitter_amount) # noqa: S311
|
|
236
|
+
# Ensure delay is not negative after jitter
|
|
237
|
+
delay = max(0, delay)
|
|
238
|
+
|
|
239
|
+
return delay
|
|
240
|
+
|
|
241
|
+
def _format_failure_message(self, tool_name: str, exc: Exception, attempts_made: int) -> str:
|
|
242
|
+
"""Format the failure message when retries are exhausted.
|
|
243
|
+
|
|
244
|
+
Args:
|
|
245
|
+
tool_name: Name of the tool that failed.
|
|
246
|
+
exc: The exception that caused the failure.
|
|
247
|
+
attempts_made: Number of attempts actually made.
|
|
248
|
+
|
|
249
|
+
Returns:
|
|
250
|
+
Formatted error message string.
|
|
251
|
+
"""
|
|
252
|
+
exc_type = type(exc).__name__
|
|
253
|
+
attempt_word = "attempt" if attempts_made == 1 else "attempts"
|
|
254
|
+
return f"Tool '{tool_name}' failed after {attempts_made} {attempt_word} with {exc_type}"
|
|
255
|
+
|
|
256
|
+
def _handle_failure(
|
|
257
|
+
self, tool_name: str, tool_call_id: str | None, exc: Exception, attempts_made: int
|
|
258
|
+
) -> ToolMessage:
|
|
259
|
+
"""Handle failure when all retries are exhausted.
|
|
260
|
+
|
|
261
|
+
Args:
|
|
262
|
+
tool_name: Name of the tool that failed.
|
|
263
|
+
tool_call_id: ID of the tool call (may be None).
|
|
264
|
+
exc: The exception that caused the failure.
|
|
265
|
+
attempts_made: Number of attempts actually made.
|
|
266
|
+
|
|
267
|
+
Returns:
|
|
268
|
+
ToolMessage with error details.
|
|
269
|
+
|
|
270
|
+
Raises:
|
|
271
|
+
Exception: If on_failure is "raise", re-raises the exception.
|
|
272
|
+
"""
|
|
273
|
+
if self.on_failure == "raise":
|
|
274
|
+
raise exc
|
|
275
|
+
|
|
276
|
+
if callable(self.on_failure):
|
|
277
|
+
content = self.on_failure(exc)
|
|
278
|
+
else:
|
|
279
|
+
content = self._format_failure_message(tool_name, exc, attempts_made)
|
|
280
|
+
|
|
281
|
+
return ToolMessage(
|
|
282
|
+
content=content,
|
|
283
|
+
tool_call_id=tool_call_id,
|
|
284
|
+
name=tool_name,
|
|
285
|
+
status="error",
|
|
286
|
+
)
|
|
287
|
+
|
|
288
|
+
def wrap_tool_call(
|
|
289
|
+
self,
|
|
290
|
+
request: ToolCallRequest,
|
|
291
|
+
handler: Callable[[ToolCallRequest], ToolMessage | Command],
|
|
292
|
+
) -> ToolMessage | Command:
|
|
293
|
+
"""Intercept tool execution and retry on failure.
|
|
294
|
+
|
|
295
|
+
Args:
|
|
296
|
+
request: Tool call request with call dict, BaseTool, state, and runtime.
|
|
297
|
+
handler: Callable to execute the tool (can be called multiple times).
|
|
298
|
+
|
|
299
|
+
Returns:
|
|
300
|
+
ToolMessage or Command (the final result).
|
|
301
|
+
"""
|
|
302
|
+
tool_name = request.tool.name if request.tool else request.tool_call["name"]
|
|
303
|
+
|
|
304
|
+
# Check if retry should apply to this tool
|
|
305
|
+
if not self._should_retry_tool(tool_name):
|
|
306
|
+
return handler(request)
|
|
307
|
+
|
|
308
|
+
tool_call_id = request.tool_call["id"]
|
|
309
|
+
|
|
310
|
+
# Initial attempt + retries
|
|
311
|
+
for attempt in range(self.max_retries + 1):
|
|
312
|
+
try:
|
|
313
|
+
return handler(request)
|
|
314
|
+
except Exception as exc: # noqa: BLE001
|
|
315
|
+
attempts_made = attempt + 1 # attempt is 0-indexed
|
|
316
|
+
|
|
317
|
+
# Check if we should retry this exception
|
|
318
|
+
if not self._should_retry_exception(exc):
|
|
319
|
+
# Exception is not retryable, handle failure immediately
|
|
320
|
+
return self._handle_failure(tool_name, tool_call_id, exc, attempts_made)
|
|
321
|
+
|
|
322
|
+
# Check if we have more retries left
|
|
323
|
+
if attempt < self.max_retries:
|
|
324
|
+
# Calculate and apply backoff delay
|
|
325
|
+
delay = self._calculate_delay(attempt)
|
|
326
|
+
if delay > 0:
|
|
327
|
+
time.sleep(delay)
|
|
328
|
+
# Continue to next retry
|
|
329
|
+
else:
|
|
330
|
+
# No more retries, handle failure
|
|
331
|
+
return self._handle_failure(tool_name, tool_call_id, exc, attempts_made)
|
|
332
|
+
|
|
333
|
+
# Unreachable: loop always returns via handler success or _handle_failure
|
|
334
|
+
msg = "Unexpected: retry loop completed without returning"
|
|
335
|
+
raise RuntimeError(msg)
|
|
336
|
+
|
|
337
|
+
async def awrap_tool_call(
|
|
338
|
+
self,
|
|
339
|
+
request: ToolCallRequest,
|
|
340
|
+
handler: Callable[[ToolCallRequest], Awaitable[ToolMessage | Command]],
|
|
341
|
+
) -> ToolMessage | Command:
|
|
342
|
+
"""Intercept and control async tool execution with retry logic.
|
|
343
|
+
|
|
344
|
+
Args:
|
|
345
|
+
request: Tool call request with call dict, BaseTool, state, and runtime.
|
|
346
|
+
handler: Async callable to execute the tool and returns ToolMessage or Command.
|
|
347
|
+
|
|
348
|
+
Returns:
|
|
349
|
+
ToolMessage or Command (the final result).
|
|
350
|
+
"""
|
|
351
|
+
tool_name = request.tool.name if request.tool else request.tool_call["name"]
|
|
352
|
+
|
|
353
|
+
# Check if retry should apply to this tool
|
|
354
|
+
if not self._should_retry_tool(tool_name):
|
|
355
|
+
return await handler(request)
|
|
356
|
+
|
|
357
|
+
tool_call_id = request.tool_call["id"]
|
|
358
|
+
|
|
359
|
+
# Initial attempt + retries
|
|
360
|
+
for attempt in range(self.max_retries + 1):
|
|
361
|
+
try:
|
|
362
|
+
return await handler(request)
|
|
363
|
+
except Exception as exc: # noqa: BLE001
|
|
364
|
+
attempts_made = attempt + 1 # attempt is 0-indexed
|
|
365
|
+
|
|
366
|
+
# Check if we should retry this exception
|
|
367
|
+
if not self._should_retry_exception(exc):
|
|
368
|
+
# Exception is not retryable, handle failure immediately
|
|
369
|
+
return self._handle_failure(tool_name, tool_call_id, exc, attempts_made)
|
|
370
|
+
|
|
371
|
+
# Check if we have more retries left
|
|
372
|
+
if attempt < self.max_retries:
|
|
373
|
+
# Calculate and apply backoff delay
|
|
374
|
+
delay = self._calculate_delay(attempt)
|
|
375
|
+
if delay > 0:
|
|
376
|
+
await asyncio.sleep(delay)
|
|
377
|
+
# Continue to next retry
|
|
378
|
+
else:
|
|
379
|
+
# No more retries, handle failure
|
|
380
|
+
return self._handle_failure(tool_name, tool_call_id, exc, attempts_made)
|
|
381
|
+
|
|
382
|
+
# Unreachable: loop always returns via handler success or _handle_failure
|
|
383
|
+
msg = "Unexpected: retry loop completed without returning"
|
|
384
|
+
raise RuntimeError(msg)
|