langchain 1.0.0a12__py3-none-any.whl → 1.0.4__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.
- langchain/__init__.py +1 -1
- langchain/agents/__init__.py +7 -1
- langchain/agents/factory.py +722 -226
- langchain/agents/middleware/__init__.py +36 -9
- langchain/agents/middleware/_execution.py +388 -0
- langchain/agents/middleware/_redaction.py +350 -0
- langchain/agents/middleware/context_editing.py +46 -17
- langchain/agents/middleware/file_search.py +382 -0
- langchain/agents/middleware/human_in_the_loop.py +220 -173
- langchain/agents/middleware/model_call_limit.py +43 -10
- langchain/agents/middleware/model_fallback.py +79 -36
- langchain/agents/middleware/pii.py +68 -504
- langchain/agents/middleware/shell_tool.py +718 -0
- langchain/agents/middleware/summarization.py +2 -2
- langchain/agents/middleware/{planning.py → todo.py} +35 -16
- langchain/agents/middleware/tool_call_limit.py +308 -114
- langchain/agents/middleware/tool_emulator.py +200 -0
- langchain/agents/middleware/tool_retry.py +384 -0
- langchain/agents/middleware/tool_selection.py +25 -21
- langchain/agents/middleware/types.py +714 -257
- langchain/agents/structured_output.py +37 -27
- langchain/chat_models/__init__.py +7 -1
- langchain/chat_models/base.py +192 -190
- langchain/embeddings/__init__.py +13 -3
- langchain/embeddings/base.py +49 -29
- langchain/messages/__init__.py +50 -1
- langchain/tools/__init__.py +9 -7
- langchain/tools/tool_node.py +16 -1174
- langchain-1.0.4.dist-info/METADATA +92 -0
- langchain-1.0.4.dist-info/RECORD +34 -0
- langchain/_internal/__init__.py +0 -0
- langchain/_internal/_documents.py +0 -35
- langchain/_internal/_lazy_import.py +0 -35
- langchain/_internal/_prompts.py +0 -158
- langchain/_internal/_typing.py +0 -70
- langchain/_internal/_utils.py +0 -7
- langchain/agents/_internal/__init__.py +0 -1
- langchain/agents/_internal/_typing.py +0 -13
- langchain/agents/middleware/prompt_caching.py +0 -86
- langchain/documents/__init__.py +0 -7
- langchain/embeddings/cache.py +0 -361
- langchain/storage/__init__.py +0 -22
- langchain/storage/encoder_backed.py +0 -123
- langchain/storage/exceptions.py +0 -5
- langchain/storage/in_memory.py +0 -13
- langchain-1.0.0a12.dist-info/METADATA +0 -122
- langchain-1.0.0a12.dist-info/RECORD +0 -43
- {langchain-1.0.0a12.dist-info → langchain-1.0.4.dist-info}/WHEEL +0 -0
- {langchain-1.0.0a12.dist-info → langchain-1.0.4.dist-info}/licenses/LICENSE +0 -0
|
@@ -4,30 +4,34 @@ from __future__ import annotations
|
|
|
4
4
|
|
|
5
5
|
from typing import TYPE_CHECKING
|
|
6
6
|
|
|
7
|
-
from langchain.agents.middleware.types import
|
|
7
|
+
from langchain.agents.middleware.types import (
|
|
8
|
+
AgentMiddleware,
|
|
9
|
+
ModelCallResult,
|
|
10
|
+
ModelRequest,
|
|
11
|
+
ModelResponse,
|
|
12
|
+
)
|
|
8
13
|
from langchain.chat_models import init_chat_model
|
|
9
14
|
|
|
10
15
|
if TYPE_CHECKING:
|
|
16
|
+
from collections.abc import Awaitable, Callable
|
|
17
|
+
|
|
11
18
|
from langchain_core.language_models.chat_models import BaseChatModel
|
|
12
|
-
from langgraph.runtime import Runtime
|
|
13
19
|
|
|
14
20
|
|
|
15
21
|
class ModelFallbackMiddleware(AgentMiddleware):
|
|
16
|
-
"""
|
|
22
|
+
"""Automatic fallback to alternative models on errors.
|
|
17
23
|
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
list until either a call succeeds or all models have been exhausted.
|
|
24
|
+
Retries failed model calls with alternative models in sequence until
|
|
25
|
+
success or all models exhausted. Primary model specified in create_agent().
|
|
21
26
|
|
|
22
27
|
Example:
|
|
23
28
|
```python
|
|
24
29
|
from langchain.agents.middleware.model_fallback import ModelFallbackMiddleware
|
|
25
30
|
from langchain.agents import create_agent
|
|
26
31
|
|
|
27
|
-
# Create middleware with fallback models (not including primary)
|
|
28
32
|
fallback = ModelFallbackMiddleware(
|
|
29
|
-
"openai:gpt-4o-mini", #
|
|
30
|
-
"anthropic:claude-
|
|
33
|
+
"openai:gpt-4o-mini", # Try first on error
|
|
34
|
+
"anthropic:claude-sonnet-4-5-20250929", # Then this
|
|
31
35
|
)
|
|
32
36
|
|
|
33
37
|
agent = create_agent(
|
|
@@ -35,7 +39,7 @@ class ModelFallbackMiddleware(AgentMiddleware):
|
|
|
35
39
|
middleware=[fallback],
|
|
36
40
|
)
|
|
37
41
|
|
|
38
|
-
# If
|
|
42
|
+
# If primary fails: tries gpt-4o-mini, then claude-sonnet-4-5-20250929
|
|
39
43
|
result = await agent.invoke({"messages": [HumanMessage("Hello")]})
|
|
40
44
|
```
|
|
41
45
|
"""
|
|
@@ -45,13 +49,11 @@ class ModelFallbackMiddleware(AgentMiddleware):
|
|
|
45
49
|
first_model: str | BaseChatModel,
|
|
46
50
|
*additional_models: str | BaseChatModel,
|
|
47
51
|
) -> None:
|
|
48
|
-
"""Initialize
|
|
52
|
+
"""Initialize model fallback middleware.
|
|
49
53
|
|
|
50
54
|
Args:
|
|
51
|
-
first_model:
|
|
52
|
-
|
|
53
|
-
*additional_models: Additional fallback models to try, in order.
|
|
54
|
-
Can be model name strings or BaseChatModel instances.
|
|
55
|
+
first_model: First fallback model (string name or instance).
|
|
56
|
+
*additional_models: Additional fallbacks in order.
|
|
55
57
|
"""
|
|
56
58
|
super().__init__()
|
|
57
59
|
|
|
@@ -64,31 +66,72 @@ class ModelFallbackMiddleware(AgentMiddleware):
|
|
|
64
66
|
else:
|
|
65
67
|
self.models.append(model)
|
|
66
68
|
|
|
67
|
-
def
|
|
69
|
+
def wrap_model_call(
|
|
70
|
+
self,
|
|
71
|
+
request: ModelRequest,
|
|
72
|
+
handler: Callable[[ModelRequest], ModelResponse],
|
|
73
|
+
) -> ModelCallResult:
|
|
74
|
+
"""Try fallback models in sequence on errors.
|
|
75
|
+
|
|
76
|
+
Args:
|
|
77
|
+
request: Initial model request.
|
|
78
|
+
handler: Callback to execute the model.
|
|
79
|
+
|
|
80
|
+
Returns:
|
|
81
|
+
AIMessage from successful model call.
|
|
82
|
+
|
|
83
|
+
Raises:
|
|
84
|
+
Exception: If all models fail, re-raises last exception.
|
|
85
|
+
"""
|
|
86
|
+
# Try primary model first
|
|
87
|
+
last_exception: Exception
|
|
88
|
+
try:
|
|
89
|
+
return handler(request)
|
|
90
|
+
except Exception as e: # noqa: BLE001
|
|
91
|
+
last_exception = e
|
|
92
|
+
|
|
93
|
+
# Try fallback models
|
|
94
|
+
for fallback_model in self.models:
|
|
95
|
+
request.model = fallback_model
|
|
96
|
+
try:
|
|
97
|
+
return handler(request)
|
|
98
|
+
except Exception as e: # noqa: BLE001
|
|
99
|
+
last_exception = e
|
|
100
|
+
continue
|
|
101
|
+
|
|
102
|
+
raise last_exception
|
|
103
|
+
|
|
104
|
+
async def awrap_model_call(
|
|
68
105
|
self,
|
|
69
|
-
error: Exception, # noqa: ARG002
|
|
70
106
|
request: ModelRequest,
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
) -> ModelRequest | None:
|
|
75
|
-
"""Retry with the next fallback model.
|
|
107
|
+
handler: Callable[[ModelRequest], Awaitable[ModelResponse]],
|
|
108
|
+
) -> ModelCallResult:
|
|
109
|
+
"""Try fallback models in sequence on errors (async version).
|
|
76
110
|
|
|
77
111
|
Args:
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
state: The current agent state.
|
|
81
|
-
runtime: The langgraph runtime.
|
|
82
|
-
attempt: The current attempt number (1-indexed).
|
|
112
|
+
request: Initial model request.
|
|
113
|
+
handler: Async callback to execute the model.
|
|
83
114
|
|
|
84
115
|
Returns:
|
|
85
|
-
|
|
116
|
+
AIMessage from successful model call.
|
|
117
|
+
|
|
118
|
+
Raises:
|
|
119
|
+
Exception: If all models fail, re-raises last exception.
|
|
86
120
|
"""
|
|
87
|
-
#
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
121
|
+
# Try primary model first
|
|
122
|
+
last_exception: Exception
|
|
123
|
+
try:
|
|
124
|
+
return await handler(request)
|
|
125
|
+
except Exception as e: # noqa: BLE001
|
|
126
|
+
last_exception = e
|
|
127
|
+
|
|
128
|
+
# Try fallback models
|
|
129
|
+
for fallback_model in self.models:
|
|
130
|
+
request.model = fallback_model
|
|
131
|
+
try:
|
|
132
|
+
return await handler(request)
|
|
133
|
+
except Exception as e: # noqa: BLE001
|
|
134
|
+
last_exception = e
|
|
135
|
+
continue
|
|
136
|
+
|
|
137
|
+
raise last_exception
|