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-coretoolsconverttool
    Functionā—Since v0.2

    tool

    Convert Python functions and Runnables to LangChain tools.

    Can be used as a decorator with or without arguments to create tools from functions.

    Functions can have any signature - the tool will automatically infer input schemas unless disabled.

    Requirements
    • Functions should have type hints for proper schema inference.
    • Functions may accept multiple arguments and return types are flexible; outputs will be serialized if needed.
    • When using with Runnable, a string name must be provided.
    Copy
    tool(
      name_or_callable: str | Callable | None = None,
      runnable: Runnable | None = None,
      *args: Any = (),
      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 = True,
      extras: dict[str, Any] | None = None
    ) -> BaseTool | Callable[[Callable | Runnable], BaseTool]

    Used in Docs

    • Agents
    • Build a custom SQL agent
    • Build a data analysis agent
    • Build a multi-source knowledge base with routing
    • Build a personal assistant with subagents
    (51 more not shown)

    Parameters

    NameTypeDescription
    name_or_callablestr | Callable | None
    Default:None

    Optional name of the tool or the Callable to be converted to a tool.

    Overrides the function's name.

    Must be provided as a positional argument.

    runnableRunnable | None
    Default:None

    Optional Runnable to convert to a tool.

    Must be provided as a positional argument.

    descriptionstr | None
    Default:None

    Optional description for the tool.

    Precedence for the tool description value is as follows:

    • This description argument (used even if docstring and/or args_schema are provided)
    • Tool function docstring (used even if args_schema is provided)
    • args_schema description (used only if description and docstring are not provided)
    *argsAny
    Default:()

    Extra positional arguments.

    Must be empty.

    return_directbool
    Default:False

    Whether to return directly from the tool rather than continuing the agent loop.

    args_schemaArgsSchema | None
    Default:None

    Optional argument schema for user to specify.

    infer_schemabool
    Default:True

    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.

    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:True

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

    extrasdict[str, Any] | None
    Default:None

    Optional provider-specific extra fields for the tool.

    Used to pass configuration that doesn't fit into standard tool fields. Chat models should process known extras when constructing model payloads.

    Example

    For example, Anthropic-specific fields like cache_control, defer_loading, or input_examples.

    View source on GitHub