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.
ToolMessage(
self,
content: str | list[str | dict] | None = None,
content_blocks: list[types.ContentBlock] | None = None,
**kwargs: Any = {}
)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",
)Tool call that this message is responding to.
The type of the message (used for serialization).
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.
Status of the tool invocation.
Currently inherited from BaseMessage, but not used.
Currently inherited from BaseMessage, but not used.
Return True as this class is serializable.
Get the namespace of the LangChain object.
Return a unique identifier for this class for serialization purposes.
Convert the graph to a JSON-serializable format.
Serialize a "not implemented" object.