AIMessage Class — langchain Architecture
Architecture documentation for the AIMessage class in ai.py from the langchain codebase.
Entity Profile
Dependency Diagram
graph TD fcfa55b0_4a86_fa31_a156_3c38c76a0a9b["AIMessage"] b9553aad_b797_0a7b_73ed_8d05b0819c0f["BaseMessage"] fcfa55b0_4a86_fa31_a156_3c38c76a0a9b -->|extends| b9553aad_b797_0a7b_73ed_8d05b0819c0f 17a9b92d_bb83_78d8_7df7_7200745cc17b["AIMessageChunk"] fcfa55b0_4a86_fa31_a156_3c38c76a0a9b -->|extends| 17a9b92d_bb83_78d8_7df7_7200745cc17b 49c04e35_a53c_8d1b_eb09_1203ad7cdacf["ai.py"] fcfa55b0_4a86_fa31_a156_3c38c76a0a9b -->|defined in| 49c04e35_a53c_8d1b_eb09_1203ad7cdacf 6d4fd1ac_0e29_ecff_6a22_651239723692["__init__()"] fcfa55b0_4a86_fa31_a156_3c38c76a0a9b -->|method| 6d4fd1ac_0e29_ecff_6a22_651239723692 86b5edbf_6e6d_8241_f999_e5bb733ad642["lc_attributes()"] fcfa55b0_4a86_fa31_a156_3c38c76a0a9b -->|method| 86b5edbf_6e6d_8241_f999_e5bb733ad642 0ac03f3c_28ea_7006_98e8_1170592b9acc["content_blocks()"] fcfa55b0_4a86_fa31_a156_3c38c76a0a9b -->|method| 0ac03f3c_28ea_7006_98e8_1170592b9acc e637d4bd_1614_4d7b_5129_2d5715d19020["_backwards_compat_tool_calls()"] fcfa55b0_4a86_fa31_a156_3c38c76a0a9b -->|method| e637d4bd_1614_4d7b_5129_2d5715d19020 14423c9b_229c_1869_8eb7_b03ed405ae86["pretty_repr()"] fcfa55b0_4a86_fa31_a156_3c38c76a0a9b -->|method| 14423c9b_229c_1869_8eb7_b03ed405ae86
Relationship Graph
Source Code
libs/core/langchain_core/messages/ai.py lines 160–410
class AIMessage(BaseMessage):
"""Message from an AI.
An `AIMessage` is returned from a chat model as a response to a prompt.
This message represents the output of the model and consists of both
the raw output as returned by the model and standardized fields
(e.g., tool calls, usage metadata) added by the LangChain framework.
"""
tool_calls: list[ToolCall] = Field(default_factory=list)
"""If present, tool calls associated with the message."""
invalid_tool_calls: list[InvalidToolCall] = Field(default_factory=list)
"""If present, tool calls with parsing errors associated with the message."""
usage_metadata: UsageMetadata | None = None
"""If present, usage metadata for a message, such as token counts.
This is a standard representation of token usage that is consistent across models.
"""
type: Literal["ai"] = "ai"
"""The type of the message (used for deserialization)."""
@overload
def __init__(
self,
content: str | list[str | dict],
**kwargs: Any,
) -> None: ...
@overload
def __init__(
self,
content: str | list[str | dict] | None = None,
content_blocks: list[types.ContentBlock] | None = None,
**kwargs: Any,
) -> None: ...
def __init__(
self,
content: str | list[str | dict] | None = None,
content_blocks: list[types.ContentBlock] | None = None,
**kwargs: Any,
) -> None:
"""Initialize an `AIMessage`.
Specify `content` as positional arg or `content_blocks` for typing.
Args:
content: The content of the message.
content_blocks: Typed standard content.
**kwargs: Additional arguments to pass to the parent class.
"""
if content_blocks is not None:
# If there are tool calls in content_blocks, but not in tool_calls, add them
content_tool_calls = [
block for block in content_blocks if block.get("type") == "tool_call"
]
if content_tool_calls and "tool_calls" not in kwargs:
kwargs["tool_calls"] = content_tool_calls
super().__init__(
content=cast("str | list[str | dict]", content_blocks),
**kwargs,
)
else:
super().__init__(content=content, **kwargs)
@property
def lc_attributes(self) -> dict:
"""Attributes to be serialized.
Includes all attributes, even if they are derived from other initialization
arguments.
"""
return {
"tool_calls": self.tool_calls,
"invalid_tool_calls": self.invalid_tool_calls,
}
Defined In
Extends
Source
Frequently Asked Questions
What is the AIMessage class?
AIMessage is a class in the langchain codebase, defined in libs/core/langchain_core/messages/ai.py.
Where is AIMessage defined?
AIMessage is defined in libs/core/langchain_core/messages/ai.py at line 160.
What does AIMessage extend?
AIMessage extends BaseMessage, AIMessageChunk.
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free