Skip to content

Commit 76a11f8

Browse files
committed
handle 404s gracefully
1 parent b9068b2 commit 76a11f8

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

async-openai/src/client.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -316,13 +316,22 @@ impl<C: Config> Client<C> {
316316

317317
backoff::future::retry(self.backoff.clone(), || async {
318318
let request = request_maker().await.map_err(backoff::Error::Permanent)?;
319-
let response = client
319+
let mut response = client
320320
.execute(request)
321321
.await
322322
.map_err(OpenAIError::Reqwest)
323323
.map_err(backoff::Error::Permanent)?;
324324

325325
let status = response.status();
326+
327+
// Handle bad base URLs
328+
if status.as_u16() == 404 {
329+
response = response
330+
.error_for_status()
331+
.map_err(OpenAIError::Reqwest)
332+
.map_err(backoff::Error::Permanent)?;
333+
};
334+
326335
let bytes = response
327336
.bytes()
328337
.await
@@ -378,7 +387,7 @@ impl<C: Config> Client<C> {
378387
}
379388

380389
/// Make HTTP POST request to receive SSE
381-
pub async fn post_stream<I, O, E >(
390+
pub async fn post_stream<I, O, E>(
382391
&self,
383392
path: &str,
384393
request: I,
@@ -479,7 +488,7 @@ where
479488
String::from_utf8_lossy(message.data.as_bytes()),
480489
);
481490
Err(Into::<E>::into(e))
482-
},
491+
}
483492
Ok(output) => Ok(output),
484493
};
485494

0 commit comments

Comments
 (0)