Skip to content
Open
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
13 changes: 10 additions & 3 deletions async-openai/src/client.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::pin::Pin;
use std::{pin::Pin, time::Duration};

use bytes::Bytes;
use futures::{stream::StreamExt, Stream};
Expand Down Expand Up @@ -360,6 +360,13 @@ impl<C: Config> Client<C> {
.map_err(OpenAIError::Reqwest)
.map_err(backoff::Error::Permanent)?;

let retry_after = response
.headers()
.get("retry-after")
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.and_then(|h| h.to_str().ok())
.and_then(|s| s.parse::<u64>().ok())
.map(Duration::from_secs);

let status = response.status();

match read_response(response).await {
Expand All @@ -370,7 +377,7 @@ impl<C: Config> Client<C> {
if status.is_server_error() {
Err(backoff::Error::Transient {
err: OpenAIError::ApiError(api_error),
retry_after: None,
retry_after,
})
} else if status.as_u16() == 429
&& api_error.r#type != Some("insufficient_quota".to_string())
Expand All @@ -379,7 +386,7 @@ impl<C: Config> Client<C> {
tracing::warn!("Rate limited: {}", api_error.message);
Err(backoff::Error::Transient {
err: OpenAIError::ApiError(api_error),
retry_after: None,
retry_after,
})
} else {
Err(backoff::Error::Permanent(OpenAIError::ApiError(api_error)))
Expand Down
Loading