Skip to content

Commit 266717b

Browse files
stainless-app[bot]stainless-bot
authored andcommitted
fix(client): respect x-stainless-retry-count default headers (#1138)
1 parent 6ae19ce commit 266717b

File tree

2 files changed

+40
-3
lines changed

2 files changed

+40
-3
lines changed

src/core.ts

+7-3
Original file line numberDiff line numberDiff line change
@@ -386,9 +386,13 @@ export abstract class APIClient {
386386
delete reqHeaders['content-type'];
387387
}
388388

389-
// Don't set the retry count header if it was already set or removed by the caller. We check `headers`,
390-
// which can contain nulls, instead of `reqHeaders` to account for the removal case.
391-
if (getHeader(headers, 'x-stainless-retry-count') === undefined) {
389+
// Don't set the retry count header if it was already set or removed through default headers or by the
390+
// caller. We check `defaultHeaders` and `headers`, which can contain nulls, instead of `reqHeaders` to
391+
// account for the removal case.
392+
if (
393+
getHeader(defaultHeaders, 'x-stainless-retry-count') === undefined &&
394+
getHeader(headers, 'x-stainless-retry-count') === undefined
395+
) {
392396
reqHeaders['x-stainless-retry-count'] = String(retryCount);
393397
}
394398

tests/index.test.ts

+33
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,39 @@ describe('retries', () => {
295295
expect(capturedRequest!.headers as Headers).not.toHaveProperty('x-stainless-retry-count');
296296
});
297297

298+
test('omit retry count header by default', async () => {
299+
let count = 0;
300+
let capturedRequest: RequestInit | undefined;
301+
const testFetch = async (url: RequestInfo, init: RequestInit = {}): Promise<Response> => {
302+
count++;
303+
if (count <= 2) {
304+
return new Response(undefined, {
305+
status: 429,
306+
headers: {
307+
'Retry-After': '0.1',
308+
},
309+
});
310+
}
311+
capturedRequest = init;
312+
return new Response(JSON.stringify({ a: 1 }), { headers: { 'Content-Type': 'application/json' } });
313+
};
314+
const client = new OpenAI({
315+
apiKey: 'My API Key',
316+
fetch: testFetch,
317+
maxRetries: 4,
318+
defaultHeaders: { 'X-Stainless-Retry-Count': null },
319+
});
320+
321+
expect(
322+
await client.request({
323+
path: '/foo',
324+
method: 'get',
325+
}),
326+
).toEqual({ a: 1 });
327+
328+
expect(capturedRequest!.headers as Headers).not.toHaveProperty('x-stainless-retry-count');
329+
});
330+
298331
test('overwrite retry count header', async () => {
299332
let count = 0;
300333
let capturedRequest: RequestInit | undefined;

0 commit comments

Comments
 (0)