Protecting LangChain Workflows
Add FastPII preprocessing and filtering to LangChain chains, tools, and streaming flows.
Protecting LangChain Workflows
LangChain applications usually move text through several layers: loaders, chains, tools, memory, and agent prompts. The most reliable pattern is to sanitize data at the edges and before every model call.
Use PIIPreprocessor at the chain boundary
from fastpii.integrations.langchain import PIIPreprocessor
preprocessor = PIIPreprocessor(regions=["cz"])
safe_input = preprocessor.preprocess(
"Create a case summary for Jan Novak, born 1985-01-01, living at Národní 12."
)Run this before text enters retrieval, summarization, or agent memory.
Add create_pii_filter_tool() to agents
FastPII already provides a reusable LangChain tool for redacting PII before text moves downstream.
from fastpii.integrations.langchain import create_pii_filter_tool
pii_filter_tool = create_pii_filter_tool()
result = pii_filter_tool.run("Jan Novak, rodne cislo 8001011234")This is useful when your agent must clean notes or tool outputs before passing them to a model.
Wrap chain inputs and outputs
from fastpii import PrivacyGuard
guard = PrivacyGuard(regions=["cz"])
def sanitize_chain_input(text: str) -> str:
return guard.anonymize(text)
def sanitize_chain_output(text: str) -> str:
return guard.redact(text)Anonymize before generation when context matters. Redact after generation when responses might echo original PII.
Streaming detection pattern
For streaming apps, buffer tokens into short text windows and run periodic checks.
from fastpii import PrivacyGuard
guard = PrivacyGuard(regions=["cz"])
def sanitize_stream(chunks: list[str]) -> list[str]:
safe_chunks = []
buffer = ""
for chunk in chunks:
buffer += chunk
if len(buffer) >= 200:
safe_chunks.append(guard.redact(buffer))
buffer = ""
if buffer:
safe_chunks.append(guard.redact(buffer))
return safe_chunksThis keeps you from waiting until the full response is complete before enforcing privacy controls.
Deployment checklist
- Preprocess user input with
PIIPreprocessor. - Redact tool outputs before they reach memory.
- Sanitize streamed chunks in batches.
- Log
detect()counts for observability, not raw sensitive values.