Class StreamingParts

java.lang.Object
org.springframework.ai.chat.messages.part.StreamingParts

public final class StreamingParts extends Object
Transport attributes that let a streaming chat model deliver a message part in increments and let MessageAggregator rebuild the ordered parts list.

The contract for a streamed ChatResponse chunk:

  • A chunk's assistant message carries at most one part per content block index.
  • A part stamped with partial(MessagePart, int) is a delta: text and arguments are appended to what was already received for that index, a non-null payload replaces the accumulated one.
  • A part stamped with complete(MessagePart, int) replaces whatever was accumulated for that index.
  • Parts without an index are aggregated the pre-streaming way: text is concatenated, tool calls are appended, other parts are carried in arrival order.
  • The chat model sets ChatResponseMetadata.id on every chunk of one model response; the aggregator groups indexed parts by that id so that consecutive tool-call rounds flowing through one stream do not merge into each other.
The two attributes are removed from the aggregated message, so they never take part in message equality or persistence.
Since:
2.1.0
Author:
Christian Tzolov
  • Field Details

    • PART_INDEX_ATTRIBUTE

      public static final String PART_INDEX_ATTRIBUTE
      Attribute holding the zero-based content block index of a streamed part.
      See Also:
    • PARTIAL_ATTRIBUTE

      public static final String PARTIAL_ATTRIBUTE
      Attribute set to true on a streamed part that is a delta.
      See Also:
  • Method Details

    • partial

      public static MessagePart partial(MessagePart part, int index)
      Marks a part as a delta of the content block at the given index.
      Parameters:
      part - the part
      index - the content block index
      Returns:
      a copy of the part with the streaming attributes set
    • complete

      public static MessagePart complete(MessagePart part, int index)
      Marks a part as the complete content block at the given index.
      Parameters:
      part - the part
      index - the content block index
      Returns:
      a copy of the part with the index set and no partial flag
    • partIndex

      public static @Nullable Integer partIndex(MessagePart part)
      The content block index of a streamed part.
      Parameters:
      part - the part
      Returns:
      the index, or null when absent or malformed
    • isPartial

      public static boolean isPartial(MessagePart part)
      Whether a streamed part is a delta.
      Parameters:
      part - the part
      Returns:
      true only when the partial attribute is "true"
    • strip

      public static MessagePart strip(MessagePart part)
      Removes the streaming attributes from a part.
      Parameters:
      part - the part
      Returns:
      the same value when nothing was stamped, else a copy without the streaming attributes