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.
Files changed (49) hide show
  1. langchain/__init__.py +1 -1
  2. langchain/agents/__init__.py +7 -1
  3. langchain/agents/factory.py +722 -226
  4. langchain/agents/middleware/__init__.py +36 -9
  5. langchain/agents/middleware/_execution.py +388 -0
  6. langchain/agents/middleware/_redaction.py +350 -0
  7. langchain/agents/middleware/context_editing.py +46 -17
  8. langchain/agents/middleware/file_search.py +382 -0
  9. langchain/agents/middleware/human_in_the_loop.py +220 -173
  10. langchain/agents/middleware/model_call_limit.py +43 -10
  11. langchain/agents/middleware/model_fallback.py +79 -36
  12. langchain/agents/middleware/pii.py +68 -504
  13. langchain/agents/middleware/shell_tool.py +718 -0
  14. langchain/agents/middleware/summarization.py +2 -2
  15. langchain/agents/middleware/{planning.py → todo.py} +35 -16
  16. langchain/agents/middleware/tool_call_limit.py +308 -114
  17. langchain/agents/middleware/tool_emulator.py +200 -0
  18. langchain/agents/middleware/tool_retry.py +384 -0
  19. langchain/agents/middleware/tool_selection.py +25 -21
  20. langchain/agents/middleware/types.py +714 -257
  21. langchain/agents/structured_output.py +37 -27
  22. langchain/chat_models/__init__.py +7 -1
  23. langchain/chat_models/base.py +192 -190
  24. langchain/embeddings/__init__.py +13 -3
  25. langchain/embeddings/base.py +49 -29
  26. langchain/messages/__init__.py +50 -1
  27. langchain/tools/__init__.py +9 -7
  28. langchain/tools/tool_node.py +16 -1174
  29. langchain-1.0.4.dist-info/METADATA +92 -0
  30. langchain-1.0.4.dist-info/RECORD +34 -0
  31. langchain/_internal/__init__.py +0 -0
  32. langchain/_internal/_documents.py +0 -35
  33. langchain/_internal/_lazy_import.py +0 -35
  34. langchain/_internal/_prompts.py +0 -158
  35. langchain/_internal/_typing.py +0 -70
  36. langchain/_internal/_utils.py +0 -7
  37. langchain/agents/_internal/__init__.py +0 -1
  38. langchain/agents/_internal/_typing.py +0 -13
  39. langchain/agents/middleware/prompt_caching.py +0 -86
  40. langchain/documents/__init__.py +0 -7
  41. langchain/embeddings/cache.py +0 -361
  42. langchain/storage/__init__.py +0 -22
  43. langchain/storage/encoder_backed.py +0 -123
  44. langchain/storage/exceptions.py +0 -5
  45. langchain/storage/in_memory.py +0 -13
  46. langchain-1.0.0a12.dist-info/METADATA +0 -122
  47. langchain-1.0.0a12.dist-info/RECORD +0 -43
  48. {langchain-1.0.0a12.dist-info → langchain-1.0.4.dist-info}/WHEEL +0 -0
  49. {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 AgentMiddleware, AgentState, ModelRequest
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
- """Middleware that provides automatic model fallback on errors.
22
+ """Automatic fallback to alternative models on errors.
17
23
 
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.
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", # First fallback
30
- "anthropic:claude-3-5-sonnet-20241022", # Second fallback
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 gpt-4o fails, automatically tries gpt-4o-mini, then claude
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 the model fallback middleware.
52
+ """Initialize model fallback middleware.
49
53
 
50
54
  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
+ 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 retry_model_request(
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
- state: AgentState, # noqa: ARG002
72
- runtime: Runtime, # noqa: ARG002
73
- attempt: int,
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
- 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).
112
+ request: Initial model request.
113
+ handler: Async callback to execute the model.
83
114
 
84
115
  Returns:
85
- ModelRequest with the next fallback model, or None if all models exhausted.
116
+ AIMessage from successful model call.
117
+
118
+ Raises:
119
+ Exception: If all models fail, re-raises last exception.
86
120
  """
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
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