Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1157,7 +1157,7 @@ export class OpenAI {
const headers = buildHeaders([
idempotencyHeaders,
{
Accept: 'application/json',
Accept: options.stream ? 'text/event-stream' : 'application/json',
'User-Agent': this.getUserAgent(),
'X-Stainless-Retry-Count': String(retryCount),
...(options.timeout ? { 'X-Stainless-Timeout': String(Math.trunc(options.timeout / 1000)) } : {}),
Expand Down
20 changes: 20 additions & 0 deletions tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,26 @@ describe('instantiate client', () => {
adminAPIKey: 'My Admin API Key',
});

test('uses an event-stream accept header for streaming requests', async () => {
const { req } = await client.buildRequest({ path: '/foo', method: 'post', stream: true });
expect(req.headers.get('accept')).toEqual('text/event-stream');
});

test('uses a JSON accept header for non-streaming requests', async () => {
const { req } = await client.buildRequest({ path: '/foo', method: 'post', stream: false });
expect(req.headers.get('accept')).toEqual('application/json');
});

test('preserves an explicit accept header for streaming requests', async () => {
const { req } = await client.buildRequest({
path: '/foo',
method: 'post',
stream: true,
headers: { Accept: 'application/x-ndjson' },
});
expect(req.headers.get('accept')).toEqual('application/x-ndjson');
});

test('they are used in the request', async () => {
const { req } = await client.buildRequest({ path: '/foo', method: 'post' });
expect(req.headers.get('x-my-default-header')).toEqual('2');
Expand Down