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.

Files changed (36) hide show
  1. langchain/__init__.py +1 -24
  2. langchain/_internal/_documents.py +1 -1
  3. langchain/_internal/_prompts.py +2 -2
  4. langchain/_internal/_typing.py +1 -1
  5. langchain/agents/__init__.py +2 -3
  6. langchain/agents/factory.py +1126 -0
  7. langchain/agents/middleware/__init__.py +38 -1
  8. langchain/agents/middleware/context_editing.py +245 -0
  9. langchain/agents/middleware/human_in_the_loop.py +67 -20
  10. langchain/agents/middleware/model_call_limit.py +177 -0
  11. langchain/agents/middleware/model_fallback.py +94 -0
  12. langchain/agents/middleware/pii.py +753 -0
  13. langchain/agents/middleware/planning.py +201 -0
  14. langchain/agents/middleware/prompt_caching.py +7 -4
  15. langchain/agents/middleware/summarization.py +2 -1
  16. langchain/agents/middleware/tool_call_limit.py +260 -0
  17. langchain/agents/middleware/tool_selection.py +306 -0
  18. langchain/agents/middleware/types.py +708 -127
  19. langchain/agents/structured_output.py +15 -1
  20. langchain/chat_models/base.py +22 -25
  21. langchain/embeddings/base.py +3 -4
  22. langchain/embeddings/cache.py +0 -1
  23. langchain/messages/__init__.py +29 -0
  24. langchain/rate_limiters/__init__.py +13 -0
  25. langchain/tools/__init__.py +9 -0
  26. langchain/{agents → tools}/tool_node.py +8 -10
  27. {langchain-1.0.0a9.dist-info → langchain-1.0.0a11.dist-info}/METADATA +29 -35
  28. langchain-1.0.0a11.dist-info/RECORD +43 -0
  29. {langchain-1.0.0a9.dist-info → langchain-1.0.0a11.dist-info}/WHEEL +1 -1
  30. langchain/agents/middleware_agent.py +0 -617
  31. langchain/agents/react_agent.py +0 -1228
  32. langchain/globals.py +0 -18
  33. langchain/text_splitter.py +0 -50
  34. langchain-1.0.0a9.dist-info/RECORD +0 -38
  35. langchain-1.0.0a9.dist-info/entry_points.txt +0 -4
  36. {langchain-1.0.0a9.dist-info → langchain-1.0.0a11.dist-info}/licenses/LICENSE +0 -0
