Home / Function/ test_sync_basic_selection() — langchain Function Reference

test_sync_basic_selection() — langchain Function Reference

Architecture documentation for the test_sync_basic_selection() function in test_tool_selection.py from the langchain codebase.

Entity Profile

Dependency Diagram

graph TD
  23be8f87_edbb_d858_b399_6d159e7dc92b["test_sync_basic_selection()"]
  2ed3f696_6f8f_b1a6_6ebd_96a50ba766d5["TestLLMToolSelectorBasic"]
  23be8f87_edbb_d858_b399_6d159e7dc92b -->|defined in| 2ed3f696_6f8f_b1a6_6ebd_96a50ba766d5
  style 23be8f87_edbb_d858_b399_6d159e7dc92b fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

libs/langchain_v1/tests/unit_tests/agents/middleware/implementations/test_tool_selection.py lines 100–164

    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"]

Domain

Subdomains

Frequently Asked Questions

What does test_sync_basic_selection() do?
test_sync_basic_selection() is a function in the langchain codebase, defined in libs/langchain_v1/tests/unit_tests/agents/middleware/implementations/test_tool_selection.py.
Where is test_sync_basic_selection() defined?
test_sync_basic_selection() is defined in libs/langchain_v1/tests/unit_tests/agents/middleware/implementations/test_tool_selection.py at line 100.

Analyze Your Own Codebase

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

Try Supermodel Free