Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,10 @@ export class OpenAIAgentsTraceProcessor implements TracingProcessor {
if (typeof resp.output === 'string') {
otelSpan.setAttribute(OpenTelemetryConstants.GEN_AI_OUTPUT_MESSAGES_KEY, resp.output);
} else {
otelSpan.setAttribute(OpenTelemetryConstants.GEN_AI_OUTPUT_MESSAGES_KEY, JSON.stringify(resp.output));
otelSpan.setAttribute(
OpenTelemetryConstants.GEN_AI_OUTPUT_MESSAGES_KEY,
this.buildOutputMessages(resp.output as Array<{ role: string; content: Array<{ type: string; text: string }> }>)
Copy link

Copilot AI Jan 13, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extra whitespace before 'as' keyword. There should be only one space between 'output' and 'as'.

Suggested change
this.buildOutputMessages(resp.output as Array<{ role: string; content: Array<{ type: string; text: string }> }>)
this.buildOutputMessages(resp.output as Array<{ role: string; content: Array<{ type: string; text: string }> }>)

Copilot uses AI. Check for mistakes.
);
}
}

Expand All @@ -250,24 +253,53 @@ export class OpenAIAgentsTraceProcessor implements TracingProcessor {

if (inputObj && !this.suppressInvokeAgentInput) {
if (typeof inputObj === 'string') {
try {
const parsed = JSON.parse(inputObj as string);
if (Array.isArray(parsed)) {
otelSpan.setAttribute(
OpenTelemetryConstants.GEN_AI_INPUT_MESSAGES_KEY,
this.buildInputMessages(parsed)
);
return;
}
} catch {
// If parsing fails, fall back to raw string behavior
}
otelSpan.setAttribute(OpenTelemetryConstants.GEN_AI_INPUT_MESSAGES_KEY, inputObj);
} else if (Array.isArray(inputObj)) {
// Store the complete _input structure as JSON
// build the input messages from array
Copy link

Copilot AI Jan 13, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment should start with an uppercase letter for consistency with the codebase style. Change 'build' to 'Build'.

Suggested change
// build the input messages from array
// Build the input messages from array

Copilot uses AI. Check for mistakes.
otelSpan.setAttribute(
OpenTelemetryConstants.GEN_AI_INPUT_MESSAGES_KEY,
JSON.stringify(inputObj)
this.buildInputMessages(inputObj)
);
}
}
}

Copy link

Copilot AI Jan 13, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new private method 'buildInputMessages' is missing JSDoc documentation. Other private methods in this file have JSDoc comments explaining their purpose. Add a JSDoc comment describing what this method does, its parameters, and return value.

Suggested change
/**
* Builds a JSON-encoded representation of input messages for tracing.
* Extracts the text content of user-role messages when available, otherwise
* falls back to serializing the full input array.
*
* @param arr - Array of message objects containing a role and string content.
* @returns A JSON string of user message contents, or the original array if no valid user content is found.
*/

Copilot uses AI. Check for mistakes.
// Get attributes but filter out unwanted ones
const attrs = Utils.getAttributesFromInput(inputObj);
Object.entries(attrs).forEach(([key, value]) => {
if (value !== null && value !== undefined &&
key !== Constants.GEN_AI_REQUEST_CONTENT_KEY) {
otelSpan.setAttribute(key, value as string | number | boolean);
}
});
private buildInputMessages(arr: Array<{ role: string; content: string }>): string {
const userTexts = arr
.filter((m) => m && m.role === 'user' && typeof m.content === 'string')
.map((m) => m.content);

return JSON.stringify(userTexts.length ? userTexts : arr);
}

Copy link

Copilot AI Jan 13, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new private method 'buildOutputMessages' is missing JSDoc documentation. Other private methods in this file have JSDoc comments explaining their purpose. Add a JSDoc comment describing what this method does, its parameters, and return value.

Suggested change
/**
* Builds a serialized representation of output messages from the agent response.
*
* Extracts text from message content parts where the part type is `output_text`
* and returns a JSON string of those texts. If no such texts are found, the
* original message array is serialized instead.
*
* @param arr - Array of response messages, each containing a role and a content
* array of typed text parts.
* @returns JSON string containing the extracted output texts or the original
* message array as a fallback.
*/

