Class StreamingParts
java.lang.Object
org.springframework.ai.chat.messages.part.StreamingParts
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 is 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. - A
ToolCallPartstamped partial is a tool-call argument delta, streamed so that a consumer can show the call while the model is still writing it. It carries the id, type and name of its call and only the arguments fragment of its chunk. It is not a tool call:AssistantMessage.getToolCalls()skips it, the aggregator does not accumulate it and the chat models do not send it back to their API. The chat model emits the completeToolCallPartlater in the same response, at the same index, so a stream that is cancelled or fails before then has no tool call. - Parts without an index are aggregated the pre-streaming way: text is concatenated, tool calls are appended, except for argument deltas, other parts are carried in arrival order.
- The chat model sets
ChatResponseMetadata.idon 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 parts of a chunk are only meant for the consumers of the stream. To continue the conversation, send back the aggregated message rather than a message built from the parts of the chunks: the API of the chat model may reject a request holding a delta, for instance a signature delta without its reasoning text, and a text delta is replayed as the fragment it holds.
- Since:
- 2.1.0
- Author:
- Christian Tzolov
-
Field Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptionstatic MessagePartcomplete(MessagePart part, int index) Marks a part as the complete content block at the given index.static booleanisPartial(MessagePart part) Whether a streamed part is a delta.static MessagePartpartial(MessagePart part, int index) Marks a part as a delta of the content block at the given index.static @Nullable IntegerpartIndex(MessagePart part) The content block index of a streamed part.static MessagePartstrip(MessagePart part) Removes the streaming attributes from a part.
-
Field Details
-
PART_INDEX_ATTRIBUTE
Attribute holding the zero-based content block index of a streamed part.- See Also:
-
PARTIAL_ATTRIBUTE
Attribute set totrueon a streamed part that is a delta.- See Also:
-
-
Method Details
-
partial
Marks a part as a delta of the content block at the given index.- Parameters:
part- the partindex- the content block index- Returns:
- a copy of the part with the streaming attributes set
-
complete
Marks a part as the complete content block at the given index.- Parameters:
part- the partindex- the content block index- Returns:
- a copy of the part with the index set and no partial flag
-
partIndex
The content block index of a streamed part.- Parameters:
part- the part- Returns:
- the index, or
nullwhen absent or malformed
-
isPartial
Whether a streamed part is a delta.- Parameters:
part- the part- Returns:
trueonly when the partial attribute is"true"
-
strip
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
-