@@ -88,7 +88,7 @@ function extractRequestAttributes(args: unknown[], methodPath: string): Record<s
8888/**
8989 * Add attributes for Chat Completion responses
9090 */
91- function addChatCompletionAttributes ( span : Span , response : OpenAiChatCompletionObject ) : void {
91+ function addChatCompletionAttributes ( span : Span , response : OpenAiChatCompletionObject , recordOutputs ?: boolean ) : void {
9292 setCommonResponseAttributes ( span , response . id , response . model , response . created ) ;
9393 if ( response . usage ) {
9494 setTokenUsageAttributes (
@@ -108,24 +108,26 @@ function addChatCompletionAttributes(span: Span, response: OpenAiChatCompletionO
108108 } ) ;
109109 }
110110
111- // Extract tool calls from all choices
112- const toolCalls = response . choices
113- . map ( choice => choice . message ?. tool_calls )
114- . filter ( calls => Array . isArray ( calls ) && calls . length > 0 )
115- . flat ( ) ;
116-
117- if ( toolCalls . length > 0 ) {
118- span . setAttributes ( {
119- [ GEN_AI_RESPONSE_TOOL_CALLS_ATTRIBUTE ] : JSON . stringify ( toolCalls ) ,
120- } ) ;
111+ // Extract tool calls from all choices (only if recordOutputs is true)
112+ if ( recordOutputs ) {
113+ const toolCalls = response . choices
114+ . map ( choice => choice . message ?. tool_calls )
115+ . filter ( calls => Array . isArray ( calls ) && calls . length > 0 )
116+ . flat ( ) ;
117+
118+ if ( toolCalls . length > 0 ) {
119+ span . setAttributes ( {
120+ [ GEN_AI_RESPONSE_TOOL_CALLS_ATTRIBUTE ] : JSON . stringify ( toolCalls ) ,
121+ } ) ;
122+ }
121123 }
122124 }
123125}
124126
125127/**
126128 * Add attributes for Responses API responses
127129 */
128- function addResponsesApiAttributes ( span : Span , response : OpenAIResponseObject ) : void {
130+ function addResponsesApiAttributes ( span : Span , response : OpenAIResponseObject , recordOutputs ?: boolean ) : void {
129131 setCommonResponseAttributes ( span , response . id , response . model , response . created_at ) ;
130132 if ( response . status ) {
131133 span . setAttributes ( {
@@ -141,19 +143,21 @@ function addResponsesApiAttributes(span: Span, response: OpenAIResponseObject):
141143 ) ;
142144 }
143145
144- // Extract function calls from the response output
145- const responseWithOutput = response as OpenAIResponseObject & { output ?: unknown [ ] } ;
146- if ( Array . isArray ( responseWithOutput . output ) && responseWithOutput . output . length > 0 ) {
147- // Filter for function_call type objects in the output array
148- const functionCalls = responseWithOutput . output . filter (
149- ( item ) : unknown =>
150- typeof item === 'object' && item !== null && ( item as Record < string , unknown > ) . type === 'function_call' ,
151- ) ;
146+ // Extract function calls from output (only if recordOutputs is true)
147+ if ( recordOutputs ) {
148+ const responseWithOutput = response as OpenAIResponseObject & { output ?: unknown [ ] } ;
149+ if ( Array . isArray ( responseWithOutput . output ) && responseWithOutput . output . length > 0 ) {
150+ // Filter for function_call type objects in the output array
151+ const functionCalls = responseWithOutput . output . filter (
152+ ( item ) : unknown =>
153+ typeof item === 'object' && item !== null && ( item as Record < string , unknown > ) . type === 'function_call' ,
154+ ) ;
152155
153- if ( functionCalls . length > 0 ) {
154- span . setAttributes ( {
155- [ GEN_AI_RESPONSE_TOOL_CALLS_ATTRIBUTE ] : JSON . stringify ( functionCalls ) ,
156- } ) ;
156+ if ( functionCalls . length > 0 ) {
157+ span . setAttributes ( {
158+ [ GEN_AI_RESPONSE_TOOL_CALLS_ATTRIBUTE ] : JSON . stringify ( functionCalls ) ,
159+ } ) ;
160+ }
157161 }
158162 }
159163}
@@ -168,13 +172,13 @@ function addResponseAttributes(span: Span, result: unknown, recordOutputs?: bool
168172 const response = result as OpenAiResponse ;
169173
170174 if ( isChatCompletionResponse ( response ) ) {
171- addChatCompletionAttributes ( span , response ) ;
175+ addChatCompletionAttributes ( span , response , recordOutputs ) ;
172176 if ( recordOutputs && response . choices ?. length ) {
173177 const responseTexts = response . choices . map ( choice => choice . message ?. content || '' ) ;
174178 span . setAttributes ( { [ GEN_AI_RESPONSE_TEXT_ATTRIBUTE ] : JSON . stringify ( responseTexts ) } ) ;
175179 }
176180 } else if ( isResponsesApiResponse ( response ) ) {
177- addResponsesApiAttributes ( span , response ) ;
181+ addResponsesApiAttributes ( span , response , recordOutputs ) ;
178182 if ( recordOutputs && response . output_text ) {
179183 span . setAttributes ( { [ GEN_AI_RESPONSE_TEXT_ATTRIBUTE ] : response . output_text } ) ;
180184 }
0 commit comments