LangChain Reference home pageLangChain ReferenceLangChain Reference
  • GitHub
  • Main Docs
Deep Agents
LangChain
LangGraph
Integrations
LangSmith
  • Overview
    • Overview
    • Caches
    • Callbacks
    • Documents
    • Document loaders
    • Embeddings
    • Exceptions
    • Language models
    • Serialization
    • Output parsers
    • Prompts
    • Rate limiters
    • Retrievers
    • Runnables
    • Utilities
    • Vector stores
    MCP Adapters
    Standard Tests
    Text Splitters
    ⌘I

    LangChain Assistant

    Ask a question to get started

    Enter to send•Shift+Enter new line

    Menu

    OverviewCachesCallbacksDocumentsDocument loadersEmbeddingsExceptionsLanguage modelsSerializationOutput parsersPromptsRate limitersRetrieversRunnablesUtilitiesVector stores
    MCP Adapters
    Standard Tests
    Text Splitters
    Language
    Theme
    Pythonlangchain-coretoolsstructuredStructuredToolfrom_function
    Method●Since v0.2

    from_function

    Create tool from a given function.

    A classmethod that helps to create a tool from a function.

    Copy
    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,
      args_schema: ArgsSchema | None = None,
      infer_schema: bool = True,
      *,
      response_format: Literal['content', 'content_and_artifact'] = 'content',
      parse_docstring: bool = False,
      error_on_invalid_docstring: bool = False,
      **kwargs: Any = {}
    ) -> StructuredTool

    Parameters

    NameTypeDescription
    funcCallable | None
    Default:None

    The function from which to create a tool.

    coroutineCallable[..., Awaitable[Any]] | None
    Default:None

    The async function from which to create a tool.

    namestr | None
    Default:None

    The name of the tool.

    Defaults to the function name.

    descriptionstr | None
    Default:None

    The description of the tool.

    Defaults to the function docstring.

    return_directbool
    Default:False

    Whether to return the result directly or as a callback.

    args_schemaArgsSchema | None
    Default:None

    The schema of the tool's input arguments.

    infer_schemabool
    Default:True

    Whether to infer the schema from the function's signature.

    response_formatLiteral['content', 'content_and_artifact']
    Default:'content'

    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_docstringbool
    Default:False

    If infer_schema and parse_docstring, will attempt to parse parameter descriptions from Google Style function docstrings.

    error_on_invalid_docstringbool
    Default:False

    if parse_docstring is provided, configure whether to raise ValueError on invalid Google Style docstrings.

    **kwargsAny
    Default:{}

    Additional arguments to pass to the tool

    View source on GitHub