Class AssistantMessage

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

public class AssistantMessage extends AbstractMessage implements MediaContent
Lets the generative know the content was generated as a response to the user. This role indicates messages that the generative has previously generated in the conversation. By including assistant messages in the series, you provide context to the generative about prior exchanges in the conversation.

The message content is an ordered list of MessageParts, inherited from AbstractMessage so that UserMessage can carry the same kind of content. The text, tool calls, media and reasoning accessors are views over that list: AbstractMessage.getText() joins the TextParts, getToolCalls() selects the ToolCallParts, getMedia() selects the MediaParts and getReasoning() selects the ReasoningParts, in part order. Because reasoning is a part like any other, a message preserves the exact interleaving some providers stream between reasoning, tool calls, text and media (for example a reasoning block immediately followed by the tool call it justified). Messages built through the legacy constructor or builder setters get their parts in the legacy order: text, tool calls, media.

Since:
1.0.0
Author:
Mark Pollack, Christian Tzolov, Thomas Vitale, guan xu
  • Field Details

    • media

      @Deprecated(since="2.1.0", forRemoval=true) protected final List<Media> media
      Deprecated, for removal: This API element is subject to removal in a future version.
      since 2.1.0 in favor of getMedia(); kept so existing subclasses that read the field keep compiling. Will be removed in a future release.
      The media of this message, derived from its MediaParts.
  • Constructor Details

    • AssistantMessage

      public AssistantMessage(@Nullable String content)
    • AssistantMessage

      protected AssistantMessage(@Nullable String content, Map<String,Object> properties, List<AssistantMessage.ToolCall> toolCalls, List<Media> media)
    • AssistantMessage

      protected AssistantMessage(List<MessagePart> parts, Map<String,Object> properties)
      Creates a message from an ordered list of content parts, which may include ReasoningParts interleaved with the rest of the content.
      Parameters:
      parts - the parts, in order
      properties - the message metadata
      Since:
      2.1.0
  • Method Details