pub struct ChatCompletionAccumulator { /* private fields */ }Expand description
Assembles streamed ChatCompletionChunks into complete messages.
All choices are accumulated, keyed by their chunk index; the accessors
and ChatCompletionAccumulator::into_message default to choice 0
(the only choice unless the request set n > 1).
Fragment semantics follow the official SDKs: content / refusal
/ tool call arguments concatenate; role, tool call id / type /
name and function_call.name are overwritten whenever a fragment
carries them; finish_reason and usage are kept from the last chunk
that provided them.
Implementations§
Source§impl ChatCompletionAccumulator
impl ChatCompletionAccumulator
Sourcepub fn push(&mut self, chunk: &ChatCompletionChunk)
pub fn push(&mut self, chunk: &ChatCompletionChunk)
Merges one chunk into the accumulated state.
Sourcepub fn content(&self) -> &str
pub fn content(&self) -> &str
The accumulated text content of choice 0, empty before any content
chunk arrives.
Sourcepub fn reasoning_content(&self) -> &str
pub fn reasoning_content(&self) -> &str
The accumulated reasoning_content of choice 0 (thinking models).
Sourcepub fn reasoning(&self) -> &str
pub fn reasoning(&self) -> &str
The accumulated reasoning of choice 0.
vLLM streams the chain of thought under reasoning instead of
reasoning_content, so against a vLLM backend
Self::reasoning_content stays empty and this accessor carries
the accumulated thought.
Sourcepub fn finish_reason(&self) -> Option<&FinishReason>
pub fn finish_reason(&self) -> Option<&FinishReason>
The finish_reason of choice 0, once a chunk carries it.
Sourcepub fn usage(&self) -> Option<&CompletionUsage>
pub fn usage(&self) -> Option<&CompletionUsage>
The token usage, once a chunk carries it (the final chunk when the
request set stream_options: {"include_usage": true}).
Sourcepub fn function_call(&self) -> Option<&AccumulatedFunctionCall>
pub fn function_call(&self) -> Option<&AccumulatedFunctionCall>
The deprecated function_call of choice 0, assembled from its
fragments, if the model used the legacy path.
Sourcepub fn into_message(self) -> ChatCompletionMessage
pub fn into_message(self) -> ChatCompletionMessage
Consumes the accumulator, producing the assembled message of choice
0. An accumulator that never saw a choice-0 chunk yields an
empty assistant message.
Sourcepub fn into_messages(self) -> BTreeMap<u32, ChatCompletionMessage>
pub fn into_messages(self) -> BTreeMap<u32, ChatCompletionMessage>
Consumes the accumulator, producing every assembled message keyed by its choice index.