File tree 2 files changed +27
-3
lines changed
2 files changed +27
-3
lines changed Original file line number Diff line number Diff line change @@ -138,8 +138,11 @@ export class APIPromise<T> extends Promise<WithRequestID<T>> {
138
138
asResponse ( ) : Promise < Response > {
139
139
return this . responsePromise . then ( ( p ) => p . response ) ;
140
140
}
141
+
141
142
/**
142
- * Gets the parsed response data and the raw `Response` instance.
143
+ * Gets the parsed response data, the raw `Response` instance and the ID of the request,
144
+ * returned via the X-Request-ID header which is useful for debugging requests and reporting
145
+ * issues to OpenAI.
143
146
*
144
147
* If you just want to get the raw `Response` instance without parsing it,
145
148
* you can use {@link asResponse()}.
@@ -151,9 +154,9 @@ export class APIPromise<T> extends Promise<WithRequestID<T>> {
151
154
* - `import 'openai/shims/node'` (if you're running on Node)
152
155
* - `import 'openai/shims/web'` (otherwise)
153
156
*/
154
- async withResponse ( ) : Promise < { data : T ; response : Response } > {
157
+ async withResponse ( ) : Promise < { data : T ; response : Response ; request_id : string | null | undefined } > {
155
158
const [ data , response ] = await Promise . all ( [ this . parse ( ) , this . asResponse ( ) ] ) ;
156
- return { data, response } ;
159
+ return { data, response, request_id : response . headers . get ( 'x-request-id' ) } ;
157
160
}
158
161
159
162
private parse ( ) : Promise < WithRequestID < T > > {
Original file line number Diff line number Diff line change @@ -41,6 +41,27 @@ describe('request id', () => {
41
41
compareType < Awaited < APIPromise < Array < { foo : string } > > > , Array < { foo : string } > > ( true ) ;
42
42
} ) ;
43
43
44
+ test ( 'withResponse' , async ( ) => {
45
+ const client = new OpenAI ( {
46
+ apiKey : 'dummy' ,
47
+ fetch : async ( ) =>
48
+ new Response ( JSON . stringify ( { id : 'bar' } ) , {
49
+ headers : { 'x-request-id' : 'req_id_xxx' , 'content-type' : 'application/json' } ,
50
+ } ) ,
51
+ } ) ;
52
+
53
+ const {
54
+ data : completion ,
55
+ response,
56
+ request_id,
57
+ } = await client . chat . completions . create ( { messages : [ ] , model : 'gpt-4' } ) . withResponse ( ) ;
58
+
59
+ expect ( request_id ) . toBe ( 'req_id_xxx' ) ;
60
+ expect ( response . headers . get ( 'x-request-id' ) ) . toBe ( 'req_id_xxx' ) ;
61
+ expect ( completion . id ) . toBe ( 'bar' ) ;
62
+ expect ( JSON . stringify ( completion ) ) . toBe ( '{"id":"bar"}' ) ;
63
+ } ) ;
64
+
44
65
test ( 'object response' , async ( ) => {
45
66
const client = new OpenAI ( {
46
67
apiKey : 'dummy' ,
You can’t perform that action at this time.
0 commit comments