From c70280bfabd3af2c263219f96395fdadd4ba3af7 Mon Sep 17 00:00:00 2001 From: Stainless Bot Date: Tue, 17 Sep 2024 16:21:01 +0000 Subject: [PATCH] feat: feat(client): add ._request_id property to object responses (#1078) --- README.md | 11 +++++ src/core.ts | 58 +++++++++++++++------- tests/responses.test.ts | 104 +++++++++++++++++++++++++++++++++++++++- tests/utils/typing.ts | 9 ++++ 4 files changed, 165 insertions(+), 17 deletions(-) create mode 100644 tests/utils/typing.ts diff --git a/README.md b/README.md index 03ee259ee..b3de7fa55 100644 --- a/README.md +++ b/README.md @@ -361,6 +361,17 @@ Error codes are as followed: | >=500 | `InternalServerError` | | N/A | `APIConnectionError` | +## Request IDs + +> For more information on debugging requests, see [these docs](https://platform.openai.com/docs/api-reference/debugging-requests) + +All object responses in the SDK provide a `_request_id` property which is added from the `x-request-id` response header so that you can quickly log failing requests and report them back to OpenAI. + +```ts +const completion = await client.chat.completions.create({ messages: [{ role: 'user', content: 'Say this is a test' }], model: 'gpt-4' }); +console.log(completion._request_id) // req_123 +``` + ## Microsoft Azure OpenAI To use this library with [Azure OpenAI](https://learn.microsoft.com/azure/ai-services/openai/overview), use the `AzureOpenAI` diff --git a/src/core.ts b/src/core.ts index a4bb87a32..90714d3ce 100644 --- a/src/core.ts +++ b/src/core.ts @@ -37,7 +37,7 @@ type APIResponseProps = { controller: AbortController; }; -async function defaultParseResponse(props: APIResponseProps): Promise { +async function defaultParseResponse(props: APIResponseProps): Promise> { const { response } = props; if (props.options.stream) { debug('response', response.status, response.url, response.headers, response.body); @@ -54,11 +54,11 @@ async function defaultParseResponse(props: APIResponseProps): Promise { // fetch refuses to read the body when the status code is 204. if (response.status === 204) { - return null as T; + return null as WithRequestID; } if (props.options.__binaryResponse) { - return response as unknown as T; + return response as unknown as WithRequestID; } const contentType = response.headers.get('content-type'); @@ -69,26 +69,44 @@ async function defaultParseResponse(props: APIResponseProps): Promise { debug('response', response.status, response.url, response.headers, json); - return json as T; + return _addRequestID(json, response); } const text = await response.text(); debug('response', response.status, response.url, response.headers, text); // TODO handle blob, arraybuffer, other content types, etc. - return text as unknown as T; + return text as unknown as WithRequestID; +} + +type WithRequestID = + T extends Array | Response | AbstractPage ? T + : T extends Record ? T & { _request_id?: string | null } + : T; + +function _addRequestID(value: T, response: Response): WithRequestID { + if (!value || typeof value !== 'object' || Array.isArray(value)) { + return value as WithRequestID; + } + + return Object.defineProperty(value, '_request_id', { + value: response.headers.get('x-request-id'), + enumerable: false, + }) as WithRequestID; } /** * A subclass of `Promise` providing additional helper methods * for interacting with the SDK. */ -export class APIPromise extends Promise { - private parsedPromise: Promise | undefined; +export class APIPromise extends Promise> { + private parsedPromise: Promise> | undefined; constructor( private responsePromise: Promise, - private parseResponse: (props: APIResponseProps) => PromiseOrValue = defaultParseResponse, + private parseResponse: ( + props: APIResponseProps, + ) => PromiseOrValue> = defaultParseResponse, ) { super((resolve) => { // this is maybe a bit weird but this has to be a no-op to not implicitly @@ -99,7 +117,9 @@ export class APIPromise extends Promise { } _thenUnwrap(transform: (data: T) => U): APIPromise { - return new APIPromise(this.responsePromise, async (props) => transform(await this.parseResponse(props))); + return new APIPromise(this.responsePromise, async (props) => + _addRequestID(transform(await this.parseResponse(props)), props.response), + ); } /** @@ -136,15 +156,15 @@ export class APIPromise extends Promise { return { data, response }; } - private parse(): Promise { + private parse(): Promise> { if (!this.parsedPromise) { - this.parsedPromise = this.responsePromise.then(this.parseResponse); + this.parsedPromise = this.responsePromise.then(this.parseResponse) as any as Promise>; } return this.parsedPromise; } - override then( - onfulfilled?: ((value: T) => TResult1 | PromiseLike) | undefined | null, + override then, TResult2 = never>( + onfulfilled?: ((value: WithRequestID) => TResult1 | PromiseLike) | undefined | null, onrejected?: ((reason: any) => TResult2 | PromiseLike) | undefined | null, ): Promise { return this.parse().then(onfulfilled, onrejected); @@ -152,11 +172,11 @@ export class APIPromise extends Promise { override catch( onrejected?: ((reason: any) => TResult | PromiseLike) | undefined | null, - ): Promise { + ): Promise | TResult> { return this.parse().catch(onrejected); } - override finally(onfinally?: (() => void) | undefined | null): Promise { + override finally(onfinally?: (() => void) | undefined | null): Promise> { return this.parse().finally(onfinally); } } @@ -706,7 +726,13 @@ export class PagePromise< ) { super( request, - async (props) => new Page(client, props.response, await defaultParseResponse(props), props.options), + async (props) => + new Page( + client, + props.response, + await defaultParseResponse(props), + props.options, + ) as WithRequestID, ); } diff --git a/tests/responses.test.ts b/tests/responses.test.ts index ef6ba27bf..fbd073a79 100644 --- a/tests/responses.test.ts +++ b/tests/responses.test.ts @@ -1,5 +1,8 @@ -import { createResponseHeaders } from 'openai/core'; +import { APIPromise, createResponseHeaders } from 'openai/core'; +import OpenAI from 'openai/index'; import { Headers } from 'openai/_shims/index'; +import { Response } from 'node-fetch'; +import { compareType } from './utils/typing'; describe('response parsing', () => { // TODO: test unicode characters @@ -23,3 +26,102 @@ describe('response parsing', () => { expect(headers['content-type']).toBe('text/xml, application/json'); }); }); + +describe('request id', () => { + test('types', () => { + compareType>, string>(true); + compareType>, number>(true); + compareType>, null>(true); + compareType>, void>(true); + compareType>, Response>(true); + compareType>, Response>(true); + compareType>, { foo: string } & { _request_id?: string | null }>( + true, + ); + compareType>>, Array<{ foo: string }>>(true); + }); + + test('object response', async () => { + const client = new OpenAI({ + apiKey: 'dummy', + fetch: async () => + new Response(JSON.stringify({ id: 'bar' }), { + headers: { 'x-request-id': 'req_id_xxx', 'content-type': 'application/json' }, + }), + }); + + const rsp = await client.chat.completions.create({ messages: [], model: 'gpt-4' }); + expect(rsp.id).toBe('bar'); + expect(rsp._request_id).toBe('req_id_xxx'); + expect(JSON.stringify(rsp)).toBe('{"id":"bar"}'); + }); + + test('envelope response', async () => { + const promise = new APIPromise<{ data: { foo: string } }>( + (async () => { + return { + response: new Response(JSON.stringify({ data: { foo: 'bar' } }), { + headers: { 'x-request-id': 'req_id_xxx', 'content-type': 'application/json' }, + }), + controller: {} as any, + options: {} as any, + }; + })(), + )._thenUnwrap((d) => d.data); + + const rsp = await promise; + expect(rsp.foo).toBe('bar'); + expect(rsp._request_id).toBe('req_id_xxx'); + }); + + test('page response', async () => { + const client = new OpenAI({ + apiKey: 'dummy', + fetch: async () => + new Response(JSON.stringify({ data: [{ foo: 'bar' }] }), { + headers: { 'x-request-id': 'req_id_xxx', 'content-type': 'application/json' }, + }), + }); + + const page = await client.fineTuning.jobs.list(); + expect(page.data).toMatchObject([{ foo: 'bar' }]); + expect((page as any)._request_id).toBeUndefined(); + }); + + test('array response', async () => { + const promise = new APIPromise>( + (async () => { + return { + response: new Response(JSON.stringify([{ foo: 'bar' }]), { + headers: { 'x-request-id': 'req_id_xxx', 'content-type': 'application/json' }, + }), + controller: {} as any, + options: {} as any, + }; + })(), + ); + + const rsp = await promise; + expect(rsp.length).toBe(1); + expect(rsp[0]).toMatchObject({ foo: 'bar' }); + expect((rsp as any)._request_id).toBeUndefined(); + }); + + test('string response', async () => { + const promise = new APIPromise( + (async () => { + return { + response: new Response('hello world', { + headers: { 'x-request-id': 'req_id_xxx', 'content-type': 'application/text' }, + }), + controller: {} as any, + options: {} as any, + }; + })(), + ); + + const result = await promise; + expect(result).toBe('hello world'); + expect((result as any)._request_id).toBeUndefined(); + }); +}); diff --git a/tests/utils/typing.ts b/tests/utils/typing.ts new file mode 100644 index 000000000..4a791d2a7 --- /dev/null +++ b/tests/utils/typing.ts @@ -0,0 +1,9 @@ +type Equal = (() => T extends X ? 1 : 2) extends () => T extends Y ? 1 : 2 ? true : false; + +export const expectType = (_expression: T): void => { + return; +}; + +export const compareType = (_expression: Equal): void => { + return; +};