ToolMessage Class — langchain Architecture
Architecture documentation for the ToolMessage class in tool.py from the langchain codebase.
Entity Profile
Dependency Diagram
graph TD 4318b819_4fe9_65b0_5369_424ec9518efe["ToolMessage"] b9553aad_b797_0a7b_73ed_8d05b0819c0f["BaseMessage"] 4318b819_4fe9_65b0_5369_424ec9518efe -->|extends| b9553aad_b797_0a7b_73ed_8d05b0819c0f a6b8f122_3f0b_1642_b2eb_917414188a01["ToolOutputMixin"] 4318b819_4fe9_65b0_5369_424ec9518efe -->|extends| a6b8f122_3f0b_1642_b2eb_917414188a01 210f83c5_4be3_e20e_b877_98a194178520["tool.py"] 4318b819_4fe9_65b0_5369_424ec9518efe -->|defined in| 210f83c5_4be3_e20e_b877_98a194178520 e4276564_ae8a_ba5b_a556_c4e69f55e5ba["coerce_args()"] 4318b819_4fe9_65b0_5369_424ec9518efe -->|method| e4276564_ae8a_ba5b_a556_c4e69f55e5ba 6ceff3c1_1cd6_629f_52f9_9dace2eeb789["__init__()"] 4318b819_4fe9_65b0_5369_424ec9518efe -->|method| 6ceff3c1_1cd6_629f_52f9_9dace2eeb789
Relationship Graph
Source Code
libs/core/langchain_core/messages/tool.py lines 26–171
class ToolMessage(BaseMessage, ToolOutputMixin):
"""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.
Example:
A `ToolMessage` representing a result of `42` from a tool call with id
```python
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.
```python
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_id: str
"""Tool call that this message is responding to."""
type: Literal["tool"] = "tool"
"""The type of the message (used for serialization)."""
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.
"""
status: Literal["success", "error"] = "success"
"""Status of the tool invocation."""
additional_kwargs: dict = Field(default_factory=dict, repr=False)
"""Currently inherited from `BaseMessage`, but not used."""
response_metadata: dict = Field(default_factory=dict, repr=False)
"""Currently inherited from `BaseMessage`, but not used."""
@model_validator(mode="before")
@classmethod
def coerce_args(cls, values: dict) -> dict:
"""Coerce the model arguments to the correct types.
Args:
values: The model arguments.
"""
content = values["content"]
if isinstance(content, tuple):
content = list(content)
if not isinstance(content, (str, list)):
try:
values["content"] = str(content)
except ValueError as e:
Defined In
Extends
Source
Frequently Asked Questions
What is the ToolMessage class?
ToolMessage is a class in the langchain codebase, defined in libs/core/langchain_core/messages/tool.py.
Where is ToolMessage defined?
ToolMessage is defined in libs/core/langchain_core/messages/tool.py at line 26.
What does ToolMessage extend?
ToolMessage extends BaseMessage, ToolOutputMixin.
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free