langchain-data-handling
LangChain Data Handling
Contents
Overview
Best practices for handling sensitive data, PII protection, and compliance in LangChain applications including detection, masking, retention, consent, and audit logging.
Prerequisites
- Understanding of data privacy regulations (GDPR, CCPA)
- LangChain application processing user data
- Data classification framework
Instructions
Step 1: Detect and Mask PII
Build a PIIDetector class with regex patterns for email, phone, SSN, credit card, IP address, and date of birth. Implement detect(), mask(), and redact() methods.
Step 2: Build Privacy Pipeline
Wrap chains with PII protection using RunnableLambda preprocessing that redacts PII before sending to the LLM.
Step 3: Enforce Data Retention
Implement DataRetentionManager with configurable retention periods, auto-cleanup of expired interactions, and GDPR right-to-erasure support.
Step 4: Manage Consent
Build ConsentManager with consent types (LLM processing, retention, analytics, training) and decorator-based consent enforcement.
Step 5: Enable Audit Logging
Create AuditLogger and AuditCallback to log all LLM calls with user ID, model, token count, and PII detection status.
See detailed implementation for complete PII detector, privacy pipeline, and compliance code.
Output
- PII detection and masking
- Privacy-wrapped chain pipeline
- Data retention management
- Consent management system
- Audit logging
Error Handling
| Issue | Cause | Solution |
|---|---|---|
| PII not detected | Missing pattern | Add regex to PIIPattern list |
| Retention not enforced | Cleanup not scheduled | Add cron job for cleanup |
| Consent check failed | User not registered | Create consent record first |
Examples
Basic usage: Apply langchain data handling to a standard project setup with default configuration options.
Advanced scenario: Customize langchain data handling for production environments with multiple constraints and team-specific requirements.
Resources
Next Steps
Use langchain-security-basics for additional security measures.