langchain_core.tools.Tool

Note

Tool implements the standard Runnable Interface. 🏃

class langchain_core.tools.Tool[source]

Bases: BaseTool

Tool that takes in function or coroutine directly.

Initialize tool.

param args_schema: Optional[Type[BaseModel]] = None

Pydantic model class to validate and parse the tool’s input arguments.

param callback_manager: Optional[BaseCallbackManager] = None

Deprecated. Please use callbacks instead.

param callbacks: Optional[Union[List[BaseCallbackHandler], BaseCallbackManager]] = None

Callbacks to be called during tool execution.

param coroutine: Optional[Callable[[...], Awaitable[str]]] = None

The asynchronous version of the function.

param 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.

param func: Optional[Callable[[...], str]] = None

The function to run when the tool is called.

param handle_tool_error: Optional[Union[bool, str, Callable[[ToolException], str]]] = False

Handle the content of the ToolException thrown.

param handle_validation_error: Optional[Union[bool, str, Callable[[ValidationError], str]]] = False

Handle the content of the ValidationError thrown.

param metadata: Optional[Dict[str, Any]] = None

Optional metadata associated with the tool. Defaults to None This metadata will be associated with each call to this tool, and passed as arguments to the handlers defined in callbacks. You can use these to eg identify a specific instance of a tool with its use case.

param 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.

param tags: Optional[List[str]] = None

Optional list of tags associated with the tool. Defaults to None These tags will be associated with each call to this tool, and passed as arguments to the handlers defined in callbacks. You can use these to eg identify a specific instance of a tool with its use case.

param verbose: bool = False

Whether to log the tool’s progress.

__call__(tool_input: str, callbacks: Optional[Union[List[BaseCallbackHandler], BaseCallbackManager]] = None) str

[Deprecated] Make tool callable.

Notes

Deprecated since version langchain-core==0.1.47: Use invoke instead.

Parameters
Return type

str

async arun(tool_input: Union[str, Dict], verbose: Optional[bool] = None, start_color: Optional[str] = 'green', color: Optional[str] = 'green', callbacks: Optional[Union[List[BaseCallbackHandler], BaseCallbackManager]] = None, *, tags: Optional[List[str]] = None, metadata: Optional[Dict[str, Any]] = None, run_name: Optional[str] = None, run_id: Optional[UUID] = None, config: Optional[RunnableConfig] = None, **kwargs: Any) Any

Run the tool asynchronously.

Parameters
  • tool_input (Union[str, Dict]) –

  • verbose (Optional[bool]) –

  • start_color (Optional[str]) –

  • color (Optional[str]) –

  • callbacks (Optional[Union[List[BaseCallbackHandler], BaseCallbackManager]]) –

  • tags (Optional[List[str]]) –

  • metadata (Optional[Dict[str, Any]]) –

  • run_name (Optional[str]) –

  • run_id (Optional[UUID]) –

  • config (Optional[RunnableConfig]) –

  • kwargs (Any) –

Return type

Any

classmethod from_function(func: Optional[Callable], name: str, description: str, return_direct: bool = False, args_schema: Optional[Type[BaseModel]] = None, coroutine: Optional[Callable[[...], Awaitable[Any]]] = None, **kwargs: Any) Tool[source]

Initialize tool from a function.

Parameters
  • func (Optional[Callable]) –

  • name (str) –

  • description (str) –

  • return_direct (bool) –

  • args_schema (Optional[Type[BaseModel]]) –

  • coroutine (Optional[Callable[[...], Awaitable[Any]]]) –

  • kwargs (Any) –

Return type

Tool

run(tool_input: Union[str, Dict[str, Any]], verbose: Optional[bool] = None, start_color: Optional[str] = 'green', color: Optional[str] = 'green', callbacks: Optional[Union[List[BaseCallbackHandler], BaseCallbackManager]] = None, *, tags: Optional[List[str]] = None, metadata: Optional[Dict[str, Any]] = None, run_name: Optional[str] = None, run_id: Optional[UUID] = None, config: Optional[RunnableConfig] = None, **kwargs: Any) Any

Run the tool.

Parameters
  • tool_input (Union[str, Dict[str, Any]]) –

  • verbose (Optional[bool]) –

  • start_color (Optional[str]) –

  • color (Optional[str]) –

  • callbacks (Optional[Union[List[BaseCallbackHandler], BaseCallbackManager]]) –

  • tags (Optional[List[str]]) –

  • metadata (Optional[Dict[str, Any]]) –

  • run_name (Optional[str]) –

  • run_id (Optional[UUID]) –

  • config (Optional[RunnableConfig]) –

  • kwargs (Any) –

Return type

Any

property args: dict

The tool’s input arguments.

property is_single_input: bool

Whether the tool only accepts a single input.

Examples using Tool