FastPII Docs
Integrations

LangChain

Use FastPII with LangChain to anonymize or preprocess text before it reaches models and external services.

LangChain integration

FastPII provides LangChain-friendly wrappers for anonymization, preprocessing, and a reusable tool for filtering PII from text.

Installation

pip install fastpii[langchain]

Setup

Available imports:

from fastpii.integrations.langchain import (
    PIIAnonymizer,
    PIIPreprocessor,
    create_pii_filter_tool,
)

Usage examples

Anonymize text

from fastpii.integrations.langchain import PIIAnonymizer

anonymizer = PIIAnonymizer(regions=["cz"])

text = "Jan Novak, rodne cislo 8001011234"
clean_text = anonymizer.anonymize(text)
custom_text = anonymizer.anonymize(text, replacement="[PII]")
same_as_anonymize = anonymizer(text)

Preprocess text with different actions

from fastpii.integrations.langchain import PIIPreprocessor

preprocessor = PIIPreprocessor(regions=["cz"])

text = "Jan Novak, rodne cislo 8001011234"

redacted = preprocessor.preprocess(text, action="redact")
masked = preprocessor.preprocess(text, action="mask")
removed = preprocessor.preprocess(text, action="remove")
default_result = preprocessor(text)

Create a LangChain tool

from fastpii.integrations.langchain import create_pii_filter_tool

tool = create_pii_filter_tool()

result = tool.run("Jan Novak, rodne cislo 8001011234")

The created tool exposes the name pii_filter and supports configurable regions.

API reference

PIIAnonymizer

PIIAnonymizer(regions=None)

Methods:

  • anonymize(text, replacement="[REDACTED]") -> str
  • __call__(text) -> str

PIIPreprocessor

PIIPreprocessor(regions=None)

Methods:

  • preprocess(text, action="redact") -> str
  • __call__(text) -> str

Supported action values:

  • redact
  • mask
  • remove

If action is not one of those values, preprocess() raises ValueError.

create_pii_filter_tool()

create_pii_filter_tool() -> object

Returns a LangChain BaseTool instance with:

  • name = "pii_filter"
  • region configuration via the tool's regions field
  • synchronous and async execution through _run() and _arun()

Tool behavior

The generated tool redacts PII from input text before passing it downstream. Its description is:

Detect and redact PII (Personally Identifiable Information) from text. Use before sending user input to external services or LLMs.

On this page