Skip to content

Commit 75e6bf7

Browse files
Merge pull request #7 from spiceai/jeadie/25-01-21/404
Gracefully handle 404s; allow access to GET operation for non-openai models.
2 parents b9068b2 + 18a4472 commit 75e6bf7

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

async-openai/src/client.rs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ impl<C: Config> Client<C> {
160160
}
161161

162162
/// Make a GET request to {path} and deserialize the response body
163-
pub(crate) async fn get<O>(&self, path: &str) -> Result<O, OpenAIError>
163+
pub async fn get<O>(&self, path: &str) -> Result<O, OpenAIError>
164164
where
165165
O: DeserializeOwned,
166166
{
@@ -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)