Copilot uses AI. Check for mistakes.
private buildOutputMessages(arr: Array<{ role: string; content: Array<{ type: string; text: string }> }>): string {
const userTexts: string[] = [];
Copy link

Copilot AI Jan 13, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The variable name 'userTexts' is misleading in the context of 'buildOutputMessages'. This variable collects output texts (type 'output_text'), not user texts. Consider renaming to 'outputTexts' for clarity.

Copilot uses AI. Check for mistakes.

for (const { content } of arr) {
if (!Array.isArray(content)) {
continue;
Comment on lines +291 to +292
Copy link

Copilot AI Jan 13, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing test coverage for the edge case where elements in the output array have non-array content properties. The code handles this case with a check on line 291-292, but there's no test verifying this behavior works correctly.

Copilot uses AI. Check for mistakes.
}

for (const { type, text } of content) {
Comment on lines +290 to +295
Copy link

Copilot AI Jan 13, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The destructuring pattern 'for (const { content } of arr)' on line 290 will throw a runtime error if any element in the array is null, undefined, or doesn't have a 'content' property. Add a null check before destructuring to prevent potential runtime errors.

Suggested change
for (const { content } of arr) {
if (!Array.isArray(content)) {
continue;
}
for (const { type, text } of content) {
for (const message of arr) {
if (!message || !Array.isArray(message.content)) {
continue;
}
for (const part of message.content) {
if (!part) {
continue;
}
const { type, text } = part;

Copilot uses AI. Check for mistakes.
Copy link

Copilot AI Jan 13, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The destructuring pattern 'for (const { type, text } of content)' on line 295 will throw a runtime error if any element in the content array is null, undefined, or doesn't have 'type' or 'text' properties. Add a null check before destructuring to prevent potential runtime errors.

Suggested change
for (const { type, text } of content) {
for (const item of content) {
if (!item) {
continue;
}
const { type, text } = item;

Copilot uses AI. Check for mistakes.
if (type === 'output_text' && typeof text === 'string') {
userTexts.push(text);
}
}
}

return JSON.stringify(userTexts.length ? userTexts : arr);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -550,5 +550,210 @@ describe('OpenAIAgentsTraceProcessor', () => {
const keys = (respMock._attrs as Array<[string, unknown]>).map(([k]) => k);
expect(keys).not.toContain(OpenTelemetryConstants.GEN_AI_INPUT_MESSAGES_KEY);
});

it('records full array JSON when only assistant messages are present', async () => {
const processor = new OpenAIAgentsTraceProcessor(tracer);
const traceData = { traceId: 'trace-assistant-only', name: 'Agent' } as any;
await processor.onTraceStart(traceData);

const inputArray = [
{
role: 'assistant',
content: 'Assistant reply',
},
];

const respSpan = {
spanId: 'resp-assistant-span',
traceId: 'trace-assistant-only',
startedAt: new Date().toISOString(),
spanData: {
type: 'response' as const,
name: 'ResponseAssistantOnly',
_input: inputArray,
_response: { model: 'gpt-4', output: 'ok' },
},
} as any;

await processor.onSpanStart(respSpan);
await processor.onSpanEnd(respSpan);

const respMock = spansByName['ResponseAssistantOnly'];
const attrs = respMock._attrs as Array<[string, unknown]>;
const entry = attrs.find(([k]) => k === OpenTelemetryConstants.GEN_AI_INPUT_MESSAGES_KEY);
expect(entry).toBeDefined();

const value = entry![1] as string;
const parsed = JSON.parse(value);
expect(parsed).toEqual(inputArray);
});
it('records user text content for array _input on response spans', async () => {
const processor = new OpenAIAgentsTraceProcessor(tracer);
const traceData = { traceId: 'trace-array-input', name: 'Agent' } as any;
await processor.onTraceStart(traceData);

const respSpan = {
spanId: 'resp-array-span',
traceId: 'trace-array-input',
startedAt: new Date().toISOString(),
spanData: {
type: 'response' as const,
name: 'ResponseArray',
_input: [
{ role: 'user', content: 'Hello user 1' },
{ role: 'user', content: 'Hello user 2' },
],
_response: { model: 'gpt-4', output: 'ok' },
},
} as any;

await processor.onSpanStart(respSpan);
await processor.onSpanEnd(respSpan);

const respMock = spansByName['ResponseArray'];
const attrs = respMock._attrs as Array<[string, unknown]>;
const entry = attrs.find(([k]) => k === OpenTelemetryConstants.GEN_AI_INPUT_MESSAGES_KEY);
expect(entry).toBeDefined();

const value = entry![1] as string;
const parsed = JSON.parse(value);
expect(parsed).toEqual(['Hello user 1', 'Hello user 2']);
});

it('parses stringified array _input and records only user text content', async () => {
const processor = new OpenAIAgentsTraceProcessor(tracer);
const traceData = { traceId: 'trace-array-input-string', name: 'Agent' } as any;
await processor.onTraceStart(traceData);

const inputArray = [
{ role: 'user', content: 'Hello user 1' },
{ role: 'user', content: 'Hello user 2' },
{ role: 'assistant', content: 'Assistant reply' },
];

const respSpan = {
spanId: 'resp-array-span-string',
traceId: 'trace-array-input-string',
startedAt: new Date().toISOString(),
spanData: {
type: 'response' as const,
name: 'ResponseArrayString',
_input: JSON.stringify(inputArray),
_response: { model: 'gpt-4', output: 'ok' },
},
} as any;

await processor.onSpanStart(respSpan);
await processor.onSpanEnd(respSpan);

const respMock = spansByName['ResponseArrayString'];
const attrs = respMock._attrs as Array<[string, unknown]>;
const entry = attrs.find(([k]) => k === OpenTelemetryConstants.GEN_AI_INPUT_MESSAGES_KEY);
expect(entry).toBeDefined();

const value = entry![1] as string;
const parsed = JSON.parse(value);
expect(parsed).toEqual(['Hello user 1', 'Hello user 2']);
});

it('records [gen_ai.input.messages] attribute for array input with non standard schema on response spans', async () => {
const processor = new OpenAIAgentsTraceProcessor(tracer);
const traceData = { traceId: 'trace-array-input', name: 'Agent' } as any;
await processor.onTraceStart(traceData);
const inputArray = [
{ type: 'text', content: 'message 1' },
{ type: 'text', content: 'message 2' },
];
const respSpan = {
spanId: 'resp-array-span',
traceId: 'trace-array-input',
startedAt: new Date().toISOString(),
spanData: {
type: 'response' as const,
name: 'ResponseArray',
_input: inputArray,
Copy link

Copilot AI Jan 13, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extra whitespace before 'inputArray'. There should be only one space after the colon.

Suggested change
_input: inputArray,
_input: inputArray,

Copilot uses AI. Check for mistakes.
_response: { model: 'gpt-4', output: 'ok' },
},
} as any;

await processor.onSpanStart(respSpan);
await processor.onSpanEnd(respSpan);

const respMock = spansByName['ResponseArray'];
const attrs = respMock._attrs as Array<[string, unknown]>;
const entry = attrs.find(([k]) => k === OpenTelemetryConstants.GEN_AI_INPUT_MESSAGES_KEY);
expect(entry).toBeDefined();

const value = entry![1] as string;
const parsed = JSON.parse(value);
expect(parsed).toEqual(inputArray);
});

it('records GEN_AI_OUTPUT_MESSAGES as plain string when output is a string', async () => {
const processor = new OpenAIAgentsTraceProcessor(tracer);
const traceData = { traceId: 'trace-output-string', name: 'Agent' } as any;
await processor.onTraceStart(traceData);

const respSpan = {
spanId: 'resp-output-string',
traceId: 'trace-output-string',
startedAt: new Date().toISOString(),
spanData: {
type: 'response' as const,
name: 'ResponseOutputString',
_input: 'ignored',
_response: { model: 'gpt-4', output: 'final answer' },
},
} as any;

await processor.onSpanStart(respSpan);
await processor.onSpanEnd(respSpan);

const respMock = spansByName['ResponseOutputString'];
const attrs = respMock._attrs as Array<[string, unknown]>;
const entry = attrs.find(([k]) => k === OpenTelemetryConstants.GEN_AI_OUTPUT_MESSAGES_KEY);
expect(entry).toBeDefined();
expect(entry![1]).toBe('final answer');
});

it('records GEN_AI_OUTPUT_MESSAGES as aggregated texts when output is structured', async () => {
const processor = new OpenAIAgentsTraceProcessor(tracer);
const traceData = { traceId: 'trace-output-structured', name: 'Agent' } as any;
await processor.onTraceStart(traceData);

const outputArray = [
{
role: 'assistant',
content: [
{ type: 'output_text', text: 'Hello user 1' },
{ type: 'output_text', text: 'Hello user 2' },
],
},
];

const respSpan = {
spanId: 'resp-output-structured',
traceId: 'trace-output-structured',
startedAt: new Date().toISOString(),
spanData: {
type: 'response' as const,
name: 'ResponseOutputStructured',
_input: 'ignored',
_response: { model: 'gpt-4', output: outputArray },
},
} as any;

await processor.onSpanStart(respSpan);
await processor.onSpanEnd(respSpan);

const respMock = spansByName['ResponseOutputStructured'];
const attrs = respMock._attrs as Array<[string, unknown]>;
const entry = attrs.find(([k]) => k === OpenTelemetryConstants.GEN_AI_OUTPUT_MESSAGES_KEY);
expect(entry).toBeDefined();

const value = entry![1] as string;
const parsed = JSON.parse(value);
expect(parsed).toEqual(['Hello user 1', 'Hello user 2']);
});
});
});
Loading