Skip to content
Merged
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 .release-please-manifest.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
".": "5.8.3"
".": "5.8.4"
}
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
# Changelog

## 5.8.4 (2025-07-10)

Full Changelog: [v5.8.3...v5.8.4](https://github.com/openai/openai-node/compare/v5.8.3...v5.8.4)

### Chores

* **internal:** bump undici version in tests ([6f38b80](https://github.com/openai/openai-node/commit/6f38b809a69b6ab3fcbf1235e57bdb8912024ab3))
* make some internal functions async ([841940d](https://github.com/openai/openai-node/commit/841940d2ae036191b456e8398fbcb1f24c6e4deb))

## 5.8.3 (2025-07-08)

Full Changelog: [v5.8.2...v5.8.3](https://github.com/openai/openai-node/compare/v5.8.2...v5.8.3)
Expand Down
8 changes: 4 additions & 4 deletions ecosystem-tests/node-ts-cjs/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion ecosystem-tests/node-ts-cjs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"formdata-node": "^4.4.1",
"tsconfig-paths": "^4.0.0",
"typescript": "^5.7.3",
"undici": "^7.2.0"
"undici": "^7.10.0"
},
"devDependencies": {
"@types/node": "^20.14.8",
Expand Down
2 changes: 1 addition & 1 deletion jsr.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@openai/openai",
"version": "5.8.3",
"version": "5.8.4",
"exports": {
".": "./index.ts",
"./helpers/zod": "./helpers/zod.ts",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "openai",
"version": "5.8.3",
"version": "5.8.4",
"description": "The official TypeScript library for the OpenAI API",
"author": "OpenAI <[email protected]>",
"types": "dist/index.d.ts",
Expand Down
6 changes: 3 additions & 3 deletions src/azure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,10 @@ export class AzureOpenAI extends OpenAI {
this.deploymentName = deployment;
}

override buildRequest(
override async buildRequest(
options: FinalRequestOptions,
props: { retryCount?: number } = {},
): { req: RequestInit & { headers: Headers }; url: string; timeout: number } {
): Promise<{ req: RequestInit & { headers: Headers }; url: string; timeout: number }> {
if (_deployments_endpoints.has(options.path) && options.method === 'post' && options.body !== undefined) {
if (!isObj(options.body)) {
throw new Error('Expected request body to be an object');
Expand All @@ -154,7 +154,7 @@ export class AzureOpenAI extends OpenAI {
return undefined;
}

protected override authHeaders(opts: FinalRequestOptions): NullableHeaders | undefined {
protected override async authHeaders(opts: FinalRequestOptions): Promise<NullableHeaders | undefined> {
return;
}

Expand Down
25 changes: 14 additions & 11 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ export class OpenAI {
* Create a new client instance re-using the same options given to the current client with optional overriding.
*/
withOptions(options: Partial<ClientOptions>): this {
return new (this.constructor as any as new (props: ClientOptions) => typeof this)({
const client = new (this.constructor as any as new (props: ClientOptions) => typeof this)({
...this._options,
baseURL: this.baseURL,
maxRetries: this.maxRetries,
Expand All @@ -385,6 +385,7 @@ export class OpenAI {
webhookSecret: this.webhookSecret,
...options,
});
return client;
}

/**
Expand All @@ -402,7 +403,7 @@ export class OpenAI {
return;
}

protected authHeaders(opts: FinalRequestOptions): NullableHeaders | undefined {
protected async authHeaders(opts: FinalRequestOptions): Promise<NullableHeaders | undefined> {
return buildHeaders([{ Authorization: `Bearer ${this.apiKey}` }]);
}

Expand Down Expand Up @@ -518,7 +519,9 @@ export class OpenAI {

await this.prepareOptions(options);

const { req, url, timeout } = this.buildRequest(options, { retryCount: maxRetries - retriesRemaining });
const { req, url, timeout } = await this.buildRequest(options, {
retryCount: maxRetries - retriesRemaining,
});

await this.prepareRequest(req, { url, options });

Expand Down Expand Up @@ -600,7 +603,7 @@ export class OpenAI {
} with status ${response.status} in ${headersTime - startTime}ms`;

if (!response.ok) {
const shouldRetry = this.shouldRetry(response);
const shouldRetry = await this.shouldRetry(response);
if (retriesRemaining && shouldRetry) {
const retryMessage = `retrying, ${retriesRemaining} attempts remaining`;

Expand Down Expand Up @@ -718,7 +721,7 @@ export class OpenAI {
}
}

private shouldRetry(response: Response): boolean {
private async shouldRetry(response: Response): Promise<boolean> {
// Note this is not a standard header.
const shouldRetryHeader = response.headers.get('x-should-retry');

Expand Down Expand Up @@ -795,18 +798,18 @@ export class OpenAI {
return sleepSeconds * jitter * 1000;
}

buildRequest(
async buildRequest(
inputOptions: FinalRequestOptions,
{ retryCount = 0 }: { retryCount?: number } = {},
): { req: FinalizedRequestInit; url: string; timeout: number } {
): Promise<{ req: FinalizedRequestInit; url: string; timeout: number }> {
const options = { ...inputOptions };
const { method, path, query, defaultBaseURL } = options;

const url = this.buildURL(path!, query as Record<string, unknown>, defaultBaseURL);
if ('timeout' in options) validatePositiveInteger('timeout', options.timeout);
options.timeout = options.timeout ?? this.timeout;
const { bodyHeaders, body } = this.buildBody({ options });
const reqHeaders = this.buildHeaders({ options: inputOptions, method, bodyHeaders, retryCount });
const reqHeaders = await this.buildHeaders({ options: inputOptions, method, bodyHeaders, retryCount });

const req: FinalizedRequestInit = {
method,
Expand All @@ -822,7 +825,7 @@ export class OpenAI {
return { req, url, timeout: options.timeout };
}

private buildHeaders({
private async buildHeaders({
options,
method,
bodyHeaders,
Expand All @@ -832,7 +835,7 @@ export class OpenAI {
method: HTTPMethod;
bodyHeaders: HeadersLike;
retryCount: number;
}): Headers {
}): Promise<Headers> {
let idempotencyHeaders: HeadersLike = {};
if (this.idempotencyHeader && method !== 'get') {
if (!options.idempotencyKey) options.idempotencyKey = this.defaultIdempotencyKey();
Expand All @@ -850,7 +853,7 @@ export class OpenAI {
'OpenAI-Organization': this.organization,
'OpenAI-Project': this.project,
},
this.authHeaders(options),
await this.authHeaders(options),
this._options.defaultHeaders,
bodyHeaders,
options.headers,
Expand Down
2 changes: 1 addition & 1 deletion src/version.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export const VERSION = '5.8.3'; // x-release-please-version
export const VERSION = '5.8.4'; // x-release-please-version
30 changes: 15 additions & 15 deletions tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,22 @@ describe('instantiate client', () => {
apiKey: 'My API Key',
});

test('they are used in the request', () => {
const { req } = client.buildRequest({ path: '/foo', method: 'post' });
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');
});

test('can ignore `undefined` and leave the default', () => {
const { req } = client.buildRequest({
test('can ignore `undefined` and leave the default', async () => {
const { req } = await client.buildRequest({
path: '/foo',
method: 'post',
headers: { 'X-My-Default-Header': undefined },
});
expect(req.headers.get('x-my-default-header')).toEqual('2');
});

test('can be removed with `null`', () => {
const { req } = client.buildRequest({
test('can be removed with `null`', async () => {
const { req } = await client.buildRequest({
path: '/foo',
method: 'post',
headers: { 'X-My-Default-Header': null },
Expand Down Expand Up @@ -344,7 +344,7 @@ describe('instantiate client', () => {
});

describe('withOptions', () => {
test('creates a new client with overridden options', () => {
test('creates a new client with overridden options', async () => {
const client = new OpenAI({ baseURL: 'http://localhost:5000/', maxRetries: 3, apiKey: 'My API Key' });

const newClient = client.withOptions({
Expand All @@ -365,7 +365,7 @@ describe('instantiate client', () => {
expect(newClient.constructor).toBe(client.constructor);
});

test('inherits options from the parent client', () => {
test('inherits options from the parent client', async () => {
const client = new OpenAI({
baseURL: 'http://localhost:5000/',
defaultHeaders: { 'X-Test-Header': 'test-value' },
Expand All @@ -380,7 +380,7 @@ describe('instantiate client', () => {
// Test inherited options remain the same
expect(newClient.buildURL('/foo', null)).toEqual('http://localhost:5001/foo?test-param=test-value');

const { req } = newClient.buildRequest({ path: '/foo', method: 'get' });
const { req } = await newClient.buildRequest({ path: '/foo', method: 'get' });
expect(req.headers.get('x-test-header')).toEqual('test-value');
});

Expand Down Expand Up @@ -430,8 +430,8 @@ describe('request building', () => {
const client = new OpenAI({ apiKey: 'My API Key' });

describe('custom headers', () => {
test('handles undefined', () => {
const { req } = client.buildRequest({
test('handles undefined', async () => {
const { req } = await client.buildRequest({
path: '/foo',
method: 'post',
body: { value: 'hello' },
Expand Down Expand Up @@ -466,8 +466,8 @@ describe('default encoder', () => {
}
}
for (const jsonValue of [{}, [], { __proto__: null }, new Serializable(), new Collection(['item'])]) {
test(`serializes ${util.inspect(jsonValue)} as json`, () => {
const { req } = client.buildRequest({
test(`serializes ${util.inspect(jsonValue)} as json`, async () => {
const { req } = await client.buildRequest({
path: '/foo',
method: 'post',
body: jsonValue,
Expand All @@ -490,7 +490,7 @@ describe('default encoder', () => {
asyncIterable,
]) {
test(`converts ${util.inspect(streamValue)} to ReadableStream`, async () => {
const { req } = client.buildRequest({
const { req } = await client.buildRequest({
path: '/foo',
method: 'post',
body: streamValue,
Expand All @@ -503,7 +503,7 @@ describe('default encoder', () => {
}

test(`can set content-type for ReadableStream`, async () => {
const { req } = client.buildRequest({
const { req } = await client.buildRequest({
path: '/foo',
method: 'post',
body: new Response('a\nb\nc\n').body,
Expand Down
20 changes: 10 additions & 10 deletions tests/lib/azure.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,31 +30,31 @@ describe('instantiate azure client', () => {
apiVersion,
});

test('they are used in the request', () => {
const { req } = client.buildRequest({ path: '/foo', method: 'post' });
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');
});

test('can ignore `undefined` and leave the default', () => {
const { req } = client.buildRequest({
test('can ignore `undefined` and leave the default', async () => {
const { req } = await client.buildRequest({
path: '/foo',
method: 'post',
headers: { 'X-My-Default-Header': undefined },
});
expect(req.headers.get('x-my-default-header')).toEqual('2');
});

test('can be removed with `null`', () => {
const { req } = client.buildRequest({
test('can be removed with `null`', async () => {
const { req } = await client.buildRequest({
path: '/foo',
method: 'post',
headers: { 'X-My-Default-Header': null },
});
expect(req.headers.has('x-my-default-header')).toBe(false);
});

test('includes retry count', () => {
const { req } = client.buildRequest(
test('includes retry count', async () => {
const { req } = await client.buildRequest(
{
path: '/foo',
method: 'post',
Expand Down Expand Up @@ -593,8 +593,8 @@ describe('azure request building', () => {
});

describe('custom headers', () => {
test('handles undefined', () => {
const { req } = client.buildRequest({
test('handles undefined', async () => {
const { req } = await client.buildRequest({
path: '/foo',
method: 'post',
body: { value: 'hello' },
Expand Down
4 changes: 2 additions & 2 deletions tests/log.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ describe('debug()', () => {
defaultHeaders: authorizationTest,
});

const { req } = client.buildRequest({ path: '/foo', method: 'post' });
const { req } = await client.buildRequest({ path: '/foo', method: 'post' });
await client.post('/foo', {});

// Verify that the original headers weren't mutated
Expand Down Expand Up @@ -100,7 +100,7 @@ describe('debug()', () => {
fetch: opts.fetch,
});

const { req } = client.buildRequest({ path: '/foo', method: 'post' });
const { req } = await client.buildRequest({ path: '/foo', method: 'post' });
await client.post('/foo', {});

// Verify that the original headers weren't mutated
Expand Down