Skip to main content

Chunk

Enum Chunk 

Source
#[non_exhaustive]
pub enum Chunk { TextDelta(String), ToolCallStart { id: String, name: String, signature: Option<String>, }, ToolCallArgsDelta { id: String, args_json_delta: String, }, ToolCallEnd { id: String, }, Usage(Usage), Stop(StopReason), }
Expand description

A single event in a provider’s streaming response.

A complete stream is an ordered sequence of these. Text generation surfaces as a run of Chunk::TextDelta events; a tool call surfaces as exactly one Chunk::ToolCallStart, zero or more Chunk::ToolCallArgsDelta fragments (whose concatenated args_json_deltas form the call’s JSON arguments), and exactly one Chunk::ToolCallEnd. Every tool-call event carries the call id so concurrently-streamed calls can be demultiplexed. Chunk::Usage reports token accounting and may appear more than once. A well-formed stream ends with a single Chunk::Stop.

Variants (Non-exhaustive)§

This enum is marked as non-exhaustive
Non-exhaustive enums could have additional variants added in future. Therefore, when matching against variants of non-exhaustive enums, an extra wildcard arm must be added to account for any future variants.
§

TextDelta(String)

An incremental piece of generated text. Concatenate consecutive TextDeltas to recover the full text segment.

§

ToolCallStart

The model has begun a tool call. The id and name are known up front; arguments stream in as subsequent Chunk::ToolCallArgsDeltas bearing the same id.

Fields

§id: String

Provider-assigned call identifier, matching a future ToolCall::id.

§name: String

Name of the tool being called.

§signature: Option<String>

Opaque provider-specific signature for this call (e.g. a thinking model’s thought signature), to be carried onto the assembled ToolCall and echoed back on the follow-up request. None when the provider emits no such token.

§

ToolCallArgsDelta

An incremental fragment of a tool call’s JSON arguments. Concatenate the args_json_deltas of all fragments sharing an id to recover the full args_json. Do not parse until the matching Chunk::ToolCallEnd.

Fields

§id: String

Identifies which in-progress Chunk::ToolCallStart this fragment belongs to.

§args_json_delta: String

A partial slice of the call’s JSON-encoded arguments.

§

ToolCallEnd

The named tool call’s arguments are complete and may now be parsed.

Fields

§id: String

Identifies the completed Chunk::ToolCallStart.

§

Usage(Usage)

A token-accounting update. May arrive more than once per stream (e.g. input tokens early, output tokens at the end).

§

Stop(StopReason)

Terminal event: generation has finished for the reason given.

Implementations§

Source§

impl Chunk

Source

pub fn text_delta(s: impl Into<String>) -> Self

Wraps s in a Chunk::TextDelta.

Source

pub fn tool_call_start(id: impl Into<String>, name: impl Into<String>) -> Self

Constructs a Chunk::ToolCallStart event (no signature).

Source

pub fn tool_call_start_signed( id: impl Into<String>, name: impl Into<String>, signature: Option<String>, ) -> Self

Constructs a Chunk::ToolCallStart event carrying an opaque provider-specific signature.

Source

pub fn tool_call_args_delta( id: impl Into<String>, args_json_delta: impl Into<String>, ) -> Self

Constructs a Chunk::ToolCallArgsDelta carrying a partial-args fragment.

Source

pub fn tool_call_end(id: impl Into<String>) -> Self

Constructs a Chunk::ToolCallEnd event.

Trait Implementations§

Source§

impl Clone for Chunk

Source§

fn clone(&self) -> Chunk

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Chunk

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'de> Deserialize<'de> for Chunk

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Eq for Chunk

Source§

impl PartialEq for Chunk

Source§

fn eq(&self, other: &Chunk) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Serialize for Chunk

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl StructuralPartialEq for Chunk

Auto Trait Implementations§

§

impl Freeze for Chunk

§

impl RefUnwindSafe for Chunk

§

impl Send for Chunk

§

impl Sync for Chunk

§

impl Unpin for Chunk

§

impl UnsafeUnpin for Chunk

§

impl UnwindSafe for Chunk

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.