Skip to content

feat: Add responses API #373

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

Merged
merged 2 commits into from
Jun 2, 2025
Merged
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
14 changes: 12 additions & 2 deletions async-openai/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use crate::{
moderation::Moderations,
traits::AsyncTryFrom,
Assistants, Audio, AuditLogs, Batches, Chat, Completions, Embeddings, FineTuning, Invites,
Models, Projects, Threads, Uploads, Users, VectorStores,
Models, Projects, Responses, Threads, Uploads, Users, VectorStores,
};

#[derive(Debug, Clone, Default)]
Expand Down Expand Up @@ -162,6 +162,11 @@ impl<C: Config> Client<C> {
Projects::new(self)
}

/// To call [Responses] group related APIs using this client.
pub fn responses(&self) -> Responses<C> {
Responses::new(self)
}

pub fn config(&self) -> &C {
&self.config
}
Expand Down Expand Up @@ -341,7 +346,12 @@ impl<C: Config> Client<C> {
let message: String = String::from_utf8_lossy(&bytes).into_owned();
tracing::warn!("Server error: {status} - {message}");
return Err(backoff::Error::Transient {
err: OpenAIError::ApiError(ApiError { message, r#type: None, param: None, code: None }),
err: OpenAIError::ApiError(ApiError {
message,
r#type: None,
param: None,
code: None,
}),
retry_after: None,
});
}
Expand Down
2 changes: 2 additions & 0 deletions async-openai/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ mod project_api_keys;
mod project_service_accounts;
mod project_users;
mod projects;
mod responses;
mod runs;
mod steps;
mod threads;
Expand Down Expand Up @@ -177,6 +178,7 @@ pub use project_api_keys::ProjectAPIKeys;
pub use project_service_accounts::ProjectServiceAccounts;
pub use project_users::ProjectUsers;
pub use projects::Projects;
pub use responses::Responses;
pub use runs::Runs;
pub use steps::Steps;
pub use threads::Threads;
Expand Down
29 changes: 29 additions & 0 deletions async-openai/src/responses.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
use crate::{
config::Config,
error::OpenAIError,
types::responses::{CreateResponse, Response},
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)
pub struct Responses<'c, C: Config> {
client: &'c Client<C>,
}

impl<'c, C: Config> Responses<'c, C> {
/// Constructs a new Responses client.
pub fn new(client: &'c Client<C>) -> Self {
Self { client }
}

/// Creates a model response for the given input.
#[crate::byot(
T0 = serde::Serialize,
R = serde::de::DeserializeOwned
)]
pub async fn create(&self, request: CreateResponse) -> Result<Response, OpenAIError> {
self.client.post("/responses", request).await
}
}
49 changes: 49 additions & 0 deletions async-openai/src/types/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use crate::{
use bytes::Bytes;

use super::{
responses::{CodeInterpreterContainer, Input, InputContent, Role as ResponsesRole},
AddUploadPartRequest, AudioInput, AudioResponseFormat, ChatCompletionFunctionCall,
ChatCompletionFunctions, ChatCompletionNamedToolChoice, ChatCompletionRequestAssistantMessage,
ChatCompletionRequestAssistantMessageContent, ChatCompletionRequestDeveloperMessage,
Expand Down Expand Up @@ -987,3 +988,51 @@ impl AsyncTryFrom<AddUploadPartRequest> for reqwest::multipart::Form {
}

// end: types to multipart form

impl Default for Input {
fn default() -> Self {
Self::Text("".to_string())
}
}

impl Default for InputContent {
fn default() -> Self {
Self::TextInput("".to_string())
}
}

impl From<String> for Input {
fn from(value: String) -> Self {
Input::Text(value)
}
}

impl From<&str> for Input {
fn from(value: &str) -> Self {
Input::Text(value.to_owned())
}
}

impl Default for ResponsesRole {
fn default() -> Self {
Self::User
}
}

impl From<String> for InputContent {
fn from(value: String) -> Self {
Self::TextInput(value)
}
}

impl From<&str> for InputContent {
fn from(value: &str) -> Self {
Self::TextInput(value.to_owned())
}
}

impl Default for CodeInterpreterContainer {
fn default() -> Self {
CodeInterpreterContainer::Id("".to_string())
}
}
1 change: 1 addition & 0 deletions async-openai/src/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ mod projects;
#[cfg_attr(docsrs, doc(cfg(feature = "realtime")))]
#[cfg(feature = "realtime")]
pub mod realtime;
pub mod responses;
mod run;
mod step;
mod thread;
Expand Down
Loading