spanguard
Redact PII/PHI from text and streams — with an audit trail that can never leak the data it's auditing.
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
- Install
- 60-second quickstart
- Streaming
- CLI
- Built-in entity table
- Compliance / audit guarantee
- Docs
- Contributing
- Security
- License
Why spanguard
- Leak-free audit record —
result.auditcarries 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), andtokenize(reversible). - Reversible tokenization — turn tokens back into the original text with
Redactor.reveal()and an in-memoryKeyring, 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/LOCATIONdetection behind the[ner]extra, off by default so the core install stays light. - Streaming-safe —
StreamRedactorredacts 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
Recognizerfor domain-specific entities without forking the library. - Typed and tested — mypy
--strictand ruff clean, 128 tests, 97.9% coverage. - CLI included —
spanguard scan/spanguard redactfor 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.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| spanguard-0.1.1.tar.gz | 129.5 kB | With provenance | Details for spanguard-0.1.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| spanguard-0.1.1-py3-none-any.whl | Python 3 | none | any | Details for spanguard-0.1.1-py3-none-any.whl |
Total release size: 152.5 kB
Release files / spanguard-0.1.1.tar.gz
| Download URL | spanguard-0.1.1.tar.gz |
|---|---|
| Size | 129.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
0d0485c6cd67b941fb8c4c33e830d86dbfa75129848614e5b8765c92ac3e6ccd
|
|
BLAKE2b-256 checksum How to use checksums |
b775ba2788f44d804a2b287100985989bd41a1709ead15bdef0723e598c610f6
|
| 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 logRelease files / spanguard-0.1.1-py3-none-any.whl
| Download URL | spanguard-0.1.1-py3-none-any.whl |
|---|---|
| Size | 22.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
784663fc6f17982602343c57f9e64d91c26ba598f39c0a08778320d63157b7dc
|
|
BLAKE2b-256 checksum How to use checksums |
e9c2f737af4f2492bfa9f572fbf233aaa44ce45f758b1455e0980ef4558cae2b
|
| 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 logSecurity for spanguard 0.1.1
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.
Attestation for spanguard-0.1.1.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 logA 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.