Skip to content

Commit 95b1e95

Browse files
authored
feat(core): Automatically disable truncation when span streaming is enabled in LangGraph integration (#20231)
When span streaming is enabled, the `enableTruncation` option now defaults to `false` unless the user has explicitly set it. Closes: #20225
1 parent 8ace386 commit 95b1e95

4 files changed

Lines changed: 75 additions & 1 deletion

File tree

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import * as Sentry from '@sentry/node';
2+
import { loggingTransport } from '@sentry-internal/node-integration-tests';
3+
4+
Sentry.init({
5+
dsn: 'https://public@dsn.ingest.sentry.io/1337',
6+
release: '1.0',
7+
tracesSampleRate: 1.0,
8+
sendDefaultPii: true,
9+
transport: loggingTransport,
10+
traceLifecycle: 'stream',
11+
integrations: [
12+
Sentry.langGraphIntegration({
13+
enableTruncation: true,
14+
}),
15+
],
16+
});
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import * as Sentry from '@sentry/node';
2+
import { loggingTransport } from '@sentry-internal/node-integration-tests';
3+
4+
Sentry.init({
5+
dsn: 'https://public@dsn.ingest.sentry.io/1337',
6+
release: '1.0',
7+
tracesSampleRate: 1.0,
8+
sendDefaultPii: true,
9+
transport: loggingTransport,
10+
traceLifecycle: 'stream',
11+
});

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

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,4 +398,50 @@ describe('LangGraph integration', () => {
398398
});
399399
},
400400
);
401+
402+
const streamingLongContent = 'A'.repeat(50_000);
403+
404+
createEsmAndCjsTests(__dirname, 'scenario-no-truncation.mjs', 'instrument-streaming.mjs', (createRunner, test) => {
405+
test('automatically disables truncation when span streaming is enabled', async () => {
406+
await createRunner()
407+
.expect({
408+
span: container => {
409+
const spans = container.items;
410+
411+
const chatSpan = spans.find(s =>
412+
s.attributes?.[GEN_AI_INPUT_MESSAGES_ATTRIBUTE]?.value?.includes(streamingLongContent),
413+
);
414+
expect(chatSpan).toBeDefined();
415+
},
416+
})
417+
.start()
418+
.completed();
419+
});
420+
});
421+
422+
createEsmAndCjsTests(
423+
__dirname,
424+
'scenario-no-truncation.mjs',
425+
'instrument-streaming-with-truncation.mjs',
426+
(createRunner, test) => {
427+
test('respects explicit enableTruncation: true even when span streaming is enabled', async () => {
428+
await createRunner()
429+
.expect({
430+
span: container => {
431+
const spans = container.items;
432+
433+
// With explicit enableTruncation: true, truncation keeps only the last message
434+
// and drops the long content. The result should NOT contain the full 50k 'A' string.
435+
const chatSpan = spans.find(s =>
436+
s.attributes?.[GEN_AI_INPUT_MESSAGES_ATTRIBUTE]?.value?.includes('Follow-up question'),
437+
);
438+
expect(chatSpan).toBeDefined();
439+
expect(chatSpan!.attributes[GEN_AI_INPUT_MESSAGES_ATTRIBUTE].value).not.toContain(streamingLongContent);
440+
},
441+
})
442+
.start()
443+
.completed();
444+
});
445+
},
446+
);
401447
});

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {
1717
getJsonString,
1818
getTruncatedJsonString,
1919
resolveAIRecordingOptions,
20+
shouldEnableTruncation,
2021
} from '../ai/utils';
2122
import type { LangChainMessage } from '../langchain/types';
2223
import { normalizeLangChainMessages } from '../langchain/utils';
@@ -150,7 +151,7 @@ function instrumentCompiledGraphInvoke(
150151
span.setAttribute(GEN_AI_SYSTEM_INSTRUCTIONS_ATTRIBUTE, systemInstructions);
151152
}
152153

153-
const enableTruncation = options.enableTruncation ?? true;
154+
const enableTruncation = shouldEnableTruncation(options.enableTruncation);
154155
const filteredLength = Array.isArray(filteredMessages) ? filteredMessages.length : 0;
155156
span.setAttributes({
156157
[GEN_AI_INPUT_MESSAGES_ATTRIBUTE]: enableTruncation

0 commit comments

Comments
 (0)