Skip to content

Commit ef52b8d

Browse files
fix(client): respect x-stainless-retry-count default headers (#562)
1 parent cdc3c1d commit ef52b8d

File tree

2 files changed

+40
-3
lines changed

2 files changed

+40
-3
lines changed

src/core.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -365,9 +365,13 @@ export abstract class APIClient {
365365
delete reqHeaders['content-type'];
366366
}
367367

368-
// Don't set the retry count header if it was already set or removed by the caller. We check `headers`,
369-
// which can contain nulls, instead of `reqHeaders` to account for the removal case.
370-
if (getHeader(headers, 'x-stainless-retry-count') === undefined) {
368+
// Don't set the retry count header if it was already set or removed through default headers or by the
369+
// caller. We check `defaultHeaders` and `headers`, which can contain nulls, instead of `reqHeaders` to
370+
// account for the removal case.
371+
if (
372+
getHeader(defaultHeaders, 'x-stainless-retry-count') === undefined &&
373+
getHeader(headers, 'x-stainless-retry-count') === undefined
374+
) {
371375
reqHeaders['x-stainless-retry-count'] = String(retryCount);
372376
}
373377

tests/index.test.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,39 @@ describe('retries', () => {
301301
expect(capturedRequest!.headers as Headers).not.toHaveProperty('x-stainless-retry-count');
302302
});
303303

304+
test('omit retry count header by default', async () => {
305+
let count = 0;
306+
let capturedRequest: RequestInit | undefined;
307+
const testFetch = async (url: RequestInfo, init: RequestInit = {}): Promise<Response> => {
308+
count++;
309+
if (count <= 2) {
310+
return new Response(undefined, {
311+
status: 429,
312+
headers: {
313+
'Retry-After': '0.1',
314+
},
315+
});
316+
}
317+
capturedRequest = init;
318+
return new Response(JSON.stringify({ a: 1 }), { headers: { 'Content-Type': 'application/json' } });
319+
};
320+
const client = new Anthropic({
321+
apiKey: 'my-anthropic-api-key',
322+
fetch: testFetch,
323+
maxRetries: 4,
324+
defaultHeaders: { 'X-Stainless-Retry-Count': null },
325+
});
326+
327+
expect(
328+
await client.request({
329+
path: '/foo',
330+
method: 'get',
331+
}),
332+
).toEqual({ a: 1 });
333+
334+
expect(capturedRequest!.headers as Headers).not.toHaveProperty('x-stainless-retry-count');
335+
});
336+
304337
test('overwrite retry count header', async () => {
305338
let count = 0;
306339
let capturedRequest: RequestInit | undefined;

0 commit comments

Comments
 (0)