fix(streaming): collect chunks then stream with explicit [DONE]
Some checks failed
CI / Check (push) Has been cancelled
CI / Clippy (push) Has been cancelled
CI / Formatting (push) Has been cancelled
CI / Test (push) Has been cancelled
CI / Release Build (push) Has been cancelled

This commit is contained in:
2026-03-03 13:52:18 -05:00
parent 656a6f31ce
commit 07f91ec4bb

View File

@@ -242,22 +242,22 @@ async fn chat_completions(
}, },
); );
// Create SSE stream from aggregating stream // Create streaming response - collect all chunks first, then stream with [DONE]
let stream_id = format!("chatcmpl-{}", Uuid::new_v4()); let stream_id = format!("chatcmpl-{}", Uuid::new_v4());
let stream_created = chrono::Utc::now().timestamp() as u64; let stream_created = chrono::Utc::now().timestamp() as u64;
let stream_id_clone = stream_id.clone();
// Map chunks to SSE events - clone stream_id for the async block // Collect all chunks from the aggregator
let stream_id_for_sse = stream_id.clone(); let chunks: Vec<Result<crate::providers::ProviderStreamChunk, crate::errors::AppError>> =
aggregating_stream.collect().await;
// Use async stream macro to ensure proper sequencing // Create a stream that yields chunks then [DONE]
let final_stream = async_stream::stream! { let final_stream = futures::stream::iter(chunks)
// First, process and yield all chunks from aggregator
let mut stream = Box::pin(aggregating_stream
.map(move |chunk_result| { .map(move |chunk_result| {
match chunk_result { match chunk_result {
Ok(chunk) => { Ok(chunk) => {
let response = ChatCompletionStreamResponse { let response = ChatCompletionStreamResponse {
id: stream_id_for_sse.clone(), id: stream_id_clone.clone(),
object: "chat.completion.chunk".to_string(), object: "chat.completion.chunk".to_string(),
created: stream_created, created: stream_created,
model: chunk.model.clone(), model: chunk.model.clone(),
@@ -273,21 +273,15 @@ async fn chat_completions(
}], }],
}; };
Event::default().json_data(response) Event::default().json_data(response)
.map_err(|e| AppError::InternalError(format!("SSE error: {}", e))) .map_err(|e| crate::errors::AppError::InternalError(format!("SSE error: {}", e)))
} }
Err(e) => Err(e), Err(e) => Err(e),
} }
})
.chain(futures::stream::once(async {
Ok::<Event, crate::errors::AppError>(Event::default().data("[DONE]"))
})); }));
// Yield all chunks
while let Some(item) = stream.next().await {
yield item;
}
// Finally yield [DONE]
yield Ok::<Event, AppError>(Event::default().data("[DONE]"));
};
Ok(Sse::new(final_stream).into_response()) Ok(Sse::new(final_stream).into_response())
} }
Err(e) => { Err(e) => {