Skip to content

Commit ddf30f2

Browse files
authored
feat: Document LangChain Integration (#15303)
<!-- Use this checklist to make sure your PR is ready for merge. You may delete any sections you don't need. --> Document LangChain Integration ## IS YOUR CHANGE URGENT? Help us prioritize incoming PRs by letting us know when the change needs to go live. - [ ] Urgent deadline (GA date, etc.): <!-- ENTER DATE HERE --> - [X] Other deadline: Oct 24th - [ ] None: Not urgent, can wait up to 1 week+ ## SLA - Teamwork makes the dream work, so please add a reviewer to your PRs. - Please give the docs team up to 1 week to review your PR unless you've added an urgent due date to it. Thanks in advance for your help! ## PRE-MERGE CHECKLIST *Make sure you've checked the following before merging your changes:* - [ ] Checked Vercel preview for correctness, including links - [ ] PR was reviewed and approved by any necessary SMEs (subject matter experts) - [ ] PR was reviewed and approved by a member of the [Sentry docs team](https://github.com/orgs/getsentry/teams/docs) ## LEGAL BOILERPLATE <!-- Sentry employees and contractors can delete or ignore this section. --> Look, I get it. The entity doing business as "Sentry" was incorporated in the State of Delaware in 2015 as Functional Software, Inc. and is gonna need some rights from me in order to utilize my contributions in this here PR. So here's the deal: I retain all rights, title and interest in and to my contributions, and by keeping this boilerplate intact I confirm that Sentry can use, modify, copy, and redistribute my contributions, under Sentry's choice of terms. ## EXTRA RESOURCES - [Sentry Docs contributor guide](https://docs.sentry.io/contributing/)
1 parent 6fad030 commit ddf30f2

File tree

16 files changed

+198
-0
lines changed

16 files changed

+198
-0
lines changed
Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
---
2+
title: LangChain
3+
description: "Adds instrumentation for LangChain."
4+
supported:
5+
- javascript.node
6+
- javascript.aws-lambda
7+
- javascript.azure-functions
8+
- javascript.connect
9+
- javascript.express
10+
- javascript.fastify
11+
- javascript.gcp-functions
12+
- javascript.hapi
13+
- javascript.hono
14+
- javascript.koa
15+
- javascript.nestjs
16+
- javascript.electron
17+
- javascript.nextjs
18+
- javascript.nuxt
19+
- javascript.solidstart
20+
- javascript.sveltekit
21+
- javascript.react-router
22+
- javascript.remix
23+
- javascript.astro
24+
- javascript.bun
25+
- javascript.tanstackstart-react
26+
- javascript.cloudflare
27+
---
28+
29+
<Alert>
30+
31+
This integration works in the Node.js, Cloudflare Workers, and Vercel Edge Functions runtimes. It requires SDK version `10.22.0` or higher.
32+
33+
</Alert>
34+
35+
_Import name: `Sentry.langChainIntegration`_
36+
37+
The `langChainIntegration` adds instrumentation for LangChain to capture spans by automatically wrapping LangChain operations and recording AI agent interactions with configurable input/output recording.
38+
39+
<PlatformSection notSupported={["javascript.cloudflare", "javascript.nextjs"]}>
40+
41+
It is enabled by default and will automatically capture spans for LangChain operations including chat models, LLM invocations, chains, and tool executions. You can opt-in to capture inputs and outputs by setting `recordInputs` and `recordOutputs` in the integration config:
42+
43+
```javascript
44+
Sentry.init({
45+
dsn: "____PUBLIC_DSN____",
46+
tracesSampleRate: 1.0,
47+
integrations: [
48+
Sentry.langChainIntegration({
49+
recordInputs: true,
50+
recordOutputs: true,
51+
}),
52+
],
53+
});
54+
```
55+
56+
</PlatformSection>
57+
58+
<PlatformSection supported={["javascript.cloudflare"]}>
59+
60+
For Cloudflare Workers, you need to manually instrument LangChain operations using the `createLangChainCallbackHandler` helper:
61+
62+
```javascript
63+
import * as Sentry from "@sentry/cloudflare";
64+
import { ChatAnthropic } from "@langchain/anthropic";
65+
66+
// Create a LangChain callback handler
67+
const callbackHandler = Sentry.createLangChainCallbackHandler({
68+
recordInputs: true, // Optional: record input prompts/messages
69+
recordOutputs: true, // Optional: record output responses
70+
});
71+
72+
// Use with chat models
73+
const model = new ChatAnthropic({
74+
model: "claude-3-5-sonnet-20241022",
75+
apiKey: process.env.ANTHROPIC_API_KEY,
76+
});
77+
78+
await model.invoke("Tell me a joke", {
79+
callbacks: [callbackHandler],
80+
});
81+
```
82+
83+
</PlatformSection>
84+
85+
## Options
86+
87+
### `recordInputs`
88+
89+
_Type: `boolean`_
90+
91+
Records inputs to LangChain operations (such as prompts and messages).
92+
93+
Defaults to `true` if `sendDefaultPii` is `true`.
94+
95+
```javascript
96+
Sentry.init({
97+
integrations: [Sentry.langChainIntegration({ recordInputs: true })],
98+
});
99+
```
100+
101+
### `recordOutputs`
102+
103+
_Type: `boolean`_
104+
105+
Records outputs from LangChain operations (such as generated text and responses).
106+
107+
Defaults to `true` if `sendDefaultPii` is `true`.
108+
109+
```javascript
110+
Sentry.init({
111+
integrations: [Sentry.langChainIntegration({ recordOutputs: true })],
112+
});
113+
```
114+
115+
## Configuration
116+
117+
By default this integration adds tracing support for LangChain operations including:
118+
119+
- **Chat model invocations** (`gen_ai.chat`) - Captures spans for chat model calls
120+
- **LLM invocations** (`gen_ai.pipeline`) - Captures spans for LLM pipeline executions
121+
- **Chain executions** (`gen_ai.invoke_agent`) - Captures spans for chain invocations
122+
- **Tool executions** (`gen_ai.execute_tool`) - Captures spans for tool calls
123+
124+
### Supported Runnables
125+
126+
The integration automatically instruments the following LangChain runnable methods:
127+
128+
- `invoke()` - Single execution
129+
- `stream()` - Streaming execution
130+
- `batch()` - Batch execution
131+
132+
<PlatformSection notSupported={["javascript.cloudflare", "javascript.nextjs"]}>
133+
134+
### Supported Providers
135+
136+
The automatic instrumentation supports the following LangChain provider packages:
137+
138+
- `@langchain/anthropic`
139+
- `@langchain/openai`
140+
- `@langchain/google-genai`
141+
- `@langchain/mistralai`
142+
- `@langchain/google-vertexai`
143+
- `@langchain/groq`
144+
145+
</PlatformSection>
146+
147+
<PlatformSection supported={['javascript.nextjs']}>
148+
149+
## Edge runtime
150+
151+
This integration is automatically instrumented in the Node.js runtime. For Next.js applications using the Edge runtime, you need to manually instrument LangChain operations using the callback handler:
152+
153+
```javascript
154+
import * as Sentry from "@sentry/nextjs";
155+
import { ChatAnthropic } from "@langchain/anthropic";
156+
157+
// Create a LangChain callback handler
158+
const callbackHandler = Sentry.createLangChainCallbackHandler({
159+
recordInputs: true,
160+
recordOutputs: true,
161+
});
162+
163+
// Use with chat models
164+
const model = new ChatAnthropic({
165+
model: "claude-3-5-sonnet-20241022",
166+
apiKey: process.env.ANTHROPIC_API_KEY,
167+
});
168+
169+
await model.invoke("Tell me a joke", {
170+
callbacks: [callbackHandler],
171+
});
172+
```
173+
174+
</PlatformSection>
175+
176+
## Supported Versions
177+
178+
- `langchain`: `>=0.1.0 <1.0.0`

docs/platforms/javascript/common/tracing/instrumentation/ai-agents-module.mdx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,12 @@ The JavaScript SDK supports automatic instrumentation for some AI libraries. We
5555
>
5656
- Google Gen AI SDK
5757
</PlatformLink>
58+
<PlatformLink
59+
to="/configuration/integrations/langchain/"
60+
notSupported={["javascript.deno"]}
61+
>
62+
- LangChain
63+
</PlatformLink>
5864

5965
## Manual Instrumentation
6066

platform-includes/configuration/integrations/javascript.astro.mdx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,5 +84,6 @@ Depending on whether an integration enhances the functionality of a particular r
8484
| [`openAIIntegration`](./openai) || || |
8585
| [`anthropicAIIntegration`](./anthropic) |||| |
8686
| [`googleGenAIIntegration`](./google-genai) |||| |
87+
| [`langChainIntegration`](./langchain) |||| |
8788
| [`zodErrorsIntegration`](./zodErrors) | | | ||
8889
| [`pinoIntegration`](./pino) | || | |

platform-includes/configuration/integrations/javascript.aws-lambda.mdx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
| [`openAIIntegration`](./openai) || || |
2222
| [`anthropicAIIntegration`](./anthropic) |||| |
2323
| [`googleGenAIIntegration`](./google-genai) |||| |
24+
| [`langChainIntegration`](./langchain) |||| |
2425
| [`amqplibIntegration`](./amqplib) | | || |
2526
| [`anrIntegration`](./anr) | || | |
2627
| [`captureConsoleIntegration`](./captureconsole) | | | ||

platform-includes/configuration/integrations/javascript.bun.mdx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
| [`openAIIntegration`](./openai) || || |
3434
| [`anthropicAIIntegration`](./anthropic) |||| |
3535
| [`googleGenAIIntegration`](./google-genai) |||| |
36+
| [`langChainIntegration`](./langchain) |||| |
3637
| [`captureConsoleIntegration`](./captureconsole) | | | ||
3738
| [`dataloaderIntegration`](./dataloader) | | || |
3839
| [`extraErrorDataIntegration`](./extraerrordata) | | | ||

platform-includes/configuration/integrations/javascript.connect.mdx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
| [`openAIIntegration`](./openai) || || |
3535
| [`anthropicAIIntegration`](./anthropic) |||| |
3636
| [`googleGenAIIntegration`](./google-genai) |||| |
37+
| [`langChainIntegration`](./langchain) |||| |
3738
| [`anrIntegration`](./anr) | || | |
3839
| [`captureConsoleIntegration`](./captureconsole) | | | ||
3940
| [`eventLoopBlockIntegration`](./event-loop-block) | || | |

platform-includes/configuration/integrations/javascript.fastify.mdx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
| [`openAIIntegration`](./openai) || || |
3535
| [`anthropicAIIntegration`](./anthropic) |||| |
3636
| [`googleGenAIIntegration`](./google-genai) |||| |
37+
| [`langChainIntegration`](./langchain) |||| |
3738
| [`anrIntegration`](./anr) | || | |
3839
| [`captureConsoleIntegration`](./captureconsole) | | | ||
3940
| [`eventLoopBlockIntegration`](./event-loop-block) | || | |

platform-includes/configuration/integrations/javascript.gcp-functions.mdx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
| [`openAIIntegration`](./openai) || || |
2222
| [`anthropicAIIntegration`](./anthropic) |||| |
2323
| [`googleGenAIIntegration`](./google-genai) |||| |
24+
| [`langChainIntegration`](./langchain) |||| |
2425
| [`amqplibIntegration`](./amqplib) | | || |
2526
| [`anrIntegration`](./anr) | || | |
2627
| [`captureConsoleIntegration`](./captureconsole) | | | ||

platform-includes/configuration/integrations/javascript.hapi.mdx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
| [`openAIIntegration`](./openai) || || |
3535
| [`anthropicAIIntegration`](./anthropic) |||| |
3636
| [`googleGenAIIntegration`](./google-genai) |||| |
37+
| [`langChainIntegration`](./langchain) |||| |
3738
| [`anrIntegration`](./anr) | || | |
3839
| [`captureConsoleIntegration`](./captureconsole) | | | ||
3940
| [`eventLoopBlockIntegration`](./event-loop-block) | || | |

platform-includes/configuration/integrations/javascript.nestjs.mdx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,5 +47,6 @@
4747
| [`openAIIntegration`](./openai) || || |
4848
| [`anthropicAIIntegration`](./anthropic) |||| |
4949
| [`googleGenAIIntegration`](./google-genai) |||| |
50+
| [`langChainIntegration`](./langchain) |||| |
5051
| [`zodErrorsIntegration`](./zodErrors) | | | ||
5152
| [`pinoIntegration`](./pino) | || | |

0 commit comments

Comments
 (0)