Home / Class/ TestLLMToolSelectorBasic Class — langchain Architecture

TestLLMToolSelectorBasic Class — langchain Architecture

Architecture documentation for the TestLLMToolSelectorBasic class in test_tool_selection.py from the langchain codebase.

Entity Profile

Dependency Diagram

graph TD
  2ed3f696_6f8f_b1a6_6ebd_96a50ba766d5["TestLLMToolSelectorBasic"]
  de5a7878_b3fe_95d7_2575_7f534546dc1e["AIMessage"]
  2ed3f696_6f8f_b1a6_6ebd_96a50ba766d5 -->|extends| de5a7878_b3fe_95d7_2575_7f534546dc1e
  18e85ff8_9a5d_f800_f722_027398dc89e7["BaseTool"]
  2ed3f696_6f8f_b1a6_6ebd_96a50ba766d5 -->|extends| 18e85ff8_9a5d_f800_f722_027398dc89e7
  f96107c8_8757_b550_ad6f_b1dcda0da20b["test_tool_selection.py"]
  2ed3f696_6f8f_b1a6_6ebd_96a50ba766d5 -->|defined in| f96107c8_8757_b550_ad6f_b1dcda0da20b
  23be8f87_edbb_d858_b399_6d159e7dc92b["test_sync_basic_selection()"]
  2ed3f696_6f8f_b1a6_6ebd_96a50ba766d5 -->|method| 23be8f87_edbb_d858_b399_6d159e7dc92b
  6eb49412_c2d8_d941_5ae7_343b8905e581["test_async_basic_selection()"]
  2ed3f696_6f8f_b1a6_6ebd_96a50ba766d5 -->|method| 6eb49412_c2d8_d941_5ae7_343b8905e581

Relationship Graph

Source Code

libs/langchain_v1/tests/unit_tests/agents/middleware/implementations/test_tool_selection.py lines 97–207

class TestLLMToolSelectorBasic:
    """Test basic tool selection functionality."""

    def test_sync_basic_selection(self) -> None:
        """Test synchronous tool selection."""
        # First call: selector picks tools
        # Second call: agent uses selected tools

        model_requests = []

        @wrap_model_call
        def trace_model_requests(
            request: ModelRequest, handler: Callable[[ModelRequest], ModelResponse]
        ) -> ModelResponse:
            """Middleware to select relevant tools based on state/context."""
            # Select a small, relevant subset of tools based on state/context
            model_requests.append(request)
            return handler(request)

        tool_selection_model = FakeModel(
            messages=cycle(
                [
                    AIMessage(
                        content="",
                        tool_calls=[
                            {
                                "name": "ToolSelectionResponse",
                                "id": "1",
                                "args": {"tools": ["get_weather", "calculate"]},
                            }
                        ],
                    ),
                ]
            )
        )

        model = FakeModel(
            messages=iter(
                [
                    AIMessage(
                        content="",
                        tool_calls=[
                            {"name": "get_weather", "id": "2", "args": {"location": "Paris"}}
                        ],
                    ),
                    AIMessage(content="The weather in Paris is 72°F and sunny."),
                ]
            )
        )

        tool_selector = LLMToolSelectorMiddleware(max_tools=2, model=tool_selection_model)

        agent = create_agent(
            model=model,
            tools=[get_weather, search_web, calculate, send_email, get_stock_price],
            middleware=[tool_selector, trace_model_requests],
        )

        response = agent.invoke({"messages": [HumanMessage("What's the weather in Paris?")]})

        assert isinstance(response["messages"][-1], AIMessage)

        for request in model_requests:
            selected_tool_names = []
            for tool_ in request.tools:
                assert isinstance(tool_, BaseTool)
                selected_tool_names.append(tool_.name)
            assert selected_tool_names == ["get_weather", "calculate"]

    async def test_async_basic_selection(self) -> None:
        """Test asynchronous tool selection."""
        tool_selection_model = FakeModel(
            messages=cycle(
                [
                    AIMessage(
                        content="",
                        tool_calls=[
                            {
                                "name": "ToolSelectionResponse",
                                "id": "1",
                                "args": {"tools": ["search_web"]},

Frequently Asked Questions

What is the TestLLMToolSelectorBasic class?
TestLLMToolSelectorBasic is a class in the langchain codebase, defined in libs/langchain_v1/tests/unit_tests/agents/middleware/implementations/test_tool_selection.py.
Where is TestLLMToolSelectorBasic defined?
TestLLMToolSelectorBasic is defined in libs/langchain_v1/tests/unit_tests/agents/middleware/implementations/test_tool_selection.py at line 97.
What does TestLLMToolSelectorBasic extend?
TestLLMToolSelectorBasic extends AIMessage, BaseTool.

Analyze Your Own Codebase

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

Try Supermodel Free