ChatCompletionChunk

Struct ChatCompletionChunk 

Source
pub struct ChatCompletionChunk { /* private fields */ }
Expand description

A streaming chunk from a chat completion response.

Each chunk represents a delta update from the model as it generates the response.

Implementations§

Source§

impl ChatCompletionChunk

Source

pub fn new(response: CreateChatCompletionStreamResponse) -> Self

Create a new chunk from a stream response.

Source

pub fn content(&self) -> Option<&str>

Get the content delta from this chunk, if any.

Returns the text content that was generated in this chunk.

Examples found in repository?
examples/chat_streaming.rs (line 59)
49async fn basic_streaming(client: &Client) -> Result<()> {
50    println!("Question: Tell me a short joke");
51
52    let builder = client.chat().user("Tell me a short joke");
53
54    let mut stream = client.send_chat_stream(builder).await?;
55
56    print!("Response: ");
57    while let Some(chunk) = stream.next().await {
58        let chunk = chunk?;
59        if let Some(content) = chunk.content() {
60            print!("{}", content);
61        }
62    }
63    println!();
64
65    Ok(())
66}
67
68async fn streaming_with_parameters(client: &Client) -> Result<()> {
69    println!("Question: Write a creative tagline for a bakery");
70
71    let builder = client
72        .chat()
73        .user("Write a creative tagline for a bakery")
74        .temperature(0.9)
75        .max_tokens(50);
76
77    let mut stream = client.send_chat_stream(builder).await?;
78
79    print!("Response: ");
80    while let Some(chunk) = stream.next().await {
81        let chunk = chunk?;
82        if let Some(content) = chunk.content() {
83            print!("{}", content);
84        }
85    }
86    println!();
87
88    Ok(())
89}
90
91async fn collect_content(client: &Client) -> Result<()> {
92    println!("Question: What is the capital of France?");
93
94    let builder = client.chat().user("What is the capital of France?");
95
96    let mut stream = client.send_chat_stream(builder).await?;
97
98    // Manually collect all content
99    let mut content = String::new();
100    while let Some(chunk) = stream.next().await {
101        let chunk = chunk?;
102        if let Some(text) = chunk.content() {
103            content.push_str(text);
104        }
105    }
106    println!("Full response: {}", content);
107
108    Ok(())
109}
110
111async fn streaming_with_system(client: &Client) -> Result<()> {
112    println!("System: You are a helpful assistant that speaks like a pirate");
113    println!("Question: Tell me about the weather");
114
115    let builder = client
116        .chat()
117        .system("You are a helpful assistant that speaks like a pirate")
118        .user("Tell me about the weather")
119        .max_tokens(100);
120
121    let mut stream = client.send_chat_stream(builder).await?;
122
123    print!("Response: ");
124    while let Some(chunk) = stream.next().await {
125        let chunk = chunk?;
126        if let Some(content) = chunk.content() {
127            print!("{}", content);
128        }
129    }
130    println!();
131
132    Ok(())
133}
134
135async fn multiple_turns(client: &Client) -> Result<()> {
136    println!("Building a conversation with multiple turns...\n");
137
138    // First turn
139    println!("User: What is 2+2?");
140    let builder = client.chat().user("What is 2+2?");
141
142    let mut stream = client.send_chat_stream(builder).await?;
143
144    print!("Assistant: ");
145    let mut first_response = String::new();
146    while let Some(chunk) = stream.next().await {
147        let chunk = chunk?;
148        if let Some(content) = chunk.content() {
149            print!("{}", content);
150            first_response.push_str(content);
151        }
152    }
153    println!();
154
155    // Second turn - continuing the conversation
156    println!("\nUser: Now multiply that by 3");
157    let builder = client
158        .chat()
159        .user("What is 2+2?")
160        .assistant(&first_response)
161        .user("Now multiply that by 3");
162
163    let mut stream = client.send_chat_stream(builder).await?;
164
165    print!("Assistant: ");
166    while let Some(chunk) = stream.next().await {
167        let chunk = chunk?;
168        if let Some(content) = chunk.content() {
169            print!("{}", content);
170        }
171    }
172    println!();
173
174    Ok(())
175}
More examples
Hide additional examples
examples/langfuse_streaming.rs (line 105)
94async fn basic_streaming(client: &Client<LangfuseState<Span>>) -> Result<()> {
95    println!("Question: Tell me a short joke");
96
97    let builder = client.chat().user("Tell me a short joke");
98
99    let mut stream = client.send_chat_stream(builder).await?;
100
101    print!("Response: ");
102    let mut chunk_count = 0;
103    while let Some(chunk) = stream.next().await {
104        let chunk = chunk?;
105        if let Some(content) = chunk.content() {
106            print!("{}", content);
107            chunk_count += 1;
108        }
109    }
110    println!(
111        "\n(Received {} chunks, all traced to Langfuse)",
112        chunk_count
113    );
114
115    Ok(())
116}
117
118async fn streaming_with_parameters(client: &Client<LangfuseState<Span>>) -> Result<()> {
119    println!("Question: Write a creative tagline for a bakery");
120
121    let builder = client
122        .chat()
123        .user("Write a creative tagline for a bakery")
124        .temperature(0.9)
125        .max_tokens(50);
126
127    let mut stream = client.send_chat_stream(builder).await?;
128
129    print!("Response: ");
130    let mut chunk_count = 0;
131    while let Some(chunk) = stream.next().await {
132        let chunk = chunk?;
133        if let Some(content) = chunk.content() {
134            print!("{}", content);
135            chunk_count += 1;
136        }
137    }
138    println!(
139        "\n(Received {} chunks, all traced to Langfuse)",
140        chunk_count
141    );
142
143    Ok(())
144}
145
146async fn collect_content(client: &Client<LangfuseState<Span>>) -> Result<()> {
147    println!("Question: What is the capital of France?");
148
149    let builder = client.chat().user("What is the capital of France?");
150
151    let mut stream = client.send_chat_stream(builder).await?;
152
153    // Manually collect content (interceptor hooks are still called for each chunk)
154    let mut content = String::new();
155    while let Some(chunk) = stream.next().await {
156        let chunk = chunk?;
157        if let Some(text) = chunk.content() {
158            content.push_str(text);
159        }
160    }
161    println!("Full response: {}", content);
162    println!("(All chunks were traced to Langfuse during collection)");
163
164    Ok(())
165}
Source

pub fn role(&self) -> Option<&str>

Get the role from this chunk, if any.

This is typically only present in the first chunk.

Source

pub fn tool_calls(&self) -> Option<&Vec<ChatCompletionMessageToolCallChunk>>

Get tool calls from this chunk, if any.

Source

pub fn finish_reason(&self) -> Option<&str>

Get the finish reason, if any.

This indicates why the generation stopped and is only present in the last chunk.

Source

pub fn is_final(&self) -> bool

Check if this is the last chunk in the stream.

Source

pub fn raw_response(&self) -> &CreateChatCompletionStreamResponse

Get the underlying raw response.

Source

pub fn delta(&self) -> Option<&ChatCompletionStreamResponseDelta>

Get the delta object directly.

Trait Implementations§

Source§

impl Clone for ChatCompletionChunk

Source§

fn clone(&self) -> ChatCompletionChunk

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for ChatCompletionChunk

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

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> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> FutureExt for T

Source§

fn with_context(self, otel_cx: Context) -> WithContext<Self>

Attaches the provided Context to this type, returning a WithContext wrapper. Read more
Source§

fn with_current_context(self) -> WithContext<Self>

Attaches the current Context to this type, returning a WithContext wrapper. Read more
Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> ErasedDestructor for T
where T: 'static,