langchain 1.0.0a9__py3-none-any.whl → 1.0.0a11__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 -24
- langchain/_internal/_documents.py +1 -1
- langchain/_internal/_prompts.py +2 -2
- langchain/_internal/_typing.py +1 -1
- langchain/agents/__init__.py +2 -3
- langchain/agents/factory.py +1126 -0
- langchain/agents/middleware/__init__.py +38 -1
- langchain/agents/middleware/context_editing.py +245 -0
- langchain/agents/middleware/human_in_the_loop.py +67 -20
- langchain/agents/middleware/model_call_limit.py +177 -0
- langchain/agents/middleware/model_fallback.py +94 -0
- langchain/agents/middleware/pii.py +753 -0
- langchain/agents/middleware/planning.py +201 -0
- langchain/agents/middleware/prompt_caching.py +7 -4
- langchain/agents/middleware/summarization.py +2 -1
- langchain/agents/middleware/tool_call_limit.py +260 -0
- langchain/agents/middleware/tool_selection.py +306 -0
- langchain/agents/middleware/types.py +708 -127
- langchain/agents/structured_output.py +15 -1
- langchain/chat_models/base.py +22 -25
- langchain/embeddings/base.py +3 -4
- langchain/embeddings/cache.py +0 -1
- langchain/messages/__init__.py +29 -0
- langchain/rate_limiters/__init__.py +13 -0
- langchain/tools/__init__.py +9 -0
- langchain/{agents → tools}/tool_node.py +8 -10
- {langchain-1.0.0a9.dist-info → langchain-1.0.0a11.dist-info}/METADATA +29 -35
- langchain-1.0.0a11.dist-info/RECORD +43 -0
- {langchain-1.0.0a9.dist-info → langchain-1.0.0a11.dist-info}/WHEEL +1 -1
- langchain/agents/middleware_agent.py +0 -617
- langchain/agents/react_agent.py +0 -1228
- langchain/globals.py +0 -18
- langchain/text_splitter.py +0 -50
- langchain-1.0.0a9.dist-info/RECORD +0 -38
- langchain-1.0.0a9.dist-info/entry_points.txt +0 -4
- {langchain-1.0.0a9.dist-info → langchain-1.0.0a11.dist-info}/licenses/LICENSE +0 -0
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
"""Model fallback middleware for agents."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from typing import TYPE_CHECKING
|
|
6
|
+
|
|
7
|
+
from langchain.agents.middleware.types import AgentMiddleware, AgentState, ModelRequest
|
|
8
|
+
from langchain.chat_models import init_chat_model
|
|
9
|
+
|
|
10
|
+
if TYPE_CHECKING:
|
|
11
|
+
from langchain_core.language_models.chat_models import BaseChatModel
|
|
12
|
+
from langgraph.runtime import Runtime
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
class ModelFallbackMiddleware(AgentMiddleware):
|
|
16
|
+
"""Middleware that provides automatic model fallback on errors.
|
|
17
|
+
|
|
18
|
+
This middleware attempts to retry failed model calls with alternative models
|
|
19
|
+
in sequence. When a model call fails, it tries the next model in the fallback
|
|
20
|
+
list until either a call succeeds or all models have been exhausted.
|
|
21
|
+
|
|
22
|
+
Example:
|
|
23
|
+
```python
|
|
24
|
+
from langchain.agents.middleware.model_fallback import ModelFallbackMiddleware
|
|
25
|
+
from langchain.agents import create_agent
|
|
26
|
+
|
|
27
|
+
# Create middleware with fallback models (not including primary)
|
|
28
|
+
fallback = ModelFallbackMiddleware(
|
|
29
|
+
"openai:gpt-4o-mini", # First fallback
|
|
30
|
+
"anthropic:claude-3-5-sonnet-20241022", # Second fallback
|
|
31
|
+
)
|
|
32
|
+
|
|
33
|
+
agent = create_agent(
|
|
34
|
+
model="openai:gpt-4o", # Primary model
|
|
35
|
+
middleware=[fallback],
|
|
36
|
+
)
|
|
37
|
+
|
|
38
|
+
# If gpt-4o fails, automatically tries gpt-4o-mini, then claude
|
|
39
|
+
result = await agent.invoke({"messages": [HumanMessage("Hello")]})
|
|
40
|
+
```
|
|
41
|
+
"""
|
|
42
|
+
|
|
43
|
+
def __init__(
|
|
44
|
+
self,
|
|
45
|
+
first_model: str | BaseChatModel,
|
|
46
|
+
*additional_models: str | BaseChatModel,
|
|
47
|
+
) -> None:
|
|
48
|
+
"""Initialize the model fallback middleware.
|
|
49
|
+
|
|
50
|
+
Args:
|
|
51
|
+
first_model: The first fallback model to try when the primary model fails.
|
|
52
|
+
Can be a model name string or BaseChatModel instance.
|
|
53
|
+
*additional_models: Additional fallback models to try, in order.
|
|
54
|
+
Can be model name strings or BaseChatModel instances.
|
|
55
|
+
"""
|
|
56
|
+
super().__init__()
|
|
57
|
+
|
|
58
|
+
# Initialize all fallback models
|
|
59
|
+
all_models = (first_model, *additional_models)
|
|
60
|
+
self.models: list[BaseChatModel] = []
|
|
61
|
+
for model in all_models:
|
|
62
|
+
if isinstance(model, str):
|
|
63
|
+
self.models.append(init_chat_model(model))
|
|
64
|
+
else:
|
|
65
|
+
self.models.append(model)
|
|
66
|
+
|
|
67
|
+
def retry_model_request(
|
|
68
|
+
self,
|
|
69
|
+
error: Exception, # noqa: ARG002
|
|
70
|
+
request: ModelRequest,
|
|
71
|
+
state: AgentState, # noqa: ARG002
|
|
72
|
+
runtime: Runtime, # noqa: ARG002
|
|
73
|
+
attempt: int,
|
|
74
|
+
) -> ModelRequest | None:
|
|
75
|
+
"""Retry with the next fallback model.
|
|
76
|
+
|
|
77
|
+
Args:
|
|
78
|
+
error: The exception that occurred during model invocation.
|
|
79
|
+
request: The original model request that failed.
|
|
80
|
+
state: The current agent state.
|
|
81
|
+
runtime: The langgraph runtime.
|
|
82
|
+
attempt: The current attempt number (1-indexed).
|
|
83
|
+
|
|
84
|
+
Returns:
|
|
85
|
+
ModelRequest with the next fallback model, or None if all models exhausted.
|
|
86
|
+
"""
|
|
87
|
+
# attempt 1 = primary model failed, try models[0] (first fallback)
|
|
88
|
+
fallback_index = attempt - 1
|
|
89
|
+
# All fallback models exhausted
|
|
90
|
+
if fallback_index >= len(self.models):
|
|
91
|
+
return None
|
|
92
|
+
# Try next fallback model
|
|
93
|
+
request.model = self.models[fallback_index]
|
|
94
|
+
return request
|