Skip to main content

Enterprise-Grade PII Redaction for Pydantic Models.

Project description

GhostPII 👻

Enterprise-Grade PII Redaction for Pydantic.

PyPI version Python CI License Typed

Note: This project is published on PyPI as ghost-pii-pydantic.

GhostPII solves the "Logged Secret" problem once and for all. It provides a smart string proxy that automatically redacts itself when accessed by unsafe contexts (logging, printing, tracebacks) but remains fully functional for your business logic, databases, and APIs.

Features

Feature Description
Auto-Magical Redaction Automatically detects print() and logging calls to mask PII.
Pydantic Native First-class support for Pydantic v2 Annotated types.
Strict Mode Opt-in for 100% redaction everywhere unless explicitly unmasked.
Tainted Memory Operations on PII (like concatenation) stay PII. No accidental leaks.
Context Aware Use unmask_pii() context manager for explicit, safe data access.
Zero-Performance-Cost Optimized stack inspection with fast-fail logic.

Installation

pip install ghost-pii-pydantic

Quick Start

from pydantic import BaseModel, EmailStr
from ghost_pii import PII, unmask_pii

class User(BaseModel):
    name: PII[str]
    email: PII[EmailStr] # Validates as email (via Pydantic), redacts in logs

user = User(name="John Doe", email="john@example.com")

# 1. Safe by Default: Redacts in logs/prints
print(user)
# Output: name=GhostString('[REDACTED]') email=GhostString('[REDACTED]')

# 2. Functional: Works in business logic/DBs
# (Internal calls to user.email return the real string)
db.execute("INSERT INTO users VALUES (?)", [user.email])
# Successfully inserts "john@example.com"

# 3. Explicit: Use context manager for sensitive tasks
with unmask_pii():
    print(user) 
    # Output: name=GhostString('John Doe') email=GhostString('john@example.com')

Advanced Scenarios

Nested Models and Collections

GhostPII seamlessly handles nested Pydantic models and lists of PII.

from typing import List
from ghost_pii import PII

class Address(BaseModel):
    street: PII[str]
    city: str

class Organization(BaseModel):
    name: str
    admin_emails: List[PII[EmailStr]]
    headquarters: Address

org = Organization(
    name="Acme Corp",
    admin_emails=["admin@acme.com", "sec@acme.com"],
    headquarters=Address(street="123 Secret Lane", city="New York")
)

print(org.model_dump())
# Output: {
#   'name': 'Acme Corp', 
#   'admin_emails': ['[REDACTED]', '[REDACTED]'], 
#   'headquarters': {'street': '[REDACTED]', 'city': 'New York'}
# }

Tainted Memory (Concatenation)

PII "infects" any string it touches. If you combine a PII field with a normal string, the result is a new GhostString that is also redacted by default.

labeled_name = "User: " + user.name
print(labeled_name) # Output: [REDACTED]

with unmask_pii():
    print(labeled_name) # Output: User: John Doe

Enterprise Strategy

GhostPII is designed to adapt to different compliance levels:

Mode Recommended For Mechanism
Auto-Magical General microservices, high developer velocity. Uses stack inspection to detect logging, print, etc.
Strict Mode FinTech, HealthTech, High-Compliance environments. Redacts everywhere. Requires explicit unmask_pii() to access data.

Enabling Strict Mode

from ghost_pii import set_strict_mode

set_strict_mode(True) # Best practice for production PII handling

Contributing

We follow strict engineering standards. Please ensure you run linters and tests before submitting PRs.

pip install -e ".[dev]"
pytest                        # run test suite
ruff check src/ghost_pii      # lint
mypy src/ghost_pii            # type-check

License

This project is licensed under the Apache License 2.0 - see the LICENSE file for details.

Copyright (c) 2026 Sthitaprajna Sahoo and contributors.

Project details


Download files

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

Source Distribution

ghost_pii_pydantic-0.1.2.tar.gz (12.9 kB view details)

Uploaded Source

Built Distribution

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

ghost_pii_pydantic-0.1.2-py3-none-any.whl (11.2 kB view details)

Uploaded Python 3

File details

Details for the file ghost_pii_pydantic-0.1.2.tar.gz.

File metadata

  • Download URL: ghost_pii_pydantic-0.1.2.tar.gz
  • Upload date:
  • Size: 12.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for ghost_pii_pydantic-0.1.2.tar.gz
Algorithm Hash digest
SHA256 1b8f609c1c26510d5193728c9798a8f3f9bdc5a18e772fc2b37405d25d781c88
MD5 44867bb503d2931983979b2b6c4ce4fb
BLAKE2b-256 bf4f07960967823c902fe14e31ea75d0b7aa99e3d4eae41751097ed37c842838

See more details on using hashes here.

Provenance

The following attestation bundles were made for ghost_pii_pydantic-0.1.2.tar.gz:

Publisher: release.yml on STHITAPRAJNAS/ghost-pii-pydantic

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

File details

Details for the file ghost_pii_pydantic-0.1.2-py3-none-any.whl.

File metadata

File hashes

Hashes for ghost_pii_pydantic-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 317a09b0bbd863a7b23eb6154a14c61beba8fb0844179f5d35a14817a3ef59bb
MD5 e8dfaa9d03fcfd9f483f27743c779e8e
BLAKE2b-256 9df5f5d342d86606d853d07344c56f21ad29dcf114252103708e4befb3625459

See more details on using hashes here.

Provenance

The following attestation bundles were made for ghost_pii_pydantic-0.1.2-py3-none-any.whl:

Publisher: release.yml on STHITAPRAJNAS/ghost-pii-pydantic

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

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page