Skip to content

Commit

Permalink
feat: retry for openai methods
Browse files Browse the repository at this point in the history
  • Loading branch information
csgulati09 committed Jan 17, 2025
1 parent 620e3d3 commit c80e471
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/baseClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ export abstract class ApiClient {
return APIError.generate(status, errorResponse, message, headers);
}

private shouldRetry(response: Response): boolean {
_shouldRetry(response: Response): boolean {
const retryStatusCode = response.status;
const retryTraceId = response.headers.get('x-portkey-trace-id');
const retryRequestId = response.headers.get('x-portkey-request-id');
Expand Down Expand Up @@ -306,7 +306,7 @@ export abstract class ApiClient {
}
this.responseHeaders = createResponseHeaders(response.headers);
if (!response.ok) {
if (retryCount < this.maxRetries && this.shouldRetry(response)) {
if (retryCount < this.maxRetries && this._shouldRetry(response)) {
return this.request(opts, retryCount + 1);
}
const errText = await response.text().catch(() => 'Unknown');
Expand Down
9 changes: 9 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,15 @@ export function initOpenAIClient(client: Portkey) {
baseURL: client.baseURL,
defaultHeaders: defaultHeadersBuilder(client),
maxRetries: 0,
fetch: async (url: RequestInfo, init?: RequestInit): Promise<Response> => {
const response = await fetch(url, init);
if (!response.ok && client._shouldRetry(response)) {
const response = await fetch(url, init);
return response;
} else {
return response;
}
},
});
}
export function toQueryParams(
Expand Down

0 comments on commit c80e471

Please sign in to comment.