@@ -0,0 +1,306 @@
1
+ """LLM-based tool selector middleware."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import logging
6
+ from dataclasses import dataclass
7
+ from typing import TYPE_CHECKING, Annotated, Literal, Union
8
+
9
+ from langchain_core.language_models.chat_models import BaseChatModel
10
+ from langchain_core.messages import HumanMessage
11
+ from pydantic import Field, TypeAdapter
12
+ from typing_extensions import TypedDict
13
+
14
+ from langchain.agents.middleware.types import AgentMiddleware, AgentState, ModelRequest, StateT
15
+ from langchain.chat_models.base import init_chat_model
16
+
17
+ if TYPE_CHECKING:
18
+ from langgraph.runtime import Runtime
19
+ from langgraph.typing import ContextT
20
+
21
+ from langchain.tools import BaseTool
22
+
23
+ logger = logging.getLogger(__name__)
24
+
25
+ DEFAULT_SYSTEM_PROMPT = (
26
+ "Your goal is to select the most relevant tools for answering the user's query."
27
+ )
28
+
29
+
30
+ @dataclass
31
+ class _SelectionRequest:
32
+ """Prepared inputs for tool selection."""
33
+
34
+ available_tools: list[BaseTool]
35
+ system_message: str
36
+ last_user_message: HumanMessage
37
+ model: BaseChatModel
38
+ valid_tool_names: list[str]
39
+
40
+
41
+ def _create_tool_selection_response(tools: list[BaseTool]) -> TypeAdapter:
42
+ """Create a structured output schema for tool selection.
43
+
44
+ Args:
45
+ tools: Available tools to include in the schema.
46
+
47
+ Returns:
48
+ TypeAdapter for a schema where each tool name is a Literal with its description.
49
+ """
50
+ if not tools:
51
+ msg = "Invalid usage: tools must be non-empty"
52
+ raise AssertionError(msg)
53
+
54
+ # Create a Union of Annotated Literal types for each tool name with description
55
+ # Example: Union[Annotated[Literal["tool1"], Field(description="...")], ...] noqa: ERA001
56
+ literals = [
57
+ Annotated[Literal[tool.name], Field(description=tool.description)] for tool in tools
58
+ ]
59
+ selected_tool_type = Union[tuple(literals)] # type: ignore[valid-type] # noqa: UP007
60
+
61
+ description = "Tools to use. Place the most relevant tools first."
62
+
63
+ class ToolSelectionResponse(TypedDict):
64
+ """Use to select relevant tools."""
65
+
66
+ tools: Annotated[list[selected_tool_type], Field(description=description)] # type: ignore[valid-type]
67
+
68
+ return TypeAdapter(ToolSelectionResponse)
69
+
70
+
71
+ def _render_tool_list(tools: list[BaseTool]) -> str:
72
+ """Format tools as markdown list.
73
+
74
+ Args:
75
+ tools: Tools to format.
76
+
77
+ Returns:
78
+ Markdown string with each tool on a new line.
79
+ """
80
+ return "\n".join(f"- {tool.name}: {tool.description}" for tool in tools)
81
+
82
+
83
+ class LLMToolSelectorMiddleware(AgentMiddleware):
84
+ """Uses an LLM to select relevant tools before calling the main model.
85
+
86
+ When an agent has many tools available, this middleware filters them down
87
+ to only the most relevant ones for the user's query. This reduces token usage
88
+ and helps the main model focus on the right tools.
89
+
90
+ Examples:
91
+ Limit to 3 tools:
92
+ ```python
93
+ from langchain.agents.middleware import LLMToolSelectorMiddleware
94
+
95
+ middleware = LLMToolSelectorMiddleware(max_tools=3)
96
+
97
+ agent = create_agent(
98
+ model="openai:gpt-4o",
99
+ tools=[tool1, tool2, tool3, tool4, tool5],
100
+ middleware=[middleware],
101
+ )
102
+ ```
103
+
104
+ Use a smaller model for selection:
105
+ ```python
106
+ middleware = LLMToolSelectorMiddleware(model="openai:gpt-4o-mini", max_tools=2)
107
+ ```
108
+ """
109
+
110
+ def __init__(
111
+ self,
112
+ *,
113
+ model: str | BaseChatModel | None = None,
114
+ system_prompt: str = DEFAULT_SYSTEM_PROMPT,
115
+ max_tools: int | None = None,
116
+ always_include: list[str] | None = None,
117
+ ) -> None:
118
+ """Initialize the tool selector.
119
+
120
+ Args:
121
+ model: Model to use for selection. If not provided, uses the agent's main model.
122
+ Can be a model identifier string or BaseChatModel instance.
123
+ system_prompt: Instructions for the selection model.
124
+ max_tools: Maximum number of tools to select. If the model selects more,
125
+ only the first max_tools will be used. No limit if not specified.
126
+ always_include: Tool names to always include regardless of selection.
127
+ These do not count against the max_tools limit.
128
+ """
129
+ super().__init__()
130
+ self.system_prompt = system_prompt
131
+ self.max_tools = max_tools
132
+ self.always_include = always_include or []
133
+
134
+ if isinstance(model, (BaseChatModel, type(None))):
135
+ self.model: BaseChatModel | None = model
136
+ else:
137
+ self.model = init_chat_model(model)
138
+
139
+ def _prepare_selection_request(self, request: ModelRequest) -> _SelectionRequest | None:
140
+ """Prepare inputs for tool selection.
141
+
142
+ Returns:
143
+ SelectionRequest with prepared inputs, or None if no selection is needed.
144
+ """
145
+ # If no tools available, return None
146
+ if not request.tools or len(request.tools) == 0:
147
+ return None
148
+
149
+ # Filter to only BaseTool instances (exclude provider-specific tool dicts)
150
+ base_tools = [tool for tool in request.tools if not isinstance(tool, dict)]
151
+
152
+ # Validate that always_include tools exist
153
+ if self.always_include:
154
+ available_tool_names = {tool.name for tool in base_tools}
155
+ missing_tools = [
156
+ name for name in self.always_include if name not in available_tool_names
157
+ ]
158
+ if missing_tools:
159
+ msg = (
160
+ f"Tools in always_include not found in request: {missing_tools}. "
161
+ f"Available tools: {sorted(available_tool_names)}"
162
+ )
163
+ raise ValueError(msg)
164
+
165
+ # Separate tools that are always included from those available for selection
166
+ available_tools = [tool for tool in base_tools if tool.name not in self.always_include]
167
+
168
+ # If no tools available for selection, return None
169
+ if not available_tools:
170
+ return None
171
+
172
+ system_message = self.system_prompt
173
+ # If there's a max_tools limit, append instructions to the system prompt
174
+ if self.max_tools is not None:
175
+ system_message += (
176
+ f"\nIMPORTANT: List the tool names in order of relevance, "
177
+ f"with the most relevant first. "
178
+ f"If you exceed the maximum number of tools, "
179
+ f"only the first {self.max_tools} will be used."
180
+ )
181
+
182
+ # Get the last user message from the conversation history
183
+ last_user_message: HumanMessage
184
+ for message in reversed(request.messages):
185
+ if isinstance(message, HumanMessage):
186
+ last_user_message = message
187
+ break
188
+ else:
189
+ msg = "No user message found in request messages"
190
+ raise AssertionError(msg)
191
+
192
+ model = self.model or request.model
193
+ valid_tool_names = [tool.name for tool in available_tools]
194
+
195
+ return _SelectionRequest(
196
+ available_tools=available_tools,
197
+ system_message=system_message,
198
+ last_user_message=last_user_message,
199
+ model=model,
200
+ valid_tool_names=valid_tool_names,
201
+ )
202
+
203
+ def _process_selection_response(
204
+ self,
205
+ response: dict,
206
+ available_tools: list[BaseTool],
207
+ valid_tool_names: list[str],
208
+ request: ModelRequest,
209
+ ) -> ModelRequest:
210
+ """Process the selection response and return filtered ModelRequest."""
211
+ selected_tool_names: list[str] = []
212
+ invalid_tool_selections = []
213
+
214
+ for tool_name in response["tools"]:
215
+ if tool_name not in valid_tool_names:
216
+ invalid_tool_selections.append(tool_name)
217
+ continue
218
+
219
+ # Only add if not already selected and within max_tools limit
220
+ if tool_name not in selected_tool_names and (
221
+ self.max_tools is None or len(selected_tool_names) < self.max_tools
222
+ ):
223
+ selected_tool_names.append(tool_name)
224
+
225
+ if invalid_tool_selections:
226
+ msg = f"Model selected invalid tools: {invalid_tool_selections}"
227
+ raise ValueError(msg)
228
+
229
+ # Filter tools based on selection and append always-included tools
230
+ selected_tools: list[BaseTool] = [
231
+ tool for tool in available_tools if tool.name in selected_tool_names
232
+ ]
233
+ always_included_tools: list[BaseTool] = [
234
+ tool
235
+ for tool in request.tools
236
+ if not isinstance(tool, dict) and tool.name in self.always_include
237
+ ]
238
+ selected_tools.extend(always_included_tools)
239
+
240
+ # Also preserve any provider-specific tool dicts from the original request
241
+ provider_tools = [tool for tool in request.tools if isinstance(tool, dict)]
242
+
243
+ request.tools = [*selected_tools, *provider_tools]
244
+ return request
245
+
246
+ def modify_model_request(
247
+ self,
248
+ request: ModelRequest,
249
+ state: StateT, # noqa: ARG002
250
+ runtime: Runtime[ContextT], # noqa: ARG002
251
+ ) -> ModelRequest:
252
+ """Modify the model request to filter tools based on LLM selection."""
253
+ selection_request = self._prepare_selection_request(request)
254
+ if selection_request is None:
255
+ return request
256
+
257
+ # Create dynamic response model with Literal enum of available tool names
258
+ type_adapter = _create_tool_selection_response(selection_request.available_tools)
259
+ schema = type_adapter.json_schema()
260
+ structured_model = selection_request.model.with_structured_output(schema)
261
+
262
+ response = structured_model.invoke(
263
+ [
264
+ {"role": "system", "content": selection_request.system_message},
265
+ selection_request.last_user_message,
266
+ ]
267
+ )
268
+
269
+ # Response should be a dict since we're passing a schema (not a Pydantic model class)
270
+ if not isinstance(response, dict):
271
+ msg = f"Expected dict response, got {type(response)}"
272
+ raise AssertionError(msg)
273
+ return self._process_selection_response(
274
+ response, selection_request.available_tools, selection_request.valid_tool_names, request
275
+ )
276
+
277
+ async def amodify_model_request(
278
+ self,
279
+ request: ModelRequest,
280
+ state: AgentState, # noqa: ARG002
281
+ runtime: Runtime, # noqa: ARG002
282
+ ) -> ModelRequest:
283
+ """Modify the model request to filter tools based on LLM selection."""
284
+ selection_request = self._prepare_selection_request(request)
285
+ if selection_request is None:
286
+ return request
287
+
288
+ # Create dynamic response model with Literal enum of available tool names
289
+ type_adapter = _create_tool_selection_response(selection_request.available_tools)
290
+ schema = type_adapter.json_schema()
291
+ structured_model = selection_request.model.with_structured_output(schema)
292
+
293
+ response = await structured_model.ainvoke(
294
+ [
295
+ {"role": "system", "content": selection_request.system_message},
296
+ selection_request.last_user_message,
297
+ ]
298
+ )
299
+
300
+ # Response should be a dict since we're passing a schema (not a Pydantic model class)
301
+ if not isinstance(response, dict):
302
+ msg = f"Expected dict response, got {type(response)}"
303
+ raise AssertionError(msg)
304
+ return self._process_selection_response(
305
+ response, selection_request.available_tools, selection_request.valid_tool_names, request
306
+ )