Skip to main content

PromptVeil — Project Summary

1. Executive Summary​

PromptVeil is a Python middleware library (v0.1.0) that intercepts prompts before they reach a Large Language Model (LLM) API, detects sensitive personally identifiable information (PII) and secrets, and applies configurable privacy policies (mask, block, or allow). It produces structured JSON audit logs for compliance without persisting raw sensitive values.

The 0.1.0 release includes confidence-based policy thresholds (per-rule minimum confidence); rich scan results with probabilistic risk scoring (scan_detailed()); jurisdiction compliance profiles (india_dpdpa, eu_gdpr, us_hipaa) loadable by a single YAML key; a reversible pseudonymization vault (scan_with_vault()) that replaces PII with stable <TYPE_N> tokens restorable after LLM response; and framework-native adapters for OpenAI (wrap_openai) and LangChain (get_langchain_callback). US-specific PII detection (SSN, US Passport, EIN) has been added via USDetector.


2. Architecture Overview​

High-Level Architecture

PromptVeil sits between the caller application and the LLM API. The caller invokes Shield.scan(prompt), which runs the detection pipeline, evaluates results against a YAML-driven policy, applies masking or blocking, emits a structured JSON audit log to stdout, and returns the sanitised string. The optional PromptVeilMiddleware wraps FastAPI/Starlette applications to intercept POST request bodies transparently.

System Boundaries​

BoundaryDescription
Caller ApplicationAny Python app, FastAPI service, notebook, or CLI script that calls Shield.scan()
PromptVeilCore library — detection, policy, actions, logging (this project)
LLM APIDownstream target: OpenAI, Anthropic, Azure OpenAI, etc. (not part of this project)
policy.yamlYAML configuration file that drives the policy rules (supports profile: key)
Audit Log (stdout)Structured JSON lines emitted for each scan event; raw values never stored
PseudonymVaultIn-process session store for reversible <TYPE_N> tokens
Framework AdaptersThin wrappers for OpenAI SDK and LangChain that intercept calls transparently

3. Processing Pipeline​

Processing Pipeline

Shield.scan(prompt) executes the following steps in strict order:

Step-by-Step Flow​

Step 1 — Input Validation Shield.scan() receives a prompt: str. If the argument is not a string, a TypeError is raised immediately before any processing begins.

Step 2 — Entity Detection All registered BaseDetector instances are run against the prompt in sequence. Each detector returns a List[DetectedEntity]. Each entity now carries a confidence: float (default 1.0) representing detection certainty. Each detector removes overlaps within its own matches, and Shield collects the matches from all detectors without merging them, so a span found by two detectors (for example phone and in_phone) is still evaluated under both rules. Overlaps across detectors are resolved later, in Step 5. If no entities are found, the original prompt is returned unchanged.

Step 3 — Policy Evaluation PolicyEngine.evaluate(entities) maps each detected entity to an Action (MASK, BLOCK, or ALLOW) by looking up the entity type in the rule map. If the entity's confidence is below the rule's min_confidence threshold, the rule is skipped and default_action applies. The result is a Dict[DetectedEntity, Action].

Step 4 — Block (Fail-Fast) Before any masking occurs, the engine iterates through evaluated entities and checks for any BLOCK actions. The first BLOCK entity triggers AuditLogger.log_blocked() (records entity type, no raw value) and then block() raises a SensitiveDataError immediately. The prompt is never forwarded to the LLM.

Step 5 — Apply Masking Entities resolved to MASK are collected, and Shield._select_masks() drops overlapping spans among them (the longest span wins; the first-registered detector wins ties) so no text is replaced twice. apply_masks(prompt, to_mask) rebuilds the string by replacing each sensitive span with a type-aware redacted value from mask_value(). Replacements are applied right-to-left by position so earlier offsets remain valid.

Step 6 — Audit Logging AuditLogger.log_scan_result() writes a JSON line to stdout recording the event: timestamp, prompt length, number of detections, entity types, actions applied, and masked previews. Raw values are never logged.

Step 7 — Return The sanitised string is returned to the caller, ready to forward to the LLM API.

FastAPI Middleware Flow​

When PromptVeilMiddleware is configured, it wraps dispatch() around every POST request with application/json content. It decodes the body, extracts the configured prompt_field, calls shield.scan(), and either rewrites the request with the sanitised value (mask path) or returns a 400 Bad Request JSON response immediately (block path). Non-JSON bodies and non-POST requests are forwarded untouched.


