Skip to content

Commit 7e3614c

Browse files
committed
fix(core): backfill OpenAI responses model in spans
1 parent 85187e4 commit 7e3614c

2 files changed

Lines changed: 62 additions & 1 deletion

File tree

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { Span } from '../../types-hoist/span';
2+
import { updateSpanName } from '../../utils/spanUtils';
23
import {
34
GEN_AI_CONVERSATION_ID_ATTRIBUTE,
45
GEN_AI_REQUEST_DIMENSIONS_ATTRIBUTE,
@@ -201,6 +202,12 @@ export function addChatCompletionAttributes(
201202
*/
202203
export function addResponsesApiAttributes(span: Span, response: OpenAIResponseObject, recordOutputs?: boolean): void {
203204
setCommonResponseAttributes(span, response.id, response.model, response.created_at);
205+
if (typeof response.model === 'string' && response.model.length > 0) {
206+
span.setAttributes({
207+
[GEN_AI_REQUEST_MODEL_ATTRIBUTE]: response.model,
208+
});
209+
updateSpanName(span, `${OPENAI_OPERATIONS.CHAT} ${response.model}`);
210+
}
204211
if (response.status) {
205212
span.setAttributes({
206213
[GEN_AI_RESPONSE_FINISH_REASONS_ATTRIBUTE]: JSON.stringify([response.status]),

packages/core/test/lib/utils/openai-utils.test.ts

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
import { describe, expect, it } from 'vitest';
1+
import { describe, expect, it, vi } from 'vitest';
22
import {
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

1417
describe('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

Comments
 (0)