Skip to content

Commit aa6d279

Browse files
committed
fixup! fix(tracing): add gen_ai.request.messages.original_length attributes (#18608)
1 parent 6cd97d6 commit aa6d279

File tree

1 file changed

+7
-13
lines changed
  • packages/core/src/tracing/openai

1 file changed

+7
-13
lines changed

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

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -117,20 +117,14 @@ function addResponseAttributes(span: Span, result: unknown, recordOutputs?: bool
117117

118118
// Extract and record AI request inputs, if present. This is intentionally separate from response attributes.
119119
function addRequestAttributes(span: Span, params: Record<string, unknown>): void {
120-
if ('messages' in params) {
121-
const truncatedMessages = getTruncatedJsonString(params.messages);
122-
span.setAttributes({
123-
[GEN_AI_REQUEST_MESSAGES_ATTRIBUTE]: truncatedMessages,
124-
[GEN_AI_REQUEST_MESSAGES_ORIGINAL_LENGTH_ATTRIBUTE]: undefined,
125-
});
126-
if (Array.isArray(params.messages)) {
127-
span.setAttribute(GEN_AI_REQUEST_MESSAGES_ORIGINAL_LENGTH_ATTRIBUTE, params.messages.length);
128-
}
129-
} else if ('input' in params) {
130-
const truncatedInput = getTruncatedJsonString(params.input);
120+
const src = 'input' in params ? params.input : 'messages' in params ? params.messages : undefined;
121+
// typically an array, but can be other types. skip if an empty array.
122+
const length = Array.isArray(src) ? src.length : undefined;
123+
if (src && length !== 0) {
124+
const truncatedInput = getTruncatedJsonString(src);
131125
span.setAttribute(GEN_AI_REQUEST_MESSAGES_ATTRIBUTE, truncatedInput);
132-
if (Array.isArray(params.input)) {
133-
span.setAttribute(GEN_AI_REQUEST_MESSAGES_ORIGINAL_LENGTH_ATTRIBUTE, params.input.length);
126+
if (length) {
127+
span.setAttribute(GEN_AI_REQUEST_MESSAGES_ORIGINAL_LENGTH_ATTRIBUTE, length);
134128
}
135129
}
136130
}

0 commit comments

Comments
 (0)