4. Core Components​

Component Relationships

Detection Layer​

Class / SymbolFileResponsibility
BaseDetector (ABC)detector/base.pyAbstract base; defines detect(text) → List[DetectedEntity] and supported_types
DetectedEntity (dataclass)detector/base.pyImmutable record: entity_type, value, start, end, confidence (default 1.0)
RegexDetectordetector/regex_detector.pyCompiled regex patterns for email, phone, credit_card, api_key; overlap resolution
IndiaDetectordetector/india_detector.pyIndia-specific PII: in_aadhaar (Verhoeff checksum), in_pan, in_phone, in_passport, in_voter_id, in_ifsc, in_gstin
USDetectordetector/us_detector.pyUS-specific PII: ssn (segment-validated), us_passport (1 alpha + 8 digits), us_ein (XX-XXXXXXX)

Policy Layer​

Class / SymbolFileResponsibility
Action (StrEnum)policy/models.pyValues: MASK, BLOCK, ALLOW
Rule (dataclass)policy/models.pyMaps type: str → action: Action; optional min_confidence: float (default 0.0)
PolicyConfig (dataclass)policy/models.pyHolds rules: List[Rule], default_action, version
PolicyEnginepolicy/engine.pyO(1) rule lookup; confidence-aware get_action(), evaluate()
load_config()config/loader.pyReads and validates YAML → PolicyConfig; rejects unknown actions
load_config_with_profile()config/loader.pyExtends load_config with profile: YAML key support; returns (PolicyConfig, detectors∨None)

Actions Layer​

FunctionFileResponsibility
apply_masks(text, entities)actions/masker.pyRebuilds prompt with all masked spans substituted
mask_value(value, entity_type)actions/masker.pyDispatches to per-type masker (email: j***@domain.com, phone: ***-***-5309, card: ****-****-****-1111, api_key: sk-a...7890)
block(entity)actions/blocker.pyAlways raises SensitiveDataError

Infrastructure Layer​

ClassFileResponsibility
AuditLoggerlogger/audit_logger.pyStructured JSON lines to stdout; raw values never stored
PromptVeilMiddlewaremiddleware/fastapi_middleware.pyStarlette BaseHTTPMiddleware; intercepts POST bodies, rewrites or blocks
Shieldshield.pyCentral orchestrator: wires detection → policy → actions → logging; exposes scan, scan_detailed, scan_with_vault
SensitiveDataErrorexceptions.pyRaised on block; carries entity_type for caller error handling
ConfigurationErrorexceptions.pyRaised on invalid Shield construction or bad YAML

Results Layer (new in v0.1.0)​

Class / SymbolFileResponsibility
ScanResult (dataclass)results.pyFrozen: safe_text, entities, risk_score, risk_level, was_mutated
EntityResult (dataclass)results.pyPer-entity: entity_type, action, start, end, masked_value, confidence
_compute_risk_score()results.pyProbabilistic union of per-entity weights; confidence and action both scale contribution
_risk_level()results.pyThresholds: NONE=0.0, LOW<0.30, MEDIUM<0.60, HIGH<0.85, CRITICAL≥0.85

Profiles Layer (new in v0.1.0)​

SymbolFileResponsibility
Profile (dataclass)profiles/base.pyBundles name, policy: PolicyConfig, detectors: List[BaseDetector]
load_profile(name)profiles/__init__.pyFactory; raises ConfigurationError for unknown names
available_profiles()profiles/__init__.pyReturns sorted list of registered profile names
india_dpdpaprofiles/india_dpdpa.pyIndia DPDPA rules + RegexDetector + IndiaDetector
eu_gdprprofiles/eu_gdpr.pyEU GDPR rules + RegexDetector
us_hipaaprofiles/us_hipaa.pyUS HIPAA rules + RegexDetector + USDetector

Vault & Adapters (new in v0.1.0)​

Class / SymbolFileResponsibility
PseudonymVaultvault.pyThread-safe; mints <TYPE_N> tokens; pseudonymize(), restore(), get_token(), clear()
wrap_openai(client, shield, vault=None)adapters/openai_adapter.pyPatches client.chat.completions.create in-place; optional vault support
get_langchain_callback()adapters/langchain_adapter.pyFactory returning a BaseCallbackHandler class; implements on_llm_start + on_chat_model_start

