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-coremessagestoolToolMessage
    Classā—Since v0.1

    ToolMessage

    Message for passing the result of executing a tool back to a model.

    ToolMessage objects contain the result of a tool invocation. Typically, the result is encoded inside the content field.

    tool_call_id is used to associate the tool call request with the tool call response. Useful in situations where a chat model is able to request multiple tool calls in parallel.

    Copy
    ToolMessage(
      self,
      content: str | list[str | dict] | None = None,
      content_blocks: list[types.ContentBlock] | None = None,
      **kwargs: Any = {}
    )

    Bases

    BaseMessageToolOutputMixin

    Example:

    A ToolMessage representing a result of 42 from a tool call with id

    from langchain_core.messages import ToolMessage
    
    ToolMessage(content="42", tool_call_id="call_Jja7J89XsjrOLA5r!MEOW!SL")

    Example:

    A ToolMessage where only part of the tool output is sent to the model and the full output is passed in to artifact.

    from langchain_core.messages import ToolMessage
    
    tool_output = {
        "stdout": "From the graph we can see that the correlation between "
        "x and y is ...",
        "stderr": None,
        "artifacts": {"type": "image", "base64_data": "/9j/4gIcSU..."},
    }
    
    ToolMessage(
        content=tool_output["stdout"],
        artifact=tool_output,
        tool_call_id="call_Jja7J89XsjrOLA5r!MEOW!SL",
    )

    Used in Docs

    • Agents
    • Build a SQL assistant with on-demand skills
    • Build customer support with handoffs
    • Custom middleware
    • Handoffs
    (3 more not shown)

    Parameters

    NameTypeDescription
    contentstr | list[str | dict] | None
    Default:None

    The contents of the message.

    content_blockslist[types.ContentBlock] | None
    Default:None

    Typed standard content.

    **kwargsAny
    Default:{}

    Additional fields.

    Constructors

    constructor
    __init__
    NameType
    contentstr | list[str | dict] | None
    content_blockslist[types.ContentBlock] | None

    Attributes

    attribute
    tool_call_id: str

    Tool call that this message is responding to.

    attribute
    type: Literal['tool']

    The type of the message (used for serialization).

    attribute
    artifact: Any

    Artifact of the Tool execution which is not meant to be sent to the model.

    Should only be specified if it is different from the message content, e.g. if only a subset of the full tool output is being passed as message content but the full output is needed in other parts of the code.

    attribute
    status: Literal['success', 'error']

    Status of the tool invocation.

    attribute
    additional_kwargs: dict

    Currently inherited from BaseMessage, but not used.

    attribute
    response_metadata: dict

    Currently inherited from BaseMessage, but not used.

    Methods

    method
    coerce_args

    Coerce the model arguments to the correct types.

    Inherited fromBaseMessage

    Attributes

    Acontent: str | list[str | dict]
    —

    The contents of the message.

    Aname: str
    —

    The name of the function.

    Aid: str
    —

    The unique identifier of the node.

    Amodel_configAcontent_blocks: list[types.ContentBlock]
    —

    Return standard, typed ContentBlock dicts from the message.

    Atext: str
    —

    Prompt text.

    Methods

    Mis_lc_serializable
    —

    Return True as this class is serializable.

    Mget_lc_namespace
    —

    Get the namespace of the LangChain object.

    Mpretty_repr
    —

    Return a pretty representation of the message for display.

    Mpretty_print
    —

    Print a pretty representation of the message.

    Inherited fromSerializable

    Attributes

    Alc_secrets: dict[str, str]
    —

    A map of constructor argument names to secret ids.

    Alc_attributes: dict
    —

    List of attribute names that should be included in the serialized kwargs.

    Amodel_config

    Methods

    Mis_lc_serializable
    —

    Return True as this class is serializable.

    Mget_lc_namespace
    —

    Get the namespace of the LangChain object.

    Mlc_id
    —

    Return a unique identifier for this class for serialization purposes.

    Mto_json
    —

    Convert the graph to a JSON-serializable format.

    Mto_json_not_implemented
    —

    Serialize a "not implemented" object.

    View source on GitHub