Skip to content

Commit 20514f5

Browse files
fix(langchain_v1): linting fixes for llm tool selector (#33278)
* Including server side tools by default * Fixing up typing / linting on `master`
1 parent df2ecd9 commit 20514f5

File tree

1 file changed

+18
-5
lines changed

1 file changed

+18
-5
lines changed

libs/langchain_v1/langchain/agents/middleware/tool_selection.py

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -146,9 +146,12 @@ def _prepare_selection_request(self, request: ModelRequest) -> _SelectionRequest
146146
if not request.tools or len(request.tools) == 0:
147147
return None
148148

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+
149152
# Validate that always_include tools exist
150153
if self.always_include:
151-
available_tool_names = {tool.name for tool in request.tools}
154+
available_tool_names = {tool.name for tool in base_tools}
152155
missing_tools = [
153156
name for name in self.always_include if name not in available_tool_names
154157
]
@@ -160,7 +163,7 @@ def _prepare_selection_request(self, request: ModelRequest) -> _SelectionRequest
160163
raise ValueError(msg)
161164

162165
# Separate tools that are always included from those available for selection
163-
available_tools = [tool for tool in request.tools if tool.name not in self.always_include]
166+
available_tools = [tool for tool in base_tools if tool.name not in self.always_include]
164167

165168
# If no tools available for selection, return None
166169
if not available_tools:
@@ -224,10 +227,20 @@ def _process_selection_response(
224227
raise ValueError(msg)
225228

226229
# Filter tools based on selection and append always-included tools
227-
selected_tools = [tool for tool in available_tools if tool.name in selected_tool_names]
228-
always_included_tools = [tool for tool in request.tools if tool.name in self.always_include]
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+
]
229238
selected_tools.extend(always_included_tools)
230-
request.tools = selected_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]
231244
return request
232245

233246
def modify_model_request(

0 commit comments

Comments
 (0)