5. API Contracts and Message Schemas​

Shield Constructor​

ParameterTypeDefaultDescription
config_pathstr | PathNonePath to a YAML policy file
configPolicyConfigNonePre-built config object (alternative to config_path)
detectorsList[BaseDetector]NoneExplicit detector list; overrides profile detectors when supplied
loggerAuditLoggerAuditLogger()Custom audit logger

Exactly one of config_path or config must be provided. When using config_path with a profile: YAML key and no explicit detectors, the profile’s detector list is activated automatically.

Shield.scan(prompt) / Shield.scan_response(response)​

ParameterTypeDescription
promptstrRaw input text to evaluate

Returns: str — Sanitised string with sensitive values masked.
Raises: SensitiveDataError if a block rule fires; TypeError if prompt is not a string.

Shield.scan_detailed(prompt) (new in v0.1.0)​

Same pipeline as scan(), but returns a ScanResult instead of a plain string.

FieldTypeDescription
safe_textstrSanitised prompt
entitiestuple[EntityResult]Per-entity detail (type, action, position, masked value, confidence)
risk_scorefloatProbabilistic aggregate risk, 0.0–1.0
risk_levelstrNONE / LOW / MEDIUM / HIGH / CRITICAL
was_mutatedboolTrue if any masking was applied

Shield.scan_with_vault(prompt, vault=None) (new in v0.1.0)​

Replaces each masked entity with a stable <TYPE_N> token. Returns (pseudonymized_str, vault).

ParameterTypeDescription
promptstrRaw input text
vaultPseudonymVault | NoneReuse an existing vault for cross-turn consistency; creates a new one if None

PolicyConfig Schema​

version: "1.0"
default_action: allow # mask | block | allow

# Optional: activate a jurisdiction profile
# profile: india_dpdpa # or eu_gdpr | us_hipaa

rules:
- type: <entity_type> # string label e.g. "email"
action: <action> # mask | block | allow
min_confidence: 0.8 # optional float 0.0–1.0; default 0.0

When profile: is set, its rules are merged first; explicit rules: entries override matching types.

DetectedEntity Schema​

FieldTypeDescription
entity_typestrCategory label, e.g. "email", "in_aadhaar"
valuestrExact matched substring
startintInclusive start index in source text
endintExclusive end index in source text
confidencefloatDetection certainty 0.0–1.0 (default 1.0)

Audit Log JSON Line Schema​

{
"event": "prompt_scan",
"timestamp": "2026-05-05T10:00:00+00:00",
"prompt_length": 42,
"detections": 1,
"entities": [
{
"entity_type": "email",
"action": "mask",
"value_preview": "a***@example.com",
"position": { "start": 12, "end": 27 }
}
]
}

FastAPI Middleware — Block Response​

{
"error": "Blocked: sensitive data detected in prompt.",
"detail": "Sensitive data of type 'credit_card' was detected and blocked by the active policy.",
"code": "SENSITIVE_DATA_BLOCKED"
}

HTTP status: 400 Bad Request.


6. Infrastructure and Deployment​

Package Installation​

# Core (regex detection, YAML policy)
pip install .

# With FastAPI/Starlette middleware
pip install ".[fastapi]"

# Development (includes pytest, pytest-cov)
pip install ".[dev]"

Requires: Python ≥ 3.10.

Dependencies​

PackageVersionCategoryPurpose
PyYAML≥ 6.0CoreYAML policy file loading
fastapi≥ 0.100.0Optional (fastapi)FastAPI framework dependency
starlette≥ 0.27.0Optional (fastapi)ASGI middleware base class
pytest≥ 7.4DevelopmentTest runner
pytest-cov≥ 4.1DevelopmentCoverage reporting

Build System​

ToolVersionRole
setuptools≥ 68Build backend
wheelanyDistribution packaging
pyproject.tomlPEP 517/518Project metadata and dependency spec

Testing​

The project ships 234 pytest tests across 13 test files with coverage for all core modules:

