A high-performance, low-latency Data Loss Prevention (DLP) API Gateway engineered to intercept, sanitize, and manage Personally Identifiable Information (PII) flowing between enterprise clients and Large Language Models (LLMs).
Built on top of FastAPI, Pydantic v2, and Microsoft Presidio, the proxy enforces data sovereignty and compliance rules by stripping sensitive tokens on the outbound path, caching secure mapping topologies in Redis, and symmetrically reconstructing payloads on the inbound response path.
The proxy executes a tight, synchronous request-response sanitization lifecycle combined with non-blocking, asynchronous telemetry logging to maintain an ultra-low latency ceiling:
- Inbound Extraction & Sanitization: Iterates over inbound
ChatCompletionRequestpayloads, isolating user messages. Thesanitization_servicescans text for PII entities, maps them to anonymized Presidio placeholders, and aggregates token tracking metadata. - Reverse Mapping (Hot Cache Storage): Token mappings are stored temporarily in a hot Redis cache tier using strict
TOKEN_TTLrules. - Upstream Execution & Interception: The sanitized payload is evaluated against the upstream model configuration. The generated assistant response contains placeholder sequences instead of raw credentials.
- Symmetric Payload Rehydration: Loops through assistant choices, scanning the output text to map the placeholder keys back to their original sensitive values using hot lookups from Redis.
- Decoupled Audit Hand-Off: Computes total execution latency (
latency_ms) and dispatches anonymized metadata summaries to a background worker thread to register permanent auditing histories in PostgreSQL.
sequenceDiagram
autonumber
actor Client as Enterprise Client
participant Proxy as FastAPI Gateway
participant Redis as Redis (Hot Token Cache)
participant LLM as Upstream LLM Engine
participant DB as PostgreSQL (Audit Database)
%% Step 1: Inbound Processing
Client->>Proxy: POST /proxy/v1/chat/completions (Raw PII payload)
activate Proxy
Note over Proxy: Loop over user messages and sanitize detected PII
Proxy->>Redis: Persist Token Mappings (TTL Enforced)
Redis-->>Proxy: Token Map Cached Successfully
%% Step 2: Upstream Generation
Proxy->>LLM: Dispatch Anonymized Message Payloads
LLM-->>Proxy: Return Assistant Response (With Placeholders)
%% Step 3: Symmetric De-anonymization
Note over Proxy: Loop over assistant choices and rehydrate response text
Proxy->>Redis: GET original values via Request ID
Redis-->>Proxy: Return Decrypted Values
Note over Proxy: Rehydrate Response Text
%% Step 4: Complete Round-trip & Dispatch Async Telemetry
Proxy-->>Client: Return Rehydrated JSON Payload
%% The following block represents the non-blocking background task.
%% It is displayed as sequential in this diagram but executes in a different thread.
Note over Proxy, DB: Non-blocking Background Tasks Thread Log Request
Proxy->>DB: audit_service.log_request(Latency, Summaries, Status)
deactivate Proxy
- Framework Core: Python
>=3.13| FastAPI - Validation & Type-Safety: Pydantic v2 | Pydantic Settings
- DLP Processing Engines: Microsoft Presidio Analyzer & Anonymizer
- Hot Caching Subtier: Redis Server (High-throughput storage for request-response state lookups)
- Persistent Analytics Database: PostgreSQL Engine (Saves anonymized audit log schemas)
- Package Management Engine:
uv(Fast dependency resolution tool)
This project leverages the uv workspace manager for ultra-fast dependency tracking and explicit environment orchestration.
Clone your codebase and execute uv sync to compile the isolated virtual environment:
git clone https://github.com/prthm2910/PII-Secure-Guardrail-Proxy.git
cd pii-secure-guardrail-proxy
# Build virtual environment and sync exact pinned locking structures
uv venv
uv sync
The underlying Microsoft Presidio Analyzer engine requires targeted spaCy models to compute accurate Name Entity Recognition (NER) tokens:
uv run python -m spacy download en_core_web_sm
Launch ephemeral container architectures to satisfy the local infrastructure footprint using standard Docker configurations:
# Run local Redis caching image
docker run -d --name pii-cache-tier -p 6379:6379 redis:alpine
# Run local PostgreSQL data engine
docker run -d --name pii-audit-db -e POSTGRES_USER=postgres -e POSTGRES_PASSWORD=local_secure_password -e POSTGRES_DB=pii_audit_ledger -p 5432:5432 postgres:alpine
Spin up your server workspace natively via the configured main.py entry point:
uv run python main.py
Validate the configuration modules and runtime stability profiles:
curl -X GET http://localhost:8000/health
Response Output:
{
"status": "healthy",
"project": "PII Secure Guardrail Proxy",
"version": "0.1.0"
}
Submit structured data arrays directly through the proxy router:
curl -X POST http://localhost:8000/proxy/v1/chat/completions \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-4o",
"messages": [
{
"role": "user",
"content": "My private email address is user@example.com."
}
]
}'
- Outbound State Post-Sanitization:
"My private email address is <EMAIL_ADDRESS>." - Inbound Trace Received From LLM Engine:
"I received your request. The PII you mentioned has been tokenized. One of the tokens is <EMAIL_ADDRESS>." - Rehydrated Final Response Output Delivered to Client: See payload structure below.
Resulting Gateway Delivery:
{
"choices": [
{
"message": {
"role": "assistant",
"content": "I received your request. The PII you mentioned has been tokenized. One of the tokens is user@example.com."
}
]
}
}
This markdown precisely mirrors your exact code layers, including main.py's launch pattern and the conditional structure inside your custom mock loop. It's a clean plug-and-play profile piece. What file or module are we building or tuning next?