Skip to main content

maskflow-langchain

MaskFlow for LangChain: a reversible PII anonymizer / deanonymizer pair that drops in for langchain-experimental's Presidio anonymizer, plus a leak-guard callback.

It runs MaskFlow's detection engine, so alongside the usual PII (email, phone, card numbers, ...) it covers the Indian identifiers most tools miss: Aadhaar, PAN, GSTIN, UPI VPA, IFSC, ABHA, Indian mobile / PIN code / voter ID / passport / driving licence / vehicle registration, and Indian names and addresses.

  • Drop-in. Same method names and mapping shapes as PresidioReversibleAnonymizer, so migrating a chain is one import line.
  • Streaming. anonymizer.deanonymizer is a streaming-aware Runnable; a placeholder split across two streamed chunks is stitched back before the caller sees it. (Presidio's RunnableLambda(deanonymize) only fires on the final string.)
  • Leak guard. An optional callback that fails a call closed if a prompt still contains PII.
  • MIT, no gates, no telemetry.

Install

pip install maskflow-langchain
pip install "maskflow-langchain[yaml]"   # if you save/load mappings as .yaml

langchain-core is a real dependency (>=0.3,<2). The first detection run downloads a small spaCy model for the name/address recognizers; pass patterns_only=True to skip it.

Migrating from the Presidio anonymizer

# from langchain_experimental.data_anonymizer import PresidioReversibleAnonymizer
from maskflow_langchain import MaskflowReversibleAnonymizer as PresidioReversibleAnonymizer

Everything a chain touches keeps working: .anonymize(text, language=None, allow_list=None), .deanonymize(text, strategy=exact_matching_strategy), .reset_deanonymizer_mapping(), .deanonymizer_mapping, .anonymizer_mapping, .save_deanonymizer_mapping(path), .load_deanonymizer_mapping(path).

Two methods differ, because Presidio recognizer and operator objects have no MaskFlow equivalent:

Presidio maskflow-langchain
add_recognizer(recognizer_obj) add_recognizer(entity_type=..., regex=..., base_confidence=0.6)
add_operators({e: OperatorConfig(...)}) add_operators({e: "replace"|"redact"|"mask"|"hash"|"surrogate"})

allow_list is passed to the constructor on the reversible anonymizer (the session is built once); a differing per-call allow_list raises.

Use it in a chain

from langchain_core.output_parsers import StrOutputParser
from langchain_core.prompts import ChatPromptTemplate
from maskflow_langchain import MaskflowReversibleAnonymizer

anonymizer = MaskflowReversibleAnonymizer()
prompt = ChatPromptTemplate.from_template("Answer: {question}")

chain = (
    {"question": lambda x: anonymizer.anonymize(x["question"])}
    | prompt
    | llm
    | StrOutputParser()
    | anonymizer.deanonymizer  # streaming-aware
)

chain.invoke({"question": "Is PAN ABCPE1234F valid for a salaried filer?"})
# the LLM sees "<PAN_1>"; you get "ABCPE1234F" back
for piece in chain.stream({"question": "Confirm receipt of PAN ABCPE1234F"}):
    print(piece, end="")  # deanonymized incrementally

Leak-guard callback

from maskflow_langchain import MaskflowLeakGuardCallback

guard = MaskflowLeakGuardCallback(raise_on_prompt_pii=True)
chain.invoke(x, config={"callbacks": [guard]})
# raises MaskflowPIILeakError if a prompt reaching the LLM still has PII

guard.summary()  # {"prompt": {"PAN": 0}, "completion": {...}} -- counts only, never values

Callbacks cannot rewrite prompts, so this does not mask; it audits (entity types and counts, never values) and, with raise_on_prompt_pii=True, aborts a call that would leak.

PII safety

No original value is written to logs, repr, callback state, or a saved mapping's structure beyond what you explicitly persist with save_deanonymizer_mapping (which, like Presidio's, contains the real values -- treat that file as sensitive).

See docs/langchain.md in the MaskFlow repo for design notes.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

maskflow_langchain-0.1.0.tar.gz (14.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

maskflow_langchain-0.1.0-py3-none-any.whl (13.6 kB view details)

Uploaded Python 3

File details

Details for the file maskflow_langchain-0.1.0.tar.gz.

File metadata

  • Download URL: maskflow_langchain-0.1.0.tar.gz
  • Upload date:
  • Size: 14.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for maskflow_langchain-0.1.0.tar.gz
Algorithm Hash digest
SHA256 f17ce622c42fb03f359e14fd3b2bee807bddf786d7b2797cdff36beb229944b2
MD5 1214f2471a3f6ece69c64ff2e737806e
BLAKE2b-256 6d69fcafab72e1fc273dd796574789fe15b89101002feef863d3f467a77113b7

See more details on using hashes here.

Provenance

The following attestation bundles were made for maskflow_langchain-0.1.0.tar.gz:

Publisher: release-langchain.yml on maskflow/maskflow

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file maskflow_langchain-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for maskflow_langchain-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e2a12742cf4fc8e4b30bb0c527338393dc86a9126db81a9cba247c80dc980da0
MD5 d6c5ca1df7e8d061143365ac645f2b02
BLAKE2b-256 964d70ae32eb5b0945e82c9c5a765f25eb86cf425fc88af8c12789bc9aa110c2

See more details on using hashes here.

Provenance

The following attestation bundles were made for maskflow_langchain-0.1.0-py3-none-any.whl:

Publisher: release-langchain.yml on maskflow/maskflow

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Release history Release notifications | RSS feed

0.1.1

2 files

This release

0.1.0 This release

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page