Skip to content

Commit 8ace386

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

File tree

4 files changed

+75
-2
lines changed

4 files changed

+75
-2
lines changed
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.langChainIntegration({
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/langchain/test.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -582,4 +582,50 @@ describe('LangChain integration', () => {
582582
});
583583
},
584584
);
585+
586+
const streamingLongContent = 'A'.repeat(50_000);
587+
588+
createEsmAndCjsTests(__dirname, 'scenario-no-truncation.mjs', 'instrument-streaming.mjs', (createRunner, test) => {
589+
test('automatically disables truncation when span streaming is enabled', async () => {
590+
await createRunner()
591+
.expect({
592+
span: container => {
593+
const spans = container.items;
594+
595+
const chatSpan = spans.find(s =>
596+
s.attributes?.[GEN_AI_INPUT_MESSAGES_ATTRIBUTE]?.value?.includes(streamingLongContent),
597+
);
598+
expect(chatSpan).toBeDefined();
599+
},
600+
})
601+
.start()
602+
.completed();
603+
});
604+
});
605+
606+
createEsmAndCjsTests(
607+
__dirname,
608+
'scenario-no-truncation.mjs',
609+
'instrument-streaming-with-truncation.mjs',
610+
(createRunner, test) => {
611+
test('respects explicit enableTruncation: true even when span streaming is enabled', async () => {
612+
await createRunner()
613+
.expect({
614+
span: container => {
615+
const spans = container.items;
616+
617+
// With explicit enableTruncation: true, truncation keeps only the last message
618+
// and drops the long content. The result should NOT contain the full 50k 'A' string.
619+
const chatSpan = spans.find(s =>
620+
s.attributes?.[GEN_AI_INPUT_MESSAGES_ATTRIBUTE]?.value?.includes('Follow-up question'),
621+
);
622+
expect(chatSpan).toBeDefined();
623+
expect(chatSpan!.attributes[GEN_AI_INPUT_MESSAGES_ATTRIBUTE].value).not.toContain(streamingLongContent);
624+
},
625+
})
626+
.start()
627+
.completed();
628+
});
629+
},
630+
);
585631
});

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {
1010
GEN_AI_TOOL_NAME_ATTRIBUTE,
1111
GEN_AI_TOOL_OUTPUT_ATTRIBUTE,
1212
} from '../ai/gen-ai-attributes';
13-
import { resolveAIRecordingOptions } from '../ai/utils';
13+
import { resolveAIRecordingOptions, shouldEnableTruncation } from '../ai/utils';
1414
import { LANGCHAIN_ORIGIN } from './constants';
1515
import type {
1616
LangChainCallbackHandler,
@@ -34,7 +34,7 @@ import {
3434
*/
3535
export function createLangChainCallbackHandler(options: LangChainOptions = {}): LangChainCallbackHandler {
3636
const { recordInputs, recordOutputs } = resolveAIRecordingOptions(options);
37-
const enableTruncation = options.enableTruncation ?? true;
37+
const enableTruncation = shouldEnableTruncation(options.enableTruncation);
3838

3939
// Internal state - single instance tracks all spans
4040
const spanMap = new Map<string, Span>();

0 commit comments

Comments
 (0)