langchain_core.tools.toolĀ¶

langchain_core.tools.tool(*args: Union[str, Callable, Runnable], return_direct: bool = False, args_schema: Optional[Type[BaseModel]] = None, infer_schema: bool = True) Callable[source]Ā¶

Make tools out of functions, can be used with or without arguments.

Parameters
  • *args (Union[str, Callable, Runnable]) ā€“ The arguments to the tool.

  • return_direct (bool) ā€“ Whether to return directly from the tool rather than continuing the agent loop.

  • args_schema (Optional[Type[BaseModel]]) ā€“ optional argument schema for user to specify

  • infer_schema (bool) ā€“ Whether to infer the schema of the arguments from the functionā€™s signature. This also makes the resultant tool accept a dictionary input to its run() function.

Return type

Callable

Requires:
  • Function must be of type (str) -> str

  • Function must have a docstring

Examples

@tool
def search_api(query: str) -> str:
    # Searches the API for the query.
    return

@tool("search", return_direct=True)
def search_api(query: str) -> str:
    # Searches the API for the query.
    return

Examples using toolĀ¶