Tool Class — langchain Architecture
Architecture documentation for the Tool class in simple.py from the langchain codebase.
Entity Profile
Dependency Diagram
graph TD 4ddde15c_138a_6971_91c4_325c8c3034dd["Tool"] 5ebe56ae_0ac8_cb13_b5a9_ee567b924009["BaseTool"] 4ddde15c_138a_6971_91c4_325c8c3034dd -->|extends| 5ebe56ae_0ac8_cb13_b5a9_ee567b924009 9d355c0c_a962_0eca_ed0e_1965c24ec58d["simple.py"] 4ddde15c_138a_6971_91c4_325c8c3034dd -->|defined in| 9d355c0c_a962_0eca_ed0e_1965c24ec58d c59ae48f_8cb1_da34_aa48_384293a31029["ainvoke()"] 4ddde15c_138a_6971_91c4_325c8c3034dd -->|method| c59ae48f_8cb1_da34_aa48_384293a31029 0a9c64df_ea07_8327_d884_4ffa15651d1d["args()"] 4ddde15c_138a_6971_91c4_325c8c3034dd -->|method| 0a9c64df_ea07_8327_d884_4ffa15651d1d a97adaea_b5f1_2906_9ae5_0d37c4c197bb["_to_args_and_kwargs()"] 4ddde15c_138a_6971_91c4_325c8c3034dd -->|method| a97adaea_b5f1_2906_9ae5_0d37c4c197bb ec4709dd_d97d_2a9b_7714_d254d0086ca6["_run()"] 4ddde15c_138a_6971_91c4_325c8c3034dd -->|method| ec4709dd_d97d_2a9b_7714_d254d0086ca6 376a9053_83b8_2a33_110c_2996e39084b9["_arun()"] 4ddde15c_138a_6971_91c4_325c8c3034dd -->|method| 376a9053_83b8_2a33_110c_2996e39084b9 8db506b5_e38b_b12b_5972_3638aa858bb6["__init__()"] 4ddde15c_138a_6971_91c4_325c8c3034dd -->|method| 8db506b5_e38b_b12b_5972_3638aa858bb6 a4750a39_8afe_6ac4_ad0b_49c77bf6dcda["from_function()"] 4ddde15c_138a_6971_91c4_325c8c3034dd -->|method| a4750a39_8afe_6ac4_ad0b_49c77bf6dcda
Relationship Graph
Source Code
libs/core/langchain_core/tools/simple.py lines 31–204
class Tool(BaseTool):
"""Tool that takes in function or coroutine directly."""
description: str = ""
func: Callable[..., str] | None
"""The function to run when the tool is called."""
coroutine: Callable[..., Awaitable[str]] | None = None
"""The asynchronous version of the function."""
# --- Runnable ---
@override
async def ainvoke(
self,
input: str | dict | ToolCall,
config: RunnableConfig | None = None,
**kwargs: Any,
) -> Any:
if not self.coroutine:
# If the tool does not implement async, fall back to default implementation
return await run_in_executor(config, self.invoke, input, config, **kwargs)
return await super().ainvoke(input, config, **kwargs)
# --- Tool ---
@property
def args(self) -> dict:
"""The tool's input arguments.
Returns:
The input arguments for the tool.
"""
if self.args_schema is not None:
return super().args
# For backwards compatibility, if the function signature is ambiguous,
# assume it takes a single string input.
return {"tool_input": {"type": "string"}}
def _to_args_and_kwargs(
self, tool_input: str | dict, tool_call_id: str | None
) -> tuple[tuple, dict]:
"""Convert tool input to Pydantic model.
Args:
tool_input: The input to the tool.
tool_call_id: The ID of the tool call.
Raises:
ToolException: If the tool input is invalid.
Returns:
The Pydantic model args and kwargs.
"""
args, kwargs = super()._to_args_and_kwargs(tool_input, tool_call_id)
# For backwards compatibility. The tool must be run with a single input
all_args = list(args) + list(kwargs.values())
if len(all_args) != 1:
msg = (
f"""Too many arguments to single-input tool {self.name}.
Consider using StructuredTool instead."""
f" Args: {all_args}"
)
raise ToolException(msg)
return tuple(all_args), {}
def _run(
self,
*args: Any,
config: RunnableConfig,
run_manager: CallbackManagerForToolRun | None = None,
**kwargs: Any,
) -> Any:
"""Use the tool.
Args:
*args: Positional arguments to pass to the tool
config: Configuration for the run
run_manager: Optional callback manager to use for the run
Defined In
Extends
Source
Frequently Asked Questions
What is the Tool class?
Tool is a class in the langchain codebase, defined in libs/core/langchain_core/tools/simple.py.
Where is Tool defined?
Tool is defined in libs/core/langchain_core/tools/simple.py at line 31.
What does Tool extend?
Tool extends BaseTool.
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free