ToolMessage#

class langchain_core.messages.tool.ToolMessage[source]#

Bases: BaseMessage

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

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

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.

Added in version 0.2.17.

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',
)

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

Pass in content as positional arg.

Parameters:
  • content – The string contents of the message.

  • kwargs – Additional fields to pass to the

param additional_kwargs: dict [Optional]#

Currently inherited from BaseMessage, but not used.

param artifact: Any = None#

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.

Added in version 0.2.17.

param content: str | list[str | dict] [Required]#

The string contents of the message.

param id: str | None = None#

An optional unique identifier for the message. This should ideally be provided by the provider/model which created the message.

param name: str | None = None#

An optional name for the message.

This can be used to provide a human-readable name for the message.

Usage of this field is optional, and whether it’s used or not is up to the model implementation.

param response_metadata: dict [Optional]#

Currently inherited from BaseMessage, but not used.

param status: Literal['success', 'error'] = 'success'#

Status of the tool invocation.

Added in version 0.2.24.

param tool_call_id: str [Required]#

Tool call that this message is responding to.

param type: Literal['tool'] = 'tool'#

The type of the message (used for serialization). Defaults to β€œtool”.

pretty_print() β†’ None#
Return type:

None

pretty_repr(html: bool = False) β†’ str#

Get a pretty representation of the message.

Parameters:

html (bool) – Whether to format the message as HTML. If True, the message will be formatted with HTML tags. Default is False.

Returns:

A pretty representation of the message.

Return type:

str

Examples using ToolMessage