Skip to content

Add streaming support for Responses API #405

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 28 additions & 2 deletions async-openai/src/responses.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
use crate::{
config::Config,
error::OpenAIError,
types::responses::{CreateResponse, Response},
types::responses::{CreateResponse, Response, ResponseStream},
Client,
};

/// Given text input or a list of context items, the model will generate a response.
///
/// Related guide: [Responses API](https://platform.openai.com/docs/guides/responses)
/// Related guide: [Responses](https://platform.openai.com/docs/api-reference/responses)
pub struct Responses<'c, C: Config> {
client: &'c Client<C>,
}
Expand All @@ -26,4 +26,30 @@ impl<'c, C: Config> Responses<'c, C> {
pub async fn create(&self, request: CreateResponse) -> Result<Response, OpenAIError> {
self.client.post("/responses", request).await
}

/// Creates a model response for the given input with streaming.
///
/// Response events will be sent as server-sent events as they become available,
#[crate::byot(
T0 = serde::Serialize,
R = serde::de::DeserializeOwned,
stream = "true",
where_clause = "R: std::marker::Send + 'static"
)]
#[allow(unused_mut)]
pub async fn create_stream(
&self,
mut request: CreateResponse,
) -> Result<ResponseStream, OpenAIError> {
#[cfg(not(feature = "byot"))]
{
if matches!(request.stream, Some(false)) {
return Err(OpenAIError::InvalidArgument(
"When stream is false, use Responses::create".into(),
));
}
request.stream = Some(true);
}
Ok(self.client.post_stream("/responses", request).await)
}
}
Loading