Skip to content

Commit f1b468f

Browse files
committed
apply message truncation to Vercel AI prompts and messages
1 parent 18265ac commit f1b468f

File tree

1 file changed

+8
-1
lines changed
  • packages/core/src/utils/vercel-ai

1 file changed

+8
-1
lines changed

packages/core/src/utils/vercel-ai/index.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { SEMANTIC_ATTRIBUTE_SENTRY_OP, SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN } from '
33
import type { Event } from '../../types-hoist/event';
44
import type { Span, SpanAttributes, SpanAttributeValue, SpanJSON, SpanOrigin } from '../../types-hoist/span';
55
import { spanToJSON } from '../spanUtils';
6+
import { truncateGenAiMessages } from '../ai/messageTruncation';
67
import { toolCallSpanMap } from './constants';
78
import type { TokenSummary } from './types';
89
import { accumulateTokensForParent, applyAccumulatedTokens } from './utils';
@@ -187,7 +188,13 @@ function processGenerateSpan(span: Span, name: string, attributes: SpanAttribute
187188
}
188189

189190
if (attributes[AI_PROMPT_ATTRIBUTE]) {
190-
span.setAttribute('gen_ai.prompt', attributes[AI_PROMPT_ATTRIBUTE]);
191+
const prompt = attributes[AI_PROMPT_ATTRIBUTE];
192+
if (Array.isArray(prompt)) {
193+
const truncatedPrompt = truncateGenAiMessages(prompt);
194+
span.setAttribute('gen_ai.prompt', JSON.stringify(truncatedPrompt));
195+
} else {
196+
span.setAttribute('gen_ai.prompt', prompt);
197+
}
191198
}
192199
if (attributes[AI_MODEL_ID_ATTRIBUTE] && !attributes[GEN_AI_RESPONSE_MODEL_ATTRIBUTE]) {
193200
span.setAttribute(GEN_AI_RESPONSE_MODEL_ATTRIBUTE, attributes[AI_MODEL_ID_ATTRIBUTE]);

0 commit comments

Comments
 (0)