Skip to content

Commit bf1003e

Browse files
committed
packages/core/src/utils/ai/messageTruncation.ts
1 parent 29e4eda commit bf1003e

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import {
1919
GEN_AI_RESPONSE_TOOL_CALLS_ATTRIBUTE,
2020
GEN_AI_SYSTEM_ATTRIBUTE,
2121
} from '../ai/gen-ai-attributes';
22+
import { truncateGenAiMessages } from '../ai/messageTruncation';
2223
import { OPENAI_INTEGRATION_NAME } from './constants';
2324
import { instrumentStream } from './streaming';
2425
import type {
@@ -188,13 +189,16 @@ function addResponseAttributes(span: Span, result: unknown, recordOutputs?: bool
188189
}
189190
}
190191

191-
// Extract and record AI request inputs, if present. This is intentionally separate from response attributes.
192192
function addRequestAttributes(span: Span, params: Record<string, unknown>): void {
193193
if ('messages' in params) {
194-
span.setAttributes({ [GEN_AI_REQUEST_MESSAGES_ATTRIBUTE]: JSON.stringify(params.messages) });
194+
const messages = params.messages;
195+
const truncatedMessages = truncateGenAiMessages(messages as unknown[]);
196+
span.setAttributes({ [GEN_AI_REQUEST_MESSAGES_ATTRIBUTE]: JSON.stringify(truncatedMessages) });
195197
}
196198
if ('input' in params) {
197-
span.setAttributes({ [GEN_AI_REQUEST_MESSAGES_ATTRIBUTE]: JSON.stringify(params.input) });
199+
const input = params.input;
200+
const truncatedInput = truncateGenAiMessages(input as unknown[]);
201+
span.setAttributes({ [GEN_AI_REQUEST_MESSAGES_ATTRIBUTE]: JSON.stringify(truncatedInput) });
198202
}
199203
}
200204

0 commit comments

Comments
 (0)