Test FileCoverage Area
tests/test_config.pyYAML loader — valid configs, missing files, invalid actions
tests/test_detector.pyRegex patterns — email, phone, credit card, API key, overlap resolution
tests/test_india_detector.pyIndia-specific PII patterns and Verhoeff Aadhaar checksum
tests/test_masker.pyPer-type masking functions and multi-entity substitution
tests/test_policy.pyPolicyEngine rule matching, default action, evaluation
tests/test_shield.pyEnd-to-end Shield.scan() — allow, mask, block, YAML config, scan_response
tests/test_middleware.pyFastAPI middleware — mask path, block path, non-JSON passthrough
tests/test_confidence.pyDetectedEntity.confidence, Rule.min_confidence, threshold enforcement
tests/test_results.pyScanResult, EntityResult, risk scoring, scan_detailed()
tests/test_profiles.pyProfile registry, structure, YAML activation, end-to-end scans
tests/test_vault.pyPseudonymVault token lifecycle, restore, scan_with_vault() round-trip
tests/test_adapters.pyOpenAI and LangChain adapter integration and error paths

Run tests:

uv run pytest -v
# or with coverage:
uv run pytest --cov=promptveil --cov-report=term-missing

7. Extension Patterns​

Adding a Custom Detector​

  1. Subclass BaseDetector from promptveil.detector.base:
import re
from promptveil.detector.base import BaseDetector, DetectedEntity

class SSNDetector(BaseDetector):
_PATTERN = re.compile(r"\b\d{3}-\d{2}-\d{4}\b")

def detect(self, text: str) -> list[DetectedEntity]:
return [
DetectedEntity("ssn", m.group(), m.start(), m.end())
for m in self._PATTERN.finditer(text)
]

@property
def supported_types(self) -> list[str]:
return ["ssn"]
  1. Register the detector when constructing Shield:
from promptveil import Shield
from promptveil.policy.models import Action, PolicyConfig, Rule

shield = Shield(
config=PolicyConfig(
rules=[Rule(type="ssn", action=Action.MASK)],
default_action=Action.ALLOW,
),
detectors=[SSNDetector()],
)
  1. Or add it at runtime to an existing shield:
shield.add_detector(SSNDetector())

Adding a New Entity Type to RegexDetector​

  1. Open promptveil/detector/regex_detector.py
  2. Add a key and regex pattern list to _RAW_PATTERNS
  3. The pattern is compiled automatically at import time
  4. Add a corresponding masking function to promptveil/actions/masker.py and register it in the _MASKERS dispatch table

Enabling India PII Detection​

Instantiate IndiaDetector alongside RegexDetector and add rules for the India entity types:

from promptveil.detector.india_detector import IndiaDetector
from promptveil.detector.regex_detector import RegexDetector

shield = Shield(
config_path="policy.yaml",
detectors=[RegexDetector(), IndiaDetector()],
)

Uncomment and add rules in policy.yaml for any of: in_aadhaar, in_pan, in_phone, in_passport, in_voter_id, in_ifsc, in_gstin.

Adding FastAPI Middleware​

from fastapi import FastAPI
from promptveil import Shield
from promptveil.middleware import PromptVeilMiddleware

app = FastAPI()
shield = Shield(config_path="policy.yaml")
app.add_middleware(PromptVeilMiddleware, shield=shield, prompt_field="prompt")

prompt_field defaults to "prompt" — change it to match your request body schema.

Custom Audit Logger​

Subclass or wrap AuditLogger from promptveil.logger.audit_logger and pass it as the logger parameter to Shield. The logger receives log_scan_result() and log_blocked() calls.


8. Rules and Anti-Patterns​

Rules (Do)​

RuleRationale
Always provide either config_path or config — never bothThe Shield constructor enforces mutual exclusivity
Catch SensitiveDataError at the call siteBlock events raise and do not return; unhandled errors will propagate to the user
Use scan_response() for LLM output as well as inputLLMs can reproduce or hallucinate PII from training data
List detectors in priority orderWhen detectors report the same span, the longest span is masked and the first-registered detector wins ties; block rules are still checked against every match
Keep policy.yaml under version controlPolicy changes are security-relevant; treat them like code
Use structured audit log fields (entity_type, action) for alertingNever parse free-text log lines for security events

Anti-Patterns (Do Not)​

