Skip to content

Commit 41ee009

Browse files
committed
fix(client): handle keepalive events in streaming responses
1 parent fef329e commit 41ee009

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

async-openai/src/client.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -737,6 +737,11 @@ pub(crate) async fn stream<O>(
737737
where
738738
O: DeserializeOwned + std::marker::Send + 'static,
739739
{
740+
#[derive(Debug, serde::Deserialize)]
741+
struct BaseStreamEvent {
742+
r#type: String,
743+
}
744+
740745
let (tx, rx) = tokio::sync::mpsc::unbounded_channel();
741746

742747
tokio::spawn(async move {
@@ -763,6 +768,12 @@ where
763768
break;
764769
}
765770

771+
if let Ok(event) = serde_json::from_str::<BaseStreamEvent>(&message.data) {
772+
if event.r#type == "keepalive" {
773+
continue;
774+
}
775+
}
776+
766777
let response = match serde_json::from_str::<O>(&message.data) {
767778
Err(e) => Err(map_deserialization_error(e, message.data.as_bytes())),
768779
Ok(output) => Ok(output),

0 commit comments

Comments
 (0)