LangChain Reference home pageLangChain ReferenceLangChain Reference
  • GitHub
  • Main Docs
Deep Agents
LangChain
LangGraph
Integrations
LangSmith
  • Overview
  • LangGraph Checkpoint
    Checkpoint Postgres
    Store Postgres
    Checkpoint SQLite
    LangGraph Prebuilt
    LangGraph CLI
    LangGraph SDK
    LangGraph Supervisor
    LangGraph Swarm
    ⌘I

    LangChain Assistant

    Ask a question to get started

    Enter to send•Shift+Enter new line

    Menu

    LangGraph Checkpoint
    Checkpoint Postgres
    Store Postgres
    Checkpoint SQLite
    LangGraph Prebuilt
    LangGraph CLI
    LangGraph SDK
    LangGraph Supervisor
    LangGraph Swarm
    Language
    Theme
    Pythonlanggraph.prebuilttool_nodeToolNode
    Class●Since v0.1

    ToolNode

    A node for executing tools in LangGraph workflows.

    Handles tool execution patterns including function calls, state injection, persistent storage, and control flow. Manages parallel execution, error handling.

    Use ToolNode when building custom workflows that require fine-grained control over tool execution—for example, custom routing logic, specialized error handling, or non-standard agent architectures.

    For standard ReAct-style agents, use create_agent instead. It uses ToolNode internally with sensible defaults for the agent loop, conditional routing, and error handling.

    Copy
    ToolNode(
      self,
      tools: Sequence[BaseTool | Callable],
      *,
      name: str = 'tools',
      tags: list[str] | None = None,
      handle_tool_errors: bool | str | Callable[..., str] | type[Exception] | tuple[type[Exception], ...] = _default_handle_tool_errors,
      messages_key: str = 'messages',
      wrap_tool_call: ToolCallWrapper | None = None,
      awrap_tool_call: AsyncToolCallWrapper | None = None
    )

    Bases

    RunnableCallable

    Input Formats:

    1. Graph state with messages key that has a list of messages:

      • Common representation for agentic workflows
      • Supports custom messages key via messages_key parameter
    2. Message List: [AIMessage(..., tool_calls=[...])]

      • List of messages with tool calls in the last AIMessage
    3. Direct Tool Calls: [{"name": "tool", "args": {...}, "id": "1", "type": "tool_call"}]

      • Bypasses message parsing for direct tool execution
      • For programmatic tool invocation and testing

    Output Formats:

    Output format depends on input type and tool behavior:

    For Regular tools:

    • Dict input → {"messages": [ToolMessage(...)]}
    • List input → [ToolMessage(...)]

    For Command tools:

    • Returns [Command(...)] or mixed list with regular tool outputs
    • Command can update state, trigger navigation, or send messages

    Used in Docs

    • Build a custom RAG agent with LangGraph
    • Build a custom SQL agent
    • How to evaluate a graph
    • Tools
    • Trace LangGraph applications

    Parameters

    NameTypeDescription
    tools*Sequence[BaseTool | Callable]

    A sequence of tools that can be invoked by this node.

    Supports:

    • BaseTool instances: Tools with schemas and metadata
    • Plain functions: Automatically converted to tools with inferred schemas
    namestr
    Default:'tools'

    The name identifier for this node in the graph. Used for debugging and visualization.

    tagslist[str] | None
    Default:None

    Optional metadata tags to associate with the node for filtering and organization.

    handle_tool_errorsbool | str | Callable[..., str] | type[Exception] | tuple[type[Exception], ...]
    Default:_default_handle_tool_errors

    Configuration for error handling during tool execution. Supports multiple strategies:

    • True: Catch all errors and return a ToolMessage with the default error template containing the exception details.
    • str: Catch all errors and return a ToolMessage with this custom error message string.
    • type[Exception]: Only catch exceptions with the specified type and return the default error message for it.
    • tuple[type[Exception], ...]: Only catch exceptions with the specified types and return default error messages for them.
    • Callable[..., str]: Catch exceptions matching the callable's signature and return the string result of calling it with the exception.
    • False: Disable error handling entirely, allowing exceptions to propagate.

    Defaults to a callable that:

    • Catches tool invocation errors (due to invalid arguments provided by the model) and returns a descriptive error message
    • Ignores tool execution errors (they will be re-raised)
    messages_keystr
    Default:'messages'

    The key in the state dictionary that contains the message list. This same key will be used for the output ToolMessage objects.

    Allows custom state schemas with different message field names.

    Constructors

    constructor
    __init__
    NameType
    toolsSequence[BaseTool | Callable]
    namestr
    tagslist[str] | None
    handle_tool_errorsbool | str | Callable[..., str] | type[Exception] | tuple[type[Exception], ...]
    messages_keystr
    wrap_tool_callToolCallWrapper | None
    awrap_tool_callAsyncToolCallWrapper | None

    Attributes

    attribute
    name: str
    attribute
    tools_by_name: dict[str, BaseTool]

    Mapping from tool name to BaseTool instance.

    View source on GitHub