Home / Function/ _process_selection_response() — langchain Function Reference

_process_selection_response() — langchain Function Reference

Architecture documentation for the _process_selection_response() function in tool_selection.py from the langchain codebase.

Entity Profile

Dependency Diagram

graph TD
  963a6e36_74b3_e68c_c642_f81d47a7666b["_process_selection_response()"]
  888c953d_8f6f_ac7f_4274_ca04589e186a["LLMToolSelectorMiddleware"]
  963a6e36_74b3_e68c_c642_f81d47a7666b -->|defined in| 888c953d_8f6f_ac7f_4274_ca04589e186a
  79ce050f_9134_368b_2f3c_59dd0f33524c["wrap_model_call()"]
  79ce050f_9134_368b_2f3c_59dd0f33524c -->|calls| 963a6e36_74b3_e68c_c642_f81d47a7666b
  914b6a96_704c_ef23_6236_b6d24e64245f["awrap_model_call()"]
  914b6a96_704c_ef23_6236_b6d24e64245f -->|calls| 963a6e36_74b3_e68c_c642_f81d47a7666b
  style 963a6e36_74b3_e68c_c642_f81d47a7666b fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

libs/langchain_v1/langchain/agents/middleware/tool_selection.py lines 232–272

    def _process_selection_response(
        self,
        response: dict[str, Any],
        available_tools: list[BaseTool],
        valid_tool_names: list[str],
        request: ModelRequest[ContextT],
    ) -> ModelRequest[ContextT]:
        """Process the selection response and return filtered `ModelRequest`."""
        selected_tool_names: list[str] = []
        invalid_tool_selections = []

        for tool_name in response["tools"]:
            if tool_name not in valid_tool_names:
                invalid_tool_selections.append(tool_name)
                continue

            # Only add if not already selected and within max_tools limit
            if tool_name not in selected_tool_names and (
                self.max_tools is None or len(selected_tool_names) < self.max_tools
            ):
                selected_tool_names.append(tool_name)

        if invalid_tool_selections:
            msg = f"Model selected invalid tools: {invalid_tool_selections}"
            raise ValueError(msg)

        # Filter tools based on selection and append always-included tools
        selected_tools: list[BaseTool] = [
            tool for tool in available_tools if tool.name in selected_tool_names
        ]
        always_included_tools: list[BaseTool] = [
            tool
            for tool in request.tools
            if not isinstance(tool, dict) and tool.name in self.always_include
        ]
        selected_tools.extend(always_included_tools)

        # Also preserve any provider-specific tool dicts from the original request
        provider_tools = [tool for tool in request.tools if isinstance(tool, dict)]

        return request.override(tools=[*selected_tools, *provider_tools])

Domain

Subdomains

Frequently Asked Questions

What does _process_selection_response() do?
_process_selection_response() is a function in the langchain codebase, defined in libs/langchain_v1/langchain/agents/middleware/tool_selection.py.
Where is _process_selection_response() defined?
_process_selection_response() is defined in libs/langchain_v1/langchain/agents/middleware/tool_selection.py at line 232.
What calls _process_selection_response()?
_process_selection_response() is called by 2 function(s): awrap_model_call, wrap_model_call.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free