Home / File/ tool_selection.py — langchain Source File

tool_selection.py — langchain Source File

Architecture documentation for tool_selection.py, a python file in the langchain codebase. 11 imports, 0 dependents.

File python CoreAbstractions RunnableInterface 11 imports 3 functions 3 classes

Entity Profile

Dependency Diagram

graph LR
  f80e85fa_bcb6_d1a7_cf35_31c556a16099["tool_selection.py"]
  2a7f66a7_8738_3d47_375b_70fcaa6ac169["logging"]
  f80e85fa_bcb6_d1a7_cf35_31c556a16099 --> 2a7f66a7_8738_3d47_375b_70fcaa6ac169
  aac5f8ad_7f2a_3a8e_3b4b_b07d681cbdcf["dataclasses"]
  f80e85fa_bcb6_d1a7_cf35_31c556a16099 --> aac5f8ad_7f2a_3a8e_3b4b_b07d681cbdcf
  8e2034b7_ceb8_963f_29fc_2ea6b50ef9b3["typing"]
  f80e85fa_bcb6_d1a7_cf35_31c556a16099 --> 8e2034b7_ceb8_963f_29fc_2ea6b50ef9b3
  2312f229_c199_ac88_c29f_62e2a2958404["langchain_core.language_models.chat_models"]
  f80e85fa_bcb6_d1a7_cf35_31c556a16099 --> 2312f229_c199_ac88_c29f_62e2a2958404
  d758344f_537f_649e_f467_b9d7442e86df["langchain_core.messages"]
  f80e85fa_bcb6_d1a7_cf35_31c556a16099 --> d758344f_537f_649e_f467_b9d7442e86df
  6e58aaea_f08e_c099_3cc7_f9567bfb1ae7["pydantic"]
  f80e85fa_bcb6_d1a7_cf35_31c556a16099 --> 6e58aaea_f08e_c099_3cc7_f9567bfb1ae7
  91721f45_4909_e489_8c1f_084f8bd87145["typing_extensions"]
  f80e85fa_bcb6_d1a7_cf35_31c556a16099 --> 91721f45_4909_e489_8c1f_084f8bd87145
  50acc543_e5f0_2162_cf07_c2bf50723e0c["langchain.agents.middleware.types"]
  f80e85fa_bcb6_d1a7_cf35_31c556a16099 --> 50acc543_e5f0_2162_cf07_c2bf50723e0c
  c417363a_1c8e_ad5d_1f70_204df24f50bf["langchain.chat_models.base"]
  f80e85fa_bcb6_d1a7_cf35_31c556a16099 --> c417363a_1c8e_ad5d_1f70_204df24f50bf
  cfe2bde5_180e_e3b0_df2b_55b3ebaca8e7["collections.abc"]
  f80e85fa_bcb6_d1a7_cf35_31c556a16099 --> cfe2bde5_180e_e3b0_df2b_55b3ebaca8e7
  e85266a5_b172_4ddd_2fa6_e91b38ae8c0e["langchain.tools"]
  f80e85fa_bcb6_d1a7_cf35_31c556a16099 --> e85266a5_b172_4ddd_2fa6_e91b38ae8c0e
  style f80e85fa_bcb6_d1a7_cf35_31c556a16099 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

"""LLM-based tool selector middleware."""

from __future__ import annotations

import logging
from dataclasses import dataclass
from typing import TYPE_CHECKING, Annotated, Any, Literal, Union

from langchain_core.language_models.chat_models import BaseChatModel
from langchain_core.messages import AIMessage, HumanMessage
from pydantic import Field, TypeAdapter
from typing_extensions import TypedDict

from langchain.agents.middleware.types import (
    AgentMiddleware,
    AgentState,
    ContextT,
    ModelRequest,
    ModelResponse,
    ResponseT,
)
from langchain.chat_models.base import init_chat_model

if TYPE_CHECKING:
    from collections.abc import Awaitable, Callable

    from langchain.tools import BaseTool

logger = logging.getLogger(__name__)

DEFAULT_SYSTEM_PROMPT = (
    "Your goal is to select the most relevant tools for answering the user's query."
)


@dataclass
class _SelectionRequest:
    """Prepared inputs for tool selection."""

    available_tools: list[BaseTool]
    system_message: str
    last_user_message: HumanMessage
    model: BaseChatModel
    valid_tool_names: list[str]


def _create_tool_selection_response(tools: list[BaseTool]) -> TypeAdapter[Any]:
    """Create a structured output schema for tool selection.

    Args:
        tools: Available tools to include in the schema.

    Returns:
        `TypeAdapter` for a schema where each tool name is a `Literal` with its
            description.

    Raises:
        AssertionError: If `tools` is empty.
    """
    if not tools:
// ... (299 more lines)

Subdomains

Dependencies

  • collections.abc
  • dataclasses
  • langchain.agents.middleware.types
  • langchain.chat_models.base
  • langchain.tools
  • langchain_core.language_models.chat_models
  • langchain_core.messages
  • logging
  • pydantic
  • typing
  • typing_extensions

Frequently Asked Questions

What does tool_selection.py do?
tool_selection.py is a source file in the langchain codebase, written in python. It belongs to the CoreAbstractions domain, RunnableInterface subdomain.
What functions are defined in tool_selection.py?
tool_selection.py defines 3 function(s): _create_tool_selection_response, _render_tool_list, collections.
What does tool_selection.py depend on?
tool_selection.py imports 11 module(s): collections.abc, dataclasses, langchain.agents.middleware.types, langchain.chat_models.base, langchain.tools, langchain_core.language_models.chat_models, langchain_core.messages, logging, and 3 more.
Where is tool_selection.py in the architecture?
tool_selection.py is located at libs/langchain_v1/langchain/agents/middleware/tool_selection.py (domain: CoreAbstractions, subdomain: RunnableInterface, directory: libs/langchain_v1/langchain/agents/middleware).

Analyze Your Own Codebase

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

Try Supermodel Free