@@ -88,7 +88,7 @@ function extractRequestAttributes(args: unknown[], methodPath: string): Record<s
88
88
/**
89
89
* Add attributes for Chat Completion responses
90
90
*/
91
- function addChatCompletionAttributes ( span : Span , response : OpenAiChatCompletionObject ) : void {
91
+ function addChatCompletionAttributes ( span : Span , response : OpenAiChatCompletionObject , recordOutputs ?: boolean ) : void {
92
92
setCommonResponseAttributes ( span , response . id , response . model , response . created ) ;
93
93
if ( response . usage ) {
94
94
setTokenUsageAttributes (
@@ -108,24 +108,26 @@ function addChatCompletionAttributes(span: Span, response: OpenAiChatCompletionO
108
108
} ) ;
109
109
}
110
110
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
+ }
121
123
}
122
124
}
123
125
}
124
126
125
127
/**
126
128
* Add attributes for Responses API responses
127
129
*/
128
- function addResponsesApiAttributes ( span : Span , response : OpenAIResponseObject ) : void {
130
+ function addResponsesApiAttributes ( span : Span , response : OpenAIResponseObject , recordOutputs ?: boolean ) : void {
129
131
setCommonResponseAttributes ( span , response . id , response . model , response . created_at ) ;
130
132
if ( response . status ) {
131
133
span . setAttributes ( {
@@ -141,19 +143,21 @@ function addResponsesApiAttributes(span: Span, response: OpenAIResponseObject):
141
143
) ;
142
144
}
143
145
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
+ ) ;
152
155
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
+ }
157
161
}
158
162
}
159
163
}
@@ -168,13 +172,13 @@ function addResponseAttributes(span: Span, result: unknown, recordOutputs?: bool
168
172
const response = result as OpenAiResponse ;
169
173
170
174
if ( isChatCompletionResponse ( response ) ) {
171
- addChatCompletionAttributes ( span , response ) ;
175
+ addChatCompletionAttributes ( span , response , recordOutputs ) ;
172
176
if ( recordOutputs && response . choices ?. length ) {
173
177
const responseTexts = response . choices . map ( choice => choice . message ?. content || '' ) ;
174
178
span . setAttributes ( { [ GEN_AI_RESPONSE_TEXT_ATTRIBUTE ] : JSON . stringify ( responseTexts ) } ) ;
175
179
}
176
180
} else if ( isResponsesApiResponse ( response ) ) {
177
- addResponsesApiAttributes ( span , response ) ;
181
+ addResponsesApiAttributes ( span , response , recordOutputs ) ;
178
182
if ( recordOutputs && response . output_text ) {
179
183
span . setAttributes ( { [ GEN_AI_RESPONSE_TEXT_ATTRIBUTE ] : response . output_text } ) ;
180
184
}
0 commit comments