1- import { describe , expect , it } from 'vitest' ;
1+ import { describe , expect , it , vi } from 'vitest' ;
22import {
3+ addResponsesApiAttributes ,
34 buildMethodPath ,
5+ extractRequestParameters ,
46 getOperationName ,
57 getSpanOperation ,
68 isChatCompletionChunk ,
@@ -10,6 +12,7 @@ import {
1012 isResponsesApiStreamEvent ,
1113 shouldInstrument ,
1214} from '../../../src/tracing/openai/utils' ;
15+ import type { OpenAIResponseObject } from '../../../src/tracing/openai/types' ;
1316
1417describe ( 'openai-utils' , ( ) => {
1518 describe ( 'getOperationName' , ( ) => {
@@ -67,6 +70,31 @@ describe('openai-utils', () => {
6770 } ) ;
6871 } ) ;
6972
73+ describe ( 'extractRequestParameters' , ( ) => {
74+ it ( 'should include the request model when it is explicitly provided' , ( ) => {
75+ expect (
76+ extractRequestParameters ( {
77+ model : 'gpt-4.1-mini' ,
78+ temperature : 0.2 ,
79+ } ) ,
80+ ) . toEqual ( {
81+ 'gen_ai.request.model' : 'gpt-4.1-mini' ,
82+ 'gen_ai.request.temperature' : 0.2 ,
83+ } ) ;
84+ } ) ;
85+
86+ it ( 'should default the request model to unknown when it is not provided' , ( ) => {
87+ expect (
88+ extractRequestParameters ( {
89+ temperature : 0.2 ,
90+ } ) ,
91+ ) . toEqual ( {
92+ 'gen_ai.request.model' : 'unknown' ,
93+ 'gen_ai.request.temperature' : 0.2 ,
94+ } ) ;
95+ } ) ;
96+ } ) ;
97+
7098 describe ( 'isChatCompletionResponse' , ( ) => {
7199 it ( 'should return true for valid chat completion responses' , ( ) => {
72100 const validResponse = {
@@ -185,4 +213,30 @@ describe('openai-utils', () => {
185213 expect ( isConversationResponse ( { object : null } ) ) . toBe ( false ) ;
186214 } ) ;
187215 } ) ;
216+
217+ describe ( 'addResponsesApiAttributes' , ( ) => {
218+ it ( 'should backfill the request model and span name from the response model' , ( ) => {
219+ const span = {
220+ setAttributes : vi . fn ( ) ,
221+ updateName : vi . fn ( ) ,
222+ } ;
223+
224+ addResponsesApiAttributes (
225+ span as unknown as Parameters < typeof addResponsesApiAttributes > [ 0 ] ,
226+ {
227+ object : 'response' ,
228+ id : 'resp_123' ,
229+ model : 'gpt-4.1-mini' ,
230+ created_at : 1704067200 ,
231+ status : 'completed' ,
232+ } as unknown as OpenAIResponseObject ,
233+ false ,
234+ ) ;
235+
236+ expect ( span . setAttributes ) . toHaveBeenCalledWith ( {
237+ 'gen_ai.request.model' : 'gpt-4.1-mini' ,
238+ } ) ;
239+ expect ( span . updateName ) . toHaveBeenCalledWith ( 'chat gpt-4.1-mini' ) ;
240+ } ) ;
241+ } ) ;
188242} ) ;
0 commit comments