Anti-PatternConsequence
Logging entity.value directlyPersists raw PII/secrets in log storage
Setting default_action: block without specific allow rulesWill block all prompts that contain any detected entity
Passing non-string objects to scan()Raises TypeError — validate upstream before calling
Sharing a Shield instance across threads without synchronisation_detectors list is mutable; add detectors only at setup time
Disabling the block action for credit_card or api_keyThese entity types are high-risk; masking is insufficient
Relying solely on regex detection for adversarial inputsRegex can be evaded; combine with ML-based detectors for higher assurance

9. Dependencies​

Core Dependencies​

PackageMin VersionLicensePurpose
PyYAML6.0MITParse policy.yaml policy files

Optional Dependencies​

ExtraPackageMin VersionPurpose
[fastapi]fastapi0.100.0FastAPI route integration
[fastapi]starlette0.27.0BaseHTTPMiddleware base class
[openai]openai1.0.0OpenAI SDK adapter
[langchain]langchain-core0.1.0LangChain callback adapter

Development Dependencies​

PackageMin VersionPurpose
pytest7.4Test runner
pytest-cov4.1Code coverage reporting

10. Code Structure​

PromptVeil/
├── promptveil/ # Main package
│ ├── __init__.py # Public API: Shield, ScanResult, EntityResult, PseudonymVault, exceptions
│ ├── shield.py # Shield — central orchestrator (scan, scan_detailed, scan_with_vault)
│ ├── exceptions.py # SensitiveDataError, ConfigurationError, PromptVeilError
│ ├── results.py # ScanResult, EntityResult, risk scoring
│ ├── vault.py # PseudonymVault — thread-safe reversible pseudonymization
│ ├── config/
│ │ ├── __init__.py
│ │ └── loader.py # load_config(), load_config_with_profile() — YAML → PolicyConfig
│ ├── detector/
│ │ ├── __init__.py
│ │ ├── base.py # BaseDetector (ABC), DetectedEntity (confidence field)
│ │ ├── regex_detector.py # RegexDetector — email, phone, credit_card, api_key
│ │ ├── india_detector.py # IndiaDetector — Aadhaar (Verhoeff), PAN, GSTIN, etc.
│ │ └── us_detector.py # USDetector — SSN, US Passport, EIN
│ ├── profiles/
│ │ ├── __init__.py # load_profile(), available_profiles() registry
│ │ ├── base.py # Profile dataclass
│ │ ├── india_dpdpa.py # India DPDPA compliance pack
│ │ ├── eu_gdpr.py # EU GDPR compliance pack
│ │ └── us_hipaa.py # US HIPAA compliance pack
│ ├── adapters/
│ │ ├── __init__.py # wrap_openai(), get_langchain_callback()
│ │ ├── openai_adapter.py # Patches openai.Client.chat.completions.create
│ │ └── langchain_adapter.py # LangChain BaseCallbackHandler factory
│ ├── policy/
│ │ ├── __init__.py
│ │ ├── models.py # Action (enum), Rule, PolicyConfig (dataclasses)
│ │ └── engine.py # PolicyEngine — O(1) rule lookup + evaluate()
│ ├── actions/
│ │ ├── __init__.py
│ │ ├── masker.py # mask_value(), apply_masks() — type-aware redaction
│ │ └── blocker.py # block() — raises SensitiveDataError
│ ├── logger/
│ │ ├── __init__.py
│ │ └── audit_logger.py # AuditLogger — structured JSON lines, no raw values
│ └── middleware/
│ ├── __init__.py
│ └── fastapi_middleware.py # PromptVeilMiddleware (optional FastAPI extra)
├── tests/ # 234 pytest tests
│ ├── test_config.py
│ ├── test_detector.py
│ ├── test_india_detector.py
│ ├── test_masker.py
│ ├── test_middleware.py
│ ├── test_policy.py
│ └── test_shield.py
├── docs/
│ ├── quickstart.md # Developer quickstart guide
│ ├── project-summary.md # This document
│ └── diagrams/
│ ├── high-level-architecture.drawio # C4 Context diagram (editable)
│ ├── high-level-architecture.drawio.png # Rendered PNG
│ ├── processing-pipeline.drawio # C4 Container diagram
│ ├── processing-pipeline.drawio.png
│ ├── component-relationships.drawio # C4 Component diagram
│ └── component-relationships.drawio.png
├── example_usage.py # Runnable demo — 10 scenarios
├── policy.yaml # Default policy configuration
├── pyproject.toml # Package metadata + dependency spec
└── README.md # Project overview