Class AssistantMessage.Builder<B extends AssistantMessage.Builder<B>>

java.lang.Object
org.springframework.ai.chat.messages.AssistantMessage.Builder<B>
Type Parameters:
B - the concrete builder type, for subclass builders
Direct Known Subclasses:
DeepSeekAssistantMessage.AbstractBuilder
Enclosing class:
AssistantMessage

public static class AssistantMessage.Builder<B extends AssistantMessage.Builder<B>> extends Object
Builder for AssistantMessage.

Parts added through part(MessagePart), parts(List) and reasoning(ReasoningPart) keep the order they were added in. The legacy setters replace the parts of their type at build() time: content(String) replaces the text parts, toolCalls(List) the tool call parts and media(List) the media parts. The new parts take the place of the first part of that type, or go where the legacy order has them when there is none (text before tool calls and media, tool calls before media), and a null content or an empty list removes the parts of that type. The setters take effect at build() time, so they also replace parts of their type added after the setter was called. A builder without explicit parts therefore produces the legacy order (text, tool calls, media) whatever the setter call order.

  • Field Details

    • explicitParts

      protected final List<MessagePart> explicitParts
    • content

      protected @Nullable String content
      The text set through content(String). A subclass builder must set it through that method: on a builder that also holds parts, a value assigned to the field directly is ignored.
    • properties

      protected Map<String,Object> properties
    • toolCalls

      protected List<AssistantMessage.ToolCall> toolCalls
      The tool calls set through toolCalls(List). As for content, a subclass builder must set them through that method.
    • media

      protected List<Media> media
      The media set through media(List). As for content, a subclass builder must set them through that method.
  • Constructor Details

    • Builder

      protected Builder()
  • Method Details

    • self

      protected B self()
    • part

      public B part(MessagePart part)
      Appends a content part.
      Parameters:
      part - the part
      Returns:
      this builder
      Since:
      2.1.0
    • supportsParts

      protected boolean supportsParts()
      Whether build() of this builder honors part(MessagePart), parts(List) and reasoning(ReasoningPart). The base builder does. A subclass builder that overrides build() must override this method to return true and construct its message from buildParts(); otherwise part(MessagePart) throws so parts are never dropped silently.
      Returns:
      whether explicit parts are supported
      Since:
      2.1.0
    • buildParts

      protected List<MessagePart> buildParts()
      The parts this builder would put in the message: the explicit parts, with the text, tool call and media parts replaced by the corresponding legacy setters that were called. A setter whose part type is not among the explicit parts puts its parts where the legacy order has them: the text before the first tool call or media part, the tool calls before the first media part, the media at the end.
      Returns:
      the parts, in message order
      Since:
      2.1.0
    • parts

      public B parts(List<MessagePart> parts)
      Appends content parts in order.
      Parameters:
      parts - the parts
      Returns:
      this builder
      Since:
      2.1.0
    • reasoning

      public B reasoning(ReasoningPart reasoning)
      Appends a reasoning part. Equivalent to part(MessagePart), since ReasoningPart is a MessagePart.
      Parameters:
      reasoning - the reasoning part
      Returns:
      this builder
      Since:
      2.1.0
    • reasoning

      public B reasoning(List<ReasoningPart> reasoning)
      Appends reasoning parts in order.
      Parameters:
      reasoning - the reasoning parts
      Returns:
      this builder
      Since:
      2.1.0
    • content

      public B content(@Nullable String content)
    • properties

      public B properties(Map<String,Object> properties)
    • toolCalls

      public B toolCalls(List<AssistantMessage.ToolCall> toolCalls)
    • media

      public B media(List<Media> media)
    • build

      public AssistantMessage build()