Class AbstractMessage

java.lang.Object
org.springframework.ai.chat.messages.AbstractMessage
All Implemented Interfaces:
Message, Content
Direct Known Subclasses:
AssistantMessage, SystemMessage, ToolResponseMessage, UserMessage

public abstract class AbstractMessage extends Object implements Message
The AbstractMessage class is an abstract implementation of the Message interface. It provides a base implementation for message content, media attachments, metadata, and message type.
See Also:
  • Field Details

    • MESSAGE_TYPE

      public static final String MESSAGE_TYPE
      The key for the message type in the metadata.
      See Also:
    • messageType

      protected final MessageType messageType
      The message type of the message.
    • textContent

      @Deprecated(since="2.1.0", forRemoval=true) protected final @Nullable String textContent
      Deprecated, for removal: This API element is subject to removal in a future version.
      since 2.1.0 in favor of getText(); kept so existing subclasses that read the field keep compiling. Will be removed in a future release.
      The text content of this message, derived from its TextParts.
    • metadata

      protected final Map<String,Object> metadata
      Additional options for the message to influence the response, not a generative map.
  • Constructor Details

    • AbstractMessage

      protected AbstractMessage(MessageType messageType, @Nullable String textContent, Map<String,Object> metadata)
      Create a new AbstractMessage with the given message type, text content, and metadata.
      Parameters:
      messageType - the message type
      textContent - the text content
      metadata - the metadata
    • AbstractMessage

      protected AbstractMessage(MessageType messageType, List<MessagePart> parts, Map<String,Object> metadata)
      Create a new AbstractMessage from an ordered list of parts. The text content is derived by joining the TextParts in the list, without a separator.
      Parameters:
      messageType - the message type
      parts - the parts, in order
      metadata - the metadata
      Since:
      2.1.0
  • Method Details

    • legacyParts

      protected static List<MessagePart> legacyParts(@Nullable String textContent)
    • select

      protected <P extends MessagePart> Stream<P> select(Class<P> type)
    • getParts

      public List<MessagePart> getParts()
      The ordered content parts of this message.
      Returns:
      an unmodifiable list of parts
      Since:
      2.1.0
    • getText

      public @Nullable String getText()
      Get the content of the message.
      Specified by:
      getText in interface Content
      Returns:
      the content of the message
    • getMetadata

      public Map<String,Object> getMetadata()
      Get the metadata of the message.
      Specified by:
      getMetadata in interface Content
      Returns:
      the metadata of the message
    • getMessageType

      public MessageType getMessageType()
      Get the message type of the message.
      Specified by:
      getMessageType in interface Message
      Returns:
      the message type of the message
    • equals

      public boolean equals(@Nullable Object o)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object