Skip to main content

skip_deserialization_errors

Function skip_deserialization_errors 

Source
pub fn skip_deserialization_errors<S, T>(
    stream: S,
) -> impl Stream<Item = Result<T, OapiError>> + Send + Unpin
where S: Stream<Item = Result<T, OapiError>> + Send + Unpin, T: Send,
Expand description

Drops the items of a parsed stream that failed to deserialize.

Chat providers occasionally emit chunks your serde model cannot parse — an unknown field shape, an intermediate event, a provider quirk. By default such a chunk surfaces as OapiError::DeserializationError and a consumer propagating it with ? abandons the rest of the stream. Wrapping the stream with this adapter filters those items out; every other error (transport failures, OapiError::SseParseError, …) is still yielded, and the stream still ends after the data: [DONE] sentinel.

§Example

use futures_util::StreamExt;
use openai_interface::chat::create::request::{Message, RequestBody};
use openai_interface::rest::{default_client, post::PostStream, RequestOptions, skip_deserialization_errors};

let request = RequestBody {
    messages: vec![Message::User {
        content: "What's your name?".into(),
        name: None,
    }],
    model: "deepseek-chat".to_string(),
    stream: Some(true),
    ..Default::default()
};

let stream = request
    .get_stream_response(&default_client(), "https://api.deepseek.com", &RequestOptions::bearer(api_key))
    .await?;
let mut stream = skip_deserialization_errors(stream);

while let Some(chunk) = stream.next().await {
    println!("{chunk:?}");
}