Skip to main content

spanguard

Redact PII/PHI from text and streams — with an audit trail that can never leak the data it's auditing.

CI PyPI Coverage Docs Python Typed License

spanguard detects and redacts PII/PHI (personally identifiable information and protected health information) in text and streaming transcripts. Every Redactor.redact() call returns a RedactionResult whose audit field holds only entity-type counts, a policy hash, and a timestamp — never the raw matched values — so the audit trail itself can never become a second place where sensitive data leaks out. That guarantee is enforced by the test suite, not just documented.

Contents

Why spanguard

  • Leak-free audit record — result.audit carries entity-type counts, a policy hash, and a timestamp only; raw matches never enter it, and tests verify this holds across every mode.
  • Four redaction modes — mask, placeholder, hash (deterministic HMAC), and tokenize (reversible).
  • Reversible tokenization — turn tokens back into the original text with Redactor.reveal() and an in-memory Keyring, scoped to a single process.
  • Built-in detectors — email (Unicode-aware), phone, SSN, MRN, DOB, and Luhn-validated credit card numbers, plus API key patterns.
  • Optional NER — spaCy-backed PERSON/LOCATION detection behind the [ner] extra, off by default so the core install stays light.
  • Streaming-safe — StreamRedactor redacts chunked input (e.g. a live transcript) so the output matches a one-shot redaction, even when an entity straddles a chunk boundary.
  • Pluggable recognizers — register a custom Recognizer for domain-specific entities without forking the library.
  • Typed and tested — mypy --strict and ruff clean, 128 tests, 97.9% coverage.
  • CLI included — spanguard scan / spanguard redact for one-off files and shell pipelines.

Install

pip install spanguard

For the optional NER (PERSON/LOCATION) recognizer:

pip install spanguard[ner]
python -m spacy download en_core_web_sm

NER is opt-in per Redactor: pass the recognizer alongside the built-ins.

from spanguard import Policy, Redactor
from spanguard.recognizers import DEFAULT_RECOGNIZERS, SpacyRecognizer

redactor = Redactor(Policy(), recognizers=[*DEFAULT_RECOGNIZERS.values(), SpacyRecognizer()])

60-second quickstart

from spanguard import Redactor, Policy

text = "Contact: jane@example.com or 555-123-4567."

# --- mask: replace each character with mask_char, preserving length ---
mask_policy = Policy(mode="mask", entities=("EMAIL", "PHONE"))
mask_result = Redactor(mask_policy).redact(text)
print(mask_result.text)
# Contact: ████████████████ or ████████████.
print(mask_result.audit.counts)
# {'EMAIL': 1, 'PHONE': 1}

# --- placeholder: replace with a [TYPE] tag ---
placeholder_policy = Policy(mode="placeholder", entities=("EMAIL", "PHONE"))
placeholder_result = Redactor(placeholder_policy).redact(text)
print(placeholder_result.text)
# Contact: [EMAIL] or [PHONE].

# --- hash: deterministic HMAC digest, reproducible with the same key ---
hash_policy = Policy(mode="hash", entities=("EMAIL", "PHONE"))
hash_redactor = Redactor(hash_policy, hmac_key=b"docs-example-key")
hash_result = hash_redactor.redact(text)
print(hash_result.text)
# Contact: [EMAIL:11d041e6c58a0530] or [PHONE:cbe886551f4e27ed].

# --- tokenize + reveal: reversible via an in-memory keyring ---
tokenize_policy = Policy(mode="tokenize", entities=("EMAIL", "PHONE"))
tokenize_redactor = Redactor(tokenize_policy, tokenize_key=b"docs-example-key")
tokenize_result = tokenize_redactor.redact(text)
print(tokenize_result.text)
# Contact: ⟦EMAIL_11d041e6c58a0530⟧ or ⟦PHONE_cbe886551f4e27ed⟧.

revealed = tokenize_redactor.reveal(tokenize_result.text)
print(revealed == text)
# True

See the Quickstart docs for notes on keys, the audit record, and result.spans.

Streaming

from spanguard import Redactor, Policy, StreamRedactor

redactor = Redactor(Policy(mode="mask", entities=("EMAIL", "PHONE")))
stream = StreamRedactor(redactor)

output = stream.feed("Contact: jane@example.com ") + stream.feed("or 555-123-4567.")
output += stream.flush()

StreamRedactor holds back a tail of the buffer (minimum 64 characters) so an entity split across a chunk boundary is still detected as one; the concatenation of every feed() plus the final flush() is guaranteed to equal a one-shot redactor.redact(full_text).text.

Text is emitted only up to a safe split point (whitespace outside any entity), so a stream with none, such as a long base64 blob, is held in memory. Pass StreamRedactor(redactor, max_buffer=N) to get a StreamBufferFullError instead of unbounded growth. See Streaming.

CLI

spanguard scan file.txt
spanguard redact file.txt --mode mask
spanguard redact file.txt --mode tokenize --out redacted.txt
cat file.txt | spanguard redact - --mode placeholder   # '-' reads stdin

scan reports entity-type counts only (no raw values); redact writes the redacted text and, with --audit-json, the audit record to stderr. Bad input (an unknown --entities name, a missing or non-UTF-8 file) exits with code 2 and a one-line error: message that never echoes input content. See CLI.

Built-in entity table

Entity type Detector Notes
EMAIL regex Unicode local part and domain; requires a dotted TLD of >= 2 letters.
PHONE regex NANP-style 3-3-4, optional +country, -/./space/parens separators.
SSN regex ###-##-####.
MRN regex (context-anchored) Requires an MRN prefix; only the digit run is redacted.
DOB regex mm/dd/yyyy (1- or 2-digit month/day) or ISO yyyy-mm-dd.
CREDIT_CARD regex + Luhn checksum Contiguous 13-19 digits, or standard printed groupings; Luhn-validated.
API_KEY regex Known prefixes (sk-, AKIA, ghp_) or a long high-entropy alnum run.
PERSON, LOCATION spaCy NER (optional [ner] extra) Requires the [ner] extra, a spaCy model, and passing SpacyRecognizer() (see Install).

Four redaction modes are available on every Policy: mask, placeholder, hash, and tokenize (reversible via Redactor.reveal()). See Recognizers for the full detail and how to plug in a custom Recognizer.

Compliance / audit guarantee

result.audit (an AuditRecord) is designed to be safe to log, ship to a SIEM, or hand to an auditor without a second review: it contains entity-type counts, a policy hash (so you can prove which redaction policy ran), and a timestamp — and structurally nothing else. No raw matched value, no span offsets, no context window. The test suite asserts this property directly so a future change that widens the audit record would fail CI, not just review.

Docs

Full documentation, including the API reference, streaming redaction, and the CLI, is at clufy.github.io/spanguard.

Contributing

Bug reports and pull requests are welcome — see CONTRIBUTING.md for how to set up a dev environment and run the test suite.

Security

To report a vulnerability, please see SECURITY.md rather than opening a public issue.

License

MIT — see LICENSE.

Metadata

Release files for spanguard 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for spanguard 0.1.0
File Size Uploaded Provenance Actions
spanguard-0.1.0.tar.gz 129.4 kB With provenance Details for spanguard-0.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for spanguard 0.1.0
File Interpreter ABI Platform Actions
spanguard-0.1.0-py3-none-any.whl Python 3 none any Details for spanguard-0.1.0-py3-none-any.whl

Total release size: 152.4 kB

Release files / spanguard-0.1.0.tar.gz

Download URL spanguard-0.1.0.tar.gz
Size 129.4 kB
Tags Source
SHA-256 checksum
How to use checksums
e61d659a695390143e14d716738983be013ee403157859064db32900bebc5dac
BLAKE2b-256 checksum
How to use checksums
2b56e7966e2424aedd5fd90877ac1442ad32bdfb5d7b1d7ccbf4d976ce63067b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 12, 2026.

Transparency log

Release files / spanguard-0.1.0-py3-none-any.whl

Download URL spanguard-0.1.0-py3-none-any.whl
Size 22.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
28c9a917d69e0c9355fd7c87f6d3d470dde2cdc2b427dbd35d0529f34f795beb
BLAKE2b-256 checksum
How to use checksums
0b3fe9ba781e3391328cb49d032365c7992ea3eaa0ab5727a218b1f4c728a102
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 12, 2026.

Transparency log

Security for spanguard 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for spanguard-0.1.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 12, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files Full provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page