tool#
- langchain_core.tools.convert.tool(*args: str | Callable | Runnable, return_direct: bool = False, args_schema: Type | None = None, infer_schema: bool = True, response_format: Literal['content', 'content_and_artifact'] = 'content', parse_docstring: bool = False, error_on_invalid_docstring: bool = True) Callable [source]#
Make tools out of functions, can be used with or without arguments.
- Parameters:
*args (str | Callable | Runnable) – The arguments to the tool.
return_direct (bool) – Whether to return directly from the tool rather than continuing the agent loop. Defaults to False.
args_schema (Type | None) – optional argument schema for user to specify. Defaults to None.
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. Defaults to True.
response_format (Literal['content', 'content_and_artifact']) – The tool response format. If “content” then the output of the tool is interpreted as the contents of a ToolMessage. If “content_and_artifact” then the output is expected to be a two-tuple corresponding to the (content, artifact) of a ToolMessage. Defaults to “content”.
parse_docstring (bool) – if
infer_schema
andparse_docstring
, will attempt to parse parameter descriptions from Google Style function docstrings. Defaults to False.error_on_invalid_docstring (bool) – if
parse_docstring
is provided, configure whether to raise ValueError on invalid Google Style docstrings. Defaults to True.
- Returns:
The tool.
- 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 @tool(response_format="content_and_artifact") def search_api(query: str) -> Tuple[str, dict]: return "partial json of results", {"full": "object of results"}
New in version 0.2.14.
Parse Google-style docstrings:
@tool(parse_docstring=True) def foo(bar: str, baz: int) -> str: """The foo. Args: bar: The bar. baz: The baz. """ return bar foo.args_schema.schema()
{ "title": "fooSchema", "description": "The foo.", "type": "object", "properties": { "bar": { "title": "Bar", "description": "The bar.", "type": "string" }, "baz": { "title": "Baz", "description": "The baz.", "type": "integer" } }, "required": [ "bar", "baz" ] }
Note that parsing by default will raise
ValueError
if the docstring is considered invalid. A docstring is considered invalid if it contains arguments not in the function signature, or is unable to be parsed into a summary and “Args:” blocks. Examples below:# No args section def invalid_docstring_1(bar: str, baz: int) -> str: """The foo.""" return bar # Improper whitespace between summary and args section def invalid_docstring_2(bar: str, baz: int) -> str: """The foo. Args: bar: The bar. baz: The baz. """ return bar # Documented args absent from function signature def invalid_docstring_3(bar: str, baz: int) -> str: """The foo. Args: banana: The bar. monkey: The baz. """ return bar
Examples using tool