Skip to content

Commit ba79b57

Browse files
Add example for configuring an anonymizer for LangGraph (#607)
## Overview Show how to set up an anonymizer with LangGraph to redact sensitive data. ## Type of change **Type:** Other ## Checklist <!-- Put an 'x' in all boxes that apply --> - [x] I have read the [contributing guidelines](README.md) - [x] I have tested my changes locally using `docs dev` - [x] All code examples have been tested and work correctly - [x] I have used **root relative** paths for internal links - [ ] I have updated navigation in `src/docs.json` if needed - [ ] I have gotten approval from the relevant reviewers - [ ] (Internal team members only / optional) I have created a preview deployment using the [Create Preview Branch workflow](https://github.com/langchain-ai/docs/actions/workflows/create-preview-branch.yml) ## Additional notes <!-- Any other information that would be helpful for reviewers --> --------- Co-authored-by: Lauren Hirata Singh <[email protected]>
1 parent f447e4b commit ba79b57

File tree

1 file changed

+56
-1
lines changed

1 file changed

+56
-1
lines changed

src/oss/langgraph/observability.mdx

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,61 @@ export LANGSMITH_API_KEY=<your-api-key>
3030

3131
By default, the trace will be logged to the project with the name `default`. To configure a custom project name, see [Log to a project](#log-to-a-project).
3232

33-
For more information, see [Trace with LangGraph](langsmith/trace-with-langgraph).
33+
For more information, see [Trace with LangGraph](/langsmith/trace-with-langgraph).
3434

3535
<observability />
36+
37+
## Use anonymizers to prevent logging of sensitive data in traces
38+
39+
You may want to mask sensitive data to prevent it from being logged to LangSmith. You can create [anonymizers](/langsmith/mask-inputs-outputs#rule-based-masking-of-inputs-and-outputs) and apply them to
40+
your graph using configuration. This example will redact anything matching the Social Security Number format XXX-XX-XXXX from traces sent to LangSmith.
41+
42+
:::python
43+
```python Python
44+
from langchain_core.tracers.langchain import LangChainTracer
45+
from langgraph.graph import StateGraph, MessagesState
46+
from langsmith import Client
47+
from langsmith.anonymizer import create_anonymizer
48+
49+
anonymizer = create_anonymizer([
50+
# Matches SSNs
51+
{ "pattern": r"\b\d{3}-?\d{2}-?\d{4}\b", "replace": "<ssn>" }
52+
])
53+
54+
tracer_client = Client(anonymizer=anonymizer)
55+
tracer = LangChainTracer(client=tracer_client)
56+
# Define the graph
57+
graph = (
58+
StateGraph(MessagesState)
59+
...
60+
.compile()
61+
.with_config({'callbacks': [tracer]})
62+
)
63+
```
64+
:::
65+
:::js
66+
```typescript TypeScript
67+
import { StateGraph } from "@langchain/langgraph";
68+
import { LangChainTracer } from "@langchain/core/tracers/tracer_langchain";
69+
import { StateAnnotation } from "./state.js";
70+
import { createAnonymizer } from "langsmith/anonymizer"
71+
import { Client } from "langsmith"
72+
73+
74+
const anonymizer = createAnonymizer([
75+
# Matches SSNs
76+
{ pattern: /\b\d{3}-?\d{2}-?\d{4}\b/, replace: "<ssn>" }
77+
])
78+
79+
const langsmithClient = new Client({ anonymizer })
80+
const tracer = new LangChainTracer({
81+
client: langsmithClient,
82+
});
83+
84+
export const graph = new StateGraph(StateAnnotation)
85+
.compile()
86+
.withConfig({
87+
callbacks: [tracer],
88+
});
89+
```
90+
:::

0 commit comments

Comments
 (0)