pub trait StreamChatLikeExt: SseStreamable + HttpClient {
// Provided methods
fn stream_for_each<'a, F, Fut>(
&'a mut self,
on_chunk: F,
) -> impl Future<Output = Result<()>> + 'a
where F: FnMut(ChatStreamResponse) -> Fut + 'a,
Fut: Future<Output = Result<()>> + 'a { ... }
fn to_stream<'a>(
&'a mut self,
) -> impl Future<Output = Result<Pin<Box<dyn Stream<Item = Result<ChatStreamResponse>> + Send + 'static>>>> + 'a { ... }
}Expand description
Streaming extension trait for chat-like endpoints.
This trait provides two complementary APIs for processing streaming responses:
- Callback-based - Simple async closure interface
- Stream-based - Composable stream interface for advanced usage
Both APIs handle SSE protocol parsing, JSON deserialization, and error propagation.
Provided Methods§
Sourcefn stream_for_each<'a, F, Fut>(
&'a mut self,
on_chunk: F,
) -> impl Future<Output = Result<()>> + 'a
fn stream_for_each<'a, F, Fut>( &'a mut self, on_chunk: F, ) -> impl Future<Output = Result<()>> + 'a
Processes streaming responses using an async callback function.
This method provides a simple interface for handling streaming chat responses. Each successfully parsed chunk is passed to the provided callback function.
§Arguments
on_chunk- Async callback function that processes eachChatStreamResponsechunk
§Returns
Result indicating success or failure of the streaming operation
§Example
client.stream_for_each(|chunk| async move {
if let Some(content) = &chunk.choices[0].delta.content {
print!("{}", content);
}
Ok(())
}).await?;Sourcefn to_stream<'a>(
&'a mut self,
) -> impl Future<Output = Result<Pin<Box<dyn Stream<Item = Result<ChatStreamResponse>> + Send + 'static>>>> + 'a
fn to_stream<'a>( &'a mut self, ) -> impl Future<Output = Result<Pin<Box<dyn Stream<Item = Result<ChatStreamResponse>> + Send + 'static>>>> + 'a
Converts the streaming response into a composable Stream.
This method returns a Stream that yields ChatStreamResponse chunks,
enabling advanced stream processing operations like filtering, mapping,
and combination with other streams.
§Returns
A future that resolves to a Stream of Result<ChatStreamResponse> items
§Example
let stream = client.to_stream().await?;
let collected: Vec<_> = stream
.filter_map(|result| result.ok())
.collect()
.await;Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.
Implementors§
impl<N, M> StreamChatLikeExt for AsyncChatCompletion<N, M, StreamOn>
impl<N, M> StreamChatLikeExt for ChatCompletion<N, M, StreamOn>
Provides streaming extension methods for streaming-enabled chat completions.
This implementation enables the use of streaming-specific methods for processing chat responses in real-time.