from_function() — langchain Function Reference
Architecture documentation for the from_function() function in structured.py from the langchain codebase.
Entity Profile
Dependency Diagram
graph TD e9a581e7_d840_3674_b506_f8b89ea98202["from_function()"] 5e183a7b_f937_e2fe_e90a_7c6aea33188b["StructuredTool"] e9a581e7_d840_3674_b506_f8b89ea98202 -->|defined in| 5e183a7b_f937_e2fe_e90a_7c6aea33188b 7d87f77c_cb91_7e44_b83d_e6b27dffd3c2["_filter_schema_args()"] e9a581e7_d840_3674_b506_f8b89ea98202 -->|calls| 7d87f77c_cb91_7e44_b83d_e6b27dffd3c2 style e9a581e7_d840_3674_b506_f8b89ea98202 fill:#6366f1,stroke:#818cf8,color:#fff
Relationship Graph
Source Code
libs/core/langchain_core/tools/structured.py lines 133–252
def from_function(
cls,
func: Callable | None = None,
coroutine: Callable[..., Awaitable[Any]] | None = None,
name: str | None = None,
description: str | None = None,
return_direct: bool = False, # noqa: FBT001,FBT002
args_schema: ArgsSchema | None = None,
infer_schema: bool = True, # noqa: FBT001,FBT002
*,
response_format: Literal["content", "content_and_artifact"] = "content",
parse_docstring: bool = False,
error_on_invalid_docstring: bool = False,
**kwargs: Any,
) -> StructuredTool:
"""Create tool from a given function.
A classmethod that helps to create a tool from a function.
Args:
func: The function from which to create a tool.
coroutine: The async function from which to create a tool.
name: The name of the tool.
Defaults to the function name.
description: The description of the tool.
Defaults to the function docstring.
return_direct: Whether to return the result directly or as a callback.
args_schema: The schema of the tool's input arguments.
infer_schema: Whether to infer the schema from the function's signature.
response_format: 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`.
parse_docstring: If `infer_schema` and `parse_docstring`, will attempt
to parse parameter descriptions from Google Style function docstrings.
error_on_invalid_docstring: if `parse_docstring` is provided, configure
whether to raise `ValueError` on invalid Google Style docstrings.
**kwargs: Additional arguments to pass to the tool
Returns:
The tool.
Raises:
ValueError: If the function is not provided.
ValueError: If the function does not have a docstring and description
is not provided.
TypeError: If the `args_schema` is not a `BaseModel` or dict.
Examples:
```python
def add(a: int, b: int) -> int:
\"\"\"Add two numbers\"\"\"
return a + b
tool = StructuredTool.from_function(add)
tool.run(1, 2) # 3
```
"""
if func is not None:
source_function = func
elif coroutine is not None:
source_function = coroutine
else:
msg = "Function and/or coroutine must be provided"
raise ValueError(msg)
name = name or source_function.__name__
if args_schema is None and infer_schema:
# schema name is appended within function
args_schema = create_schema_from_function(
name,
source_function,
parse_docstring=parse_docstring,
error_on_invalid_docstring=error_on_invalid_docstring,
filter_args=_filter_schema_args(source_function),
)
description_ = description
if description is None and not parse_docstring:
Domain
Subdomains
Defined In
Calls
Source
Frequently Asked Questions
What does from_function() do?
from_function() is a function in the langchain codebase, defined in libs/core/langchain_core/tools/structured.py.
Where is from_function() defined?
from_function() is defined in libs/core/langchain_core/tools/structured.py at line 133.
What does from_function() call?
from_function() calls 1 function(s): _filter_schema_args.
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free