Home / Class/ BaseMessage Class — langchain Architecture

BaseMessage Class — langchain Architecture

Architecture documentation for the BaseMessage class in base.py from the langchain codebase.

Entity Profile

Dependency Diagram

graph TD
  b9553aad_b797_0a7b_73ed_8d05b0819c0f["BaseMessage"]
  f3658565_d05c_7f49_b7f8_622b7ef34f33["Serializable"]
  b9553aad_b797_0a7b_73ed_8d05b0819c0f -->|extends| f3658565_d05c_7f49_b7f8_622b7ef34f33
  323271a6_dbeb_1ac5_e224_a66bfb56191b["base.py"]
  b9553aad_b797_0a7b_73ed_8d05b0819c0f -->|defined in| 323271a6_dbeb_1ac5_e224_a66bfb56191b
  5fbe1fa6_2070_db52_a95d_429b6875591f["__init__()"]
  b9553aad_b797_0a7b_73ed_8d05b0819c0f -->|method| 5fbe1fa6_2070_db52_a95d_429b6875591f
  cad44b6e_ee51_3f9b_63cb_e831e8ebf4a1["is_lc_serializable()"]
  b9553aad_b797_0a7b_73ed_8d05b0819c0f -->|method| cad44b6e_ee51_3f9b_63cb_e831e8ebf4a1
  ed0ef493_7056_0426_139c_50b05fae7415["get_lc_namespace()"]
  b9553aad_b797_0a7b_73ed_8d05b0819c0f -->|method| ed0ef493_7056_0426_139c_50b05fae7415
  f0f20fe1_dffd_001c_e2a1_844d0b37299e["content_blocks()"]
  b9553aad_b797_0a7b_73ed_8d05b0819c0f -->|method| f0f20fe1_dffd_001c_e2a1_844d0b37299e
  8a5e9826_a8be_d1ab_95e1_2c66dddf03e7["text()"]
  b9553aad_b797_0a7b_73ed_8d05b0819c0f -->|method| 8a5e9826_a8be_d1ab_95e1_2c66dddf03e7
  92d91f48_a273_723d_04ba_de992881cff8["__add__()"]
  b9553aad_b797_0a7b_73ed_8d05b0819c0f -->|method| 92d91f48_a273_723d_04ba_de992881cff8
  51c7ee24_c973_598e_e55c_4fe0f69bb436["pretty_repr()"]
  b9553aad_b797_0a7b_73ed_8d05b0819c0f -->|method| 51c7ee24_c973_598e_e55c_4fe0f69bb436
  bf7f8a8e_78bd_d925_2eb9_e4d07f210261["pretty_print()"]
  b9553aad_b797_0a7b_73ed_8d05b0819c0f -->|method| bf7f8a8e_78bd_d925_2eb9_e4d07f210261

Relationship Graph

Source Code

libs/core/langchain_core/messages/base.py lines 93–363

class BaseMessage(Serializable):
    """Base abstract message class.

    Messages are the inputs and outputs of a chat model.

    Examples include [`HumanMessage`][langchain.messages.HumanMessage],
    [`AIMessage`][langchain.messages.AIMessage], and
    [`SystemMessage`][langchain.messages.SystemMessage].
    """

    content: str | list[str | dict]
    """The contents of the message."""

    additional_kwargs: dict = Field(default_factory=dict)
    """Reserved for additional payload data associated with the message.

    For example, for a message from an AI, this could include tool calls as
    encoded by the model provider.

    """

    response_metadata: dict = Field(default_factory=dict)
    """Examples: response headers, logprobs, token counts, model name."""

    type: str
    """The type of the message. Must be a string that is unique to the message type.

    The purpose of this field is to allow for easy identification of the message type
    when deserializing messages.

    """

    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.

    """

    id: str | None = Field(default=None, coerce_numbers_to_str=True)
    """An optional unique identifier for the message.

    This should ideally be provided by the provider/model which created the message.

    """

    model_config = ConfigDict(
        extra="allow",
    )

    @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 a `BaseMessage`.

        Specify `content` as positional arg or `content_blocks` for typing.

        Args:
            content: The contents of the message.
            content_blocks: Typed standard content.

Extends

Frequently Asked Questions

What is the BaseMessage class?
BaseMessage is a class in the langchain codebase, defined in libs/core/langchain_core/messages/base.py.
Where is BaseMessage defined?
BaseMessage is defined in libs/core/langchain_core/messages/base.py at line 93.
What does BaseMessage extend?
BaseMessage extends Serializable.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free