Skip to content

Commit 5463b37

Browse files
committed
Add streaming support for Responses API.
1 parent 483c84f commit 5463b37

File tree

5 files changed

+984
-2
lines changed

5 files changed

+984
-2
lines changed

async-openai/src/responses.rs

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
use crate::{
22
config::Config,
33
error::OpenAIError,
4-
types::responses::{CreateResponse, Response},
4+
types::responses::{CreateResponse, Response, ResponseStream},
55
Client,
66
};
77

88
/// Given text input or a list of context items, the model will generate a response.
99
///
10-
/// Related guide: [Responses API](https://platform.openai.com/docs/guides/responses)
10+
/// Related guide: [Responses](https://platform.openai.com/docs/api-reference/responses)
1111
pub struct Responses<'c, C: Config> {
1212
client: &'c Client<C>,
1313
}
@@ -26,4 +26,33 @@ impl<'c, C: Config> Responses<'c, C> {
2626
pub async fn create(&self, request: CreateResponse) -> Result<Response, OpenAIError> {
2727
self.client.post("/responses", request).await
2828
}
29+
30+
/// Creates a model response for the given input with streaming.
31+
///
32+
/// Response events will be sent as server-sent events as they become available,
33+
/// with the stream terminated by a `data: [DONE]` message.
34+
///
35+
/// [ResponseStream] is a parsed SSE stream until a [DONE] is received from server.
36+
#[crate::byot(
37+
T0 = serde::Serialize,
38+
R = serde::de::DeserializeOwned,
39+
stream = "true",
40+
where_clause = "R: std::marker::Send + 'static"
41+
)]
42+
#[allow(unused_mut)]
43+
pub async fn create_stream(
44+
&self,
45+
mut request: CreateResponse,
46+
) -> Result<ResponseStream, OpenAIError> {
47+
#[cfg(not(feature = "byot"))]
48+
{
49+
if matches!(request.stream, Some(false)) {
50+
return Err(OpenAIError::InvalidArgument(
51+
"When stream is false, use Responses::create".into(),
52+
));
53+
}
54+
request.stream = Some(true);
55+
}
56+
Ok(self.client.post_stream("/responses", request).await)
57+
}
2958
}

0 commit comments

Comments
 (0)