pii.py — langchain Source File
Architecture documentation for pii.py, a python file in the langchain codebase. 7 imports, 0 dependents.
Entity Profile
Dependency Diagram
graph LR aefdf72c_7e75_deca_b9e2_b8ab9a265f61["pii.py"] 8e2034b7_ceb8_963f_29fc_2ea6b50ef9b3["typing"] aefdf72c_7e75_deca_b9e2_b8ab9a265f61 --> 8e2034b7_ceb8_963f_29fc_2ea6b50ef9b3 d758344f_537f_649e_f467_b9d7442e86df["langchain_core.messages"] aefdf72c_7e75_deca_b9e2_b8ab9a265f61 --> d758344f_537f_649e_f467_b9d7442e86df 91721f45_4909_e489_8c1f_084f8bd87145["typing_extensions"] aefdf72c_7e75_deca_b9e2_b8ab9a265f61 --> 91721f45_4909_e489_8c1f_084f8bd87145 08032485_971c_6d1d_52b0_b98f4a61b6bd["langchain.agents.middleware._redaction"] aefdf72c_7e75_deca_b9e2_b8ab9a265f61 --> 08032485_971c_6d1d_52b0_b98f4a61b6bd 50acc543_e5f0_2162_cf07_c2bf50723e0c["langchain.agents.middleware.types"] aefdf72c_7e75_deca_b9e2_b8ab9a265f61 --> 50acc543_e5f0_2162_cf07_c2bf50723e0c cfe2bde5_180e_e3b0_df2b_55b3ebaca8e7["collections.abc"] aefdf72c_7e75_deca_b9e2_b8ab9a265f61 --> cfe2bde5_180e_e3b0_df2b_55b3ebaca8e7 5dbfd558_f2f2_9663_3a3f_c317926ac1c1["langgraph.runtime"] aefdf72c_7e75_deca_b9e2_b8ab9a265f61 --> 5dbfd558_f2f2_9663_3a3f_c317926ac1c1 style aefdf72c_7e75_deca_b9e2_b8ab9a265f61 fill:#6366f1,stroke:#818cf8,color:#fff
Relationship Graph
Source Code
"""PII detection and handling middleware for agents."""
from __future__ import annotations
from typing import TYPE_CHECKING, Any, Literal
from langchain_core.messages import AIMessage, AnyMessage, HumanMessage, ToolMessage
from typing_extensions import override
from langchain.agents.middleware._redaction import (
PIIDetectionError,
PIIMatch,
RedactionRule,
ResolvedRedactionRule,
apply_strategy,
detect_credit_card,
detect_email,
detect_ip,
detect_mac_address,
detect_url,
)
from langchain.agents.middleware.types import (
AgentMiddleware,
AgentState,
ContextT,
ResponseT,
hook_config,
)
if TYPE_CHECKING:
from collections.abc import Callable
from langgraph.runtime import Runtime
class PIIMiddleware(AgentMiddleware[AgentState[ResponseT], ContextT, ResponseT]):
"""Detect and handle Personally Identifiable Information (PII) in conversations.
This middleware detects common PII types and applies configurable strategies
to handle them. It can detect emails, credit cards, IP addresses, MAC addresses, and
URLs in both user input and agent output.
Built-in PII types:
- `email`: Email addresses
- `credit_card`: Credit card numbers (validated with Luhn algorithm)
- `ip`: IP addresses (validated with stdlib)
- `mac_address`: MAC addresses
- `url`: URLs (both `http`/`https` and bare URLs)
Strategies:
- `block`: Raise an exception when PII is detected
- `redact`: Replace PII with `[REDACTED_TYPE]` placeholders
- `mask`: Partially mask PII (e.g., `****-****-****-1234` for credit card)
- `hash`: Replace PII with deterministic hash (e.g., `<email_hash:a1b2c3d4>`)
Strategy Selection Guide:
| Strategy | Preserves Identity? | Best For |
// ... (317 more lines)
Domain
Subdomains
Functions
Classes
Dependencies
- collections.abc
- langchain.agents.middleware._redaction
- langchain.agents.middleware.types
- langchain_core.messages
- langgraph.runtime
- typing
- typing_extensions
Source
Frequently Asked Questions
What does pii.py do?
pii.py is a source file in the langchain codebase, written in python. It belongs to the CoreAbstractions domain, RunnableInterface subdomain.
What functions are defined in pii.py?
pii.py defines 1 function(s): collections.
What does pii.py depend on?
pii.py imports 7 module(s): collections.abc, langchain.agents.middleware._redaction, langchain.agents.middleware.types, langchain_core.messages, langgraph.runtime, typing, typing_extensions.
Where is pii.py in the architecture?
pii.py is located at libs/langchain_v1/langchain/agents/middleware/pii.py (domain: CoreAbstractions, subdomain: RunnableInterface, directory: libs/langchain_v1/langchain/agents/middleware).
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free