#[non_exhaustive]pub enum TextDelta {
Text(String),
Reasoning(String),
ToolCallStarted {
index: usize,
id: String,
name: String,
},
ToolCallArguments {
index: usize,
chunk: String,
},
ToolCallFinished {
index: usize,
call: ToolCall,
},
Usage {
input_tokens: u64,
output_tokens: u64,
cached_input_tokens: Option<u64>,
},
Finish {
reason: FinishReason,
model: String,
},
}Expand description
One incremental piece of a streamed completion (issue #859) — the item
type of TextModel::stream.
Semantics a caller (and an overriding adapter) can rely on:
Self::Usagecarries running totals; a stream may send several and the last one wins.Self::ToolCallStarted/Self::ToolCallArguments/Self::ToolCallFinishedcarry one tool call through its life byindex. The argumentchunks are incremental and only human-readable in sequence;Self::ToolCallFinishedcarries the fully reassembledToolCall— exactly whatcompletewould have returned inCompletion::tool_calls— so a caller who only cares about finished calls can ignore the first two.- Exactly one
Self::Finishends a successful stream; it is the last item. AnErritem also ends the stream (a failed completion has noFinish). - Dropping the stream cancels the completion: an overriding adapter
MUST abort its upstream exchange on drop, the contract
HttpClient::send_streamingcarries.
Serialize/Deserialize (adjacently tagged, so every variant round
trips) is what lets a route write deltas straight out as SSE or
NDJSON: {"type":"text","data":"…"}, {"type":"finish","data":{…}}.
Variants (Non-exhaustive)§
This enum is marked as non-exhaustive
Text(String)
A run of answer text, in stream order.
Reasoning(String)
A run of reasoning the model surfaced alongside its answer. Not
part of Completion::text; a caller shows it or drops it.
ToolCallStarted
The model asked to call a tool: the call’s index in the
completion’s tool-call order, its provider-assigned id, and the
tool name. The arguments have not arrived yet.
Fields
ToolCallArguments
One run of the arguments of the call at index. Chunks concatenate
to the call’s full arguments JSON; see Self::ToolCallFinished
for the reassembled form.
Fields
ToolCallFinished
The call at index is complete, arguments and all — the same
ToolCall complete would have returned.
Usage
Token usage so far. Running totals: a later
Self::Usage supersedes it entirely.
Fields
cached_input_tokens: Option<u64>The cached subset of input_tokens, where the provider
reports one — None keeps “no report” and “reported zero”
distinct, exactly as Completion::cached_input_tokens does.
Finish
The stream is over, successfully. Always the last item of a successful stream, and the only one.
Fields
reason: FinishReasonWhy the completion stopped.