Skip to content

Commit c99b1a5

Browse files
committed
fix some tests
1 parent 6d51b21 commit c99b1a5

File tree

3 files changed

+35
-35
lines changed

3 files changed

+35
-35
lines changed

dev-packages/node-integration-tests/suites/tracing/openai/openai-tool-calls/test.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,6 @@ describe('OpenAI Tool Calls integration', () => {
7272
'gen_ai.response.model': 'gpt-4',
7373
'gen_ai.response.id': 'chatcmpl-tools-123',
7474
'gen_ai.response.finish_reasons': '["tool_calls"]',
75-
'gen_ai.response.tool_calls': CHAT_TOOL_CALLS,
7675
'gen_ai.usage.input_tokens': 15,
7776
'gen_ai.usage.output_tokens': 25,
7877
'gen_ai.usage.total_tokens': 40,
@@ -101,7 +100,6 @@ describe('OpenAI Tool Calls integration', () => {
101100
'gen_ai.response.id': 'chatcmpl-stream-tools-123',
102101
'gen_ai.response.finish_reasons': '["tool_calls"]',
103102
'gen_ai.response.streaming': true,
104-
'gen_ai.response.tool_calls': CHAT_STREAM_TOOL_CALLS,
105103
'gen_ai.usage.input_tokens': 15,
106104
'gen_ai.usage.output_tokens': 25,
107105
'gen_ai.usage.total_tokens': 40,
@@ -128,7 +126,6 @@ describe('OpenAI Tool Calls integration', () => {
128126
'gen_ai.response.model': 'gpt-4',
129127
'gen_ai.response.id': 'resp_tools_789',
130128
'gen_ai.response.finish_reasons': '["completed"]',
131-
'gen_ai.response.tool_calls': RESPONSES_TOOL_CALLS,
132129
'gen_ai.usage.input_tokens': 8,
133130
'gen_ai.usage.output_tokens': 12,
134131
'gen_ai.usage.total_tokens': 20,
@@ -157,7 +154,6 @@ describe('OpenAI Tool Calls integration', () => {
157154
'gen_ai.response.id': 'resp_stream_tools_789',
158155
'gen_ai.response.finish_reasons': '["in_progress","completed"]',
159156
'gen_ai.response.streaming': true,
160-
'gen_ai.response.tool_calls': RESPONSES_TOOL_CALLS,
161157
'gen_ai.usage.input_tokens': 8,
162158
'gen_ai.usage.output_tokens': 12,
163159
'gen_ai.usage.total_tokens': 20,

packages/core/src/utils/openai/index.ts

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

packages/core/src/utils/openai/streaming.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -167,15 +167,15 @@ function processResponsesApiEvent(
167167

168168
// Handle output text delta
169169
if (recordOutputs) {
170-
if (event.type === 'response.output_text.delta' && 'delta' in event && event.delta) {
171-
state.responseTexts.push(event.delta);
172-
return;
173-
}
174-
175170
// Handle function call events for Responses API
176171
if (event.type === 'response.output_item.done' && 'item' in event) {
177172
state.responsesApiFunctionCalls.push(event.item as ResponseFunctionCall);
178173
}
174+
175+
if (event.type === 'response.output_text.delta' && 'delta' in event && event.delta) {
176+
state.responseTexts.push(event.delta);
177+
return;
178+
}
179179
}
180180

181181
if ('response' in event) {

0 commit comments

Comments
 (0)