Home / Class/ BaseTool Class — langchain Architecture

BaseTool Class — langchain Architecture

Architecture documentation for the BaseTool class in base.py from the langchain codebase.

Entity Profile

Dependency Diagram

graph TD
  5ebe56ae_0ac8_cb13_b5a9_ee567b924009["BaseTool"]
  80cebff4_efbd_4311_85e9_de0dc81a7eee["base.py"]
  5ebe56ae_0ac8_cb13_b5a9_ee567b924009 -->|defined in| 80cebff4_efbd_4311_85e9_de0dc81a7eee
  89ef8934_385e_99ca_6def_16de079c0578["__init_subclass__()"]
  5ebe56ae_0ac8_cb13_b5a9_ee567b924009 -->|method| 89ef8934_385e_99ca_6def_16de079c0578
  03b5faf2_3e8a_cb97_542d_fe082c169caa["__init__()"]
  5ebe56ae_0ac8_cb13_b5a9_ee567b924009 -->|method| 03b5faf2_3e8a_cb97_542d_fe082c169caa
  ef443a7f_e003_642c_a7ae_228e24f1d759["is_single_input()"]
  5ebe56ae_0ac8_cb13_b5a9_ee567b924009 -->|method| ef443a7f_e003_642c_a7ae_228e24f1d759
  0f85e751_1e8f_8d82_ae8a_eabd2b6f7d88["args()"]
  5ebe56ae_0ac8_cb13_b5a9_ee567b924009 -->|method| 0f85e751_1e8f_8d82_ae8a_eabd2b6f7d88
  b7b0f260_6a90_b70a_e9d8_029e942e59a1["tool_call_schema()"]
  5ebe56ae_0ac8_cb13_b5a9_ee567b924009 -->|method| b7b0f260_6a90_b70a_e9d8_029e942e59a1
  f97ca48c_c8d1_0cbf_9f56_272dd7ef4646["_injected_args_keys()"]
  5ebe56ae_0ac8_cb13_b5a9_ee567b924009 -->|method| f97ca48c_c8d1_0cbf_9f56_272dd7ef4646
  ecf72290_45d2_5efc_567a_33bf808fcd4e["get_input_schema()"]
  5ebe56ae_0ac8_cb13_b5a9_ee567b924009 -->|method| ecf72290_45d2_5efc_567a_33bf808fcd4e
  ede8808b_c1b5_3ed7_439d_90ebc56dd263["invoke()"]
  5ebe56ae_0ac8_cb13_b5a9_ee567b924009 -->|method| ede8808b_c1b5_3ed7_439d_90ebc56dd263
  1d771513_400f_5264_361c_04bcb00f97ac["ainvoke()"]
  5ebe56ae_0ac8_cb13_b5a9_ee567b924009 -->|method| 1d771513_400f_5264_361c_04bcb00f97ac
  0f980582_36e3_bd64_ec07_fc1d2414d385["_parse_input()"]
  5ebe56ae_0ac8_cb13_b5a9_ee567b924009 -->|method| 0f980582_36e3_bd64_ec07_fc1d2414d385
  5037b154_69c3_9e8d_30cc_b509b1c868f9["_run()"]
  5ebe56ae_0ac8_cb13_b5a9_ee567b924009 -->|method| 5037b154_69c3_9e8d_30cc_b509b1c868f9
  51230af1_3edf_c5b2_cfa4_29a1aa5cd028["_arun()"]
  5ebe56ae_0ac8_cb13_b5a9_ee567b924009 -->|method| 51230af1_3edf_c5b2_cfa4_29a1aa5cd028
  fa82d3e5_c342_0a6d_3c6f_5aa889ad3f2a["_filter_injected_args()"]
  5ebe56ae_0ac8_cb13_b5a9_ee567b924009 -->|method| fa82d3e5_c342_0a6d_3c6f_5aa889ad3f2a

Relationship Graph

Source Code

libs/core/langchain_core/tools/base.py lines 405–1134

class BaseTool(RunnableSerializable[str | dict | ToolCall, Any]):
    """Base class for all LangChain tools.

    This abstract class defines the interface that all LangChain tools must implement.

    Tools are components that can be called by agents to perform specific actions.
    """

    def __init_subclass__(cls, **kwargs: Any) -> None:
        """Validate the tool class definition during subclass creation.

        Args:
            **kwargs: Additional keyword arguments passed to the parent class.

        Raises:
            SchemaAnnotationError: If `args_schema` has incorrect type annotation.
        """
        super().__init_subclass__(**kwargs)

        args_schema_type = cls.__annotations__.get("args_schema", None)

        if args_schema_type is not None and args_schema_type == BaseModel:
            # Throw errors for common mis-annotations.
            # TODO: Use get_args / get_origin and fully
            # specify valid annotations.
            typehint_mandate = """
class ChildTool(BaseTool):
    ...
    args_schema: Type[BaseModel] = SchemaClass
    ..."""
            name = cls.__name__
            msg = (
                f"Tool definition for {name} must include valid type annotations"
                f" for argument 'args_schema' to behave as expected.\n"
                f"Expected annotation of 'Type[BaseModel]'"
                f" but got '{args_schema_type}'.\n"
                f"Expected class looks like:\n"
                f"{typehint_mandate}"
            )
            raise SchemaAnnotationError(msg)

    name: str
    """The unique name of the tool that clearly communicates its purpose."""

    description: str
    """Used to tell the model how/when/why to use the tool.

    You can provide few-shot examples as a part of the description.
    """

    args_schema: Annotated[ArgsSchema | None, SkipValidation()] = Field(
        default=None, description="The tool schema."
    )
    """Pydantic model class to validate and parse the tool's input arguments.

    Args schema should be either:

    - A subclass of `pydantic.BaseModel`.
    - A subclass of `pydantic.v1.BaseModel` if accessing v1 namespace in pydantic 2
    - A JSON schema dict
    """

    return_direct: bool = False
    """Whether to return the tool's output directly.

    Setting this to `True` means that after the tool is called, the `AgentExecutor` will
    stop looping.
    """

    verbose: bool = False
    """Whether to log the tool's progress."""

    callbacks: Callbacks = Field(default=None, exclude=True)
    """Callbacks to be called during tool execution."""

    tags: list[str] | None = None
    """Optional list of tags associated with the tool.

    These tags will be associated with each call to this tool,
    and passed as arguments to the handlers defined in `callbacks`.

Frequently Asked Questions

What is the BaseTool class?
BaseTool is a class in the langchain codebase, defined in libs/core/langchain_core/tools/base.py.
Where is BaseTool defined?
BaseTool is defined in libs/core/langchain_core/tools/base.py at line 405.

Analyze Your Own Codebase

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

Try Supermodel Free