Skip to content

Commit 1b80539

Browse files
committed
Fix string input truncation
1 parent 26197a3 commit 1b80539

File tree

3 files changed

+14
-4
lines changed

3 files changed

+14
-4
lines changed

dev-packages/node-integration-tests/suites/tracing/openai/test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,7 @@ describe('OpenAI integration', () => {
439439
'scenario-message-truncation-responses.mjs',
440440
'instrument-with-pii.mjs',
441441
(createRunner, test) => {
442-
test('should truncate input strings that exceed byte limit', async () => {
442+
test('truncates string inputs when they exceed byte limit', async () => {
443443
await createRunner()
444444
.ignore('event')
445445
.expect({
@@ -453,7 +453,7 @@ describe('OpenAI integration', () => {
453453
'sentry.origin': 'auto.ai.openai',
454454
'gen_ai.system': 'openai',
455455
'gen_ai.request.model': 'gpt-3.5-turbo',
456-
// Message should be present but truncated to include only As
456+
// Messages should be present and should include truncated string input (contains only As)
457457
'gen_ai.request.messages': expect.stringMatching(/^A+$/),
458458
}),
459459
description: 'responses gpt-3.5-turbo',

packages/core/src/utils/ai/messageTruncation.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,3 +294,13 @@ export function truncateMessagesByBytes(messages: unknown[], maxBytes: number):
294294
export function truncateGenAiMessages(messages: unknown[]): unknown[] {
295295
return truncateMessagesByBytes(messages, DEFAULT_GEN_AI_MESSAGES_BYTE_LIMIT);
296296
}
297+
298+
/**
299+
* Truncate GenAI string input using the default byte limit.
300+
*
301+
* @param input - The string to truncate
302+
* @returns Truncated string
303+
*/
304+
export function truncateGenAiStringInput(input: string): string {
305+
return truncateTextByBytes(input, DEFAULT_GEN_AI_MESSAGES_BYTE_LIMIT);
306+
}

packages/core/src/utils/ai/utils.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
GEN_AI_USAGE_OUTPUT_TOKENS_ATTRIBUTE,
88
GEN_AI_USAGE_TOTAL_TOKENS_ATTRIBUTE,
99
} from './gen-ai-attributes';
10-
import { truncateGenAiMessages } from './messageTruncation';
10+
import { truncateGenAiMessages, truncateGenAiStringInput } from './messageTruncation';
1111
/**
1212
* Maps AI method paths to Sentry operation name
1313
*/
@@ -95,7 +95,7 @@ export function setTokenUsageAttributes(
9595
export function getTruncatedJsonString<T>(value: T | T[]): string {
9696
if (typeof value === 'string') {
9797
// Some values are already JSON strings, so we don't need to duplicate the JSON parsing
98-
return value;
98+
return truncateGenAiStringInput(value);
9999
}
100100
if (Array.isArray(value)) {
101101
// truncateGenAiMessages returns an array of strings, so we need to stringify it

0 commit comments

Comments
 (0)