Skip to content

Commit 2f77716

Browse files
authored
Backoff when OpenAI returns 5xx (#354)
1 parent 4c83fa4 commit 2f77716

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

async-openai/src/client.rs

+11-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use serde::{de::DeserializeOwned, Serialize};
88

99
use crate::{
1010
config::{Config, OpenAIConfig},
11-
error::{map_deserialization_error, OpenAIError, WrappedError},
11+
error::{map_deserialization_error, ApiError, OpenAIError, WrappedError},
1212
file::Files,
1313
image::Images,
1414
moderation::Moderations,
@@ -336,6 +336,16 @@ impl<C: Config> Client<C> {
336336
.map_err(OpenAIError::Reqwest)
337337
.map_err(backoff::Error::Permanent)?;
338338

339+
if status.is_server_error() {
340+
// OpenAI does not guarantee server errors are returned as JSON so we cannot deserialize them.
341+
let message: String = String::from_utf8_lossy(&bytes).into_owned();
342+
tracing::warn!("Server error: {status} - {message}");
343+
return Err(backoff::Error::Transient {
344+
err: OpenAIError::ApiError(ApiError { message, r#type: None, param: None, code: None }),
345+
retry_after: None,
346+
});
347+
}
348+
339349
// Deserialize response body from either error object or actual response object
340350
if !status.is_success() {
341351
let wrapped_error: WrappedError = serde_json::from_slice(bytes.as_ref())

0 commit comments

Comments
 (0)