From b42a5cf59a03459a698bd5fe4e92616d935187f7 Mon Sep 17 00:00:00 2001 From: Sarp Tan Doven <119648987+sarptandoven@users.noreply.github.com> Date: Mon, 6 Jul 2026 14:22:09 -0700 Subject: [PATCH] fix: set event-stream accept header Use text/event-stream as the default Accept header when request options enable streaming while preserving explicit Accept overrides. Add regression coverage for streaming, non-streaming, and override cases. --- src/client.ts | 2 +- tests/index.test.ts | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/src/client.ts b/src/client.ts index c7df965cd..ed713683b 100644 --- a/src/client.ts +++ b/src/client.ts @@ -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)) } : {}), diff --git a/tests/index.test.ts b/tests/index.test.ts index e19e33b98..a83a59269 100644 --- a/tests/index.test.ts +++ b/tests/index.test.ts @@ -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');