Integrations
LangChain
LangChain integration for PII anonymization and preprocessing.
LangChain
FastPII provides LangChain integration through PIIAnonymizer and PIIPreprocessor wrappers, plus a PIIFilterTool for LangChain agents.
Installation
pip install fastpii[langchain]PIIAnonymizer
PIIAnonymizer replaces PII with a fixed placeholder string. Use it as a preprocessing step before sending text to LLMs.
With an explicit engine
from fastpii import FastPII, DEFAULT_PRIORITY
from fastpii.integrations.langchain import PIIAnonymizer
from fastpii.countries.cz import CzechPack
engine = FastPII(priority=DEFAULT_PRIORITY)
engine.register(CzechPack())
anonymizer = PIIAnonymizer(engine=engine)
result = anonymizer.anonymize("Email: jan.novak@example.cz, RČ: 8001011238")
print(result)
# Email: [REDACTED], RČ: [REDACTED]With regions list
from fastpii.integrations.langchain import PIIAnonymizer
anonymizer = PIIAnonymizer(regions=["cz", "pl", "de", "fr"])
result = anonymizer.anonymize("PESEL: 44051401458, Steuer-ID: 86095742719")
print(result)
# PESEL: [REDACTED], Steuer-ID: [REDACTED]Custom replacement
result = anonymizer.anonymize("Email: jan.novak@example.cz", replacement="<PII>")
# Email: <PII>Callable interface
PIIAnonymizer is callable for use in LangChain chains:
# Use as a callable in chains
cleaned = anonymizer("Send this text to the LLM")PIIPreprocessor
PIIPreprocessor transforms PII with different actions: redact, mask, or remove.
With an explicit engine
from fastpii import FastPII, DEFAULT_PRIORITY
from fastpii.integrations.langchain import PIIPreprocessor
from fastpii.countries.cz import CzechPack
engine = FastPII(priority=DEFAULT_PRIORITY)
engine.register(CzechPack())
preprocessor = PIIPreprocessor(engine=engine)
# Redact (default)
result = preprocessor.preprocess("Email: jan.novak@example.cz", action="redact")
# Email: [EMAIL]
# Mask
result = preprocessor.preprocess("Email: jan.novak@example.cz", action="mask")
# Email: *******************
# Remove
result = preprocessor.preprocess("Email: jan.novak@example.cz", action="remove")
# Email:With regions list
from fastpii.integrations.langchain import PIIPreprocessor
preprocessor = PIIPreprocessor(regions=["cz", "pl"])
result = preprocessor.preprocess(text, action="redact")Callable interface
PIIPreprocessor defaults to the redact action when called:
result = preprocessor("Send this text to the LLM")PIIFilterTool
Create a LangChain tool that detects and anonymizes PII:
from fastpii import FastPII, DEFAULT_PRIORITY
from fastpii.integrations.langchain import create_pii_filter_tool
from fastpii.countries.cz import CzechPack
engine = FastPII(priority=DEFAULT_PRIORITY)
engine.register(CzechPack())
tool = create_pii_filter_tool(engine=engine)
# Or with regions
tool = create_pii_filter_tool(regions=["cz", "pl"])
# Use in LangChain agents
from langchain.agents import initialize_agent
agent = initialize_agent(tools=[tool], llm=llm, agent="zero-shot-react-description")The tool provides:
- Name:
pii_filter - Description: Detect and redact PII from text
- Input:
text(string, required) - Output: Anonymized text with PII replaced