r/Python • • Nov 04 '26

Showcase Showcase Thread

Post all of your code/projects/showcases/AI slop here.

Recycles once a month.

34 Upvotes

208 comments sorted by

View all comments

1

u/Niceyellowduck 10d ago

polanon: reversible PII masking for LLM calls, with a standard-library-only core

What My Project Does

polanon replaces personal data with stable tokens before a prompt goes to a model, and puts the originals back into the answer:

from polanon import Anonymizer, default_guards


anonymizer = Anonymizer(default_guards(with_ner=False))


result = anonymizer.anonymize_sync(
    "Proszę o korektę faktury dla Jana Kowalskiego, PESEL 50810112354, "
    "konto PL61109010140000071219812874, mail jan.kowalski@example.com."
)
print(result.anonymized)
# Proszę o korektę faktury dla <OSOBA_1>, PESEL <PESEL_1>, konto <IBAN_1>, mail <EMAIL_1>.


answer = "Korekta dla <OSOBA_1> (PESEL <PESEL_1>) jest gotowa."  # what the model replied
print(Anonymizer.restore(answer, result.mapping))
# Korekta dla Jana Kowalskiego (PESEL 50810112354) jest gotowa.

The mapping never leaves your process. The Python side:

  • The core is pure standard library. spaCy (for names the rules don't catch), Haystack components and the gateway are optional extras.
  • Anonymizer.stream_restorer(mapping) restores a streamed answer chunk by chunk, holding back anything that could still grow into a token (providers split <OSOBA_1> into <OSO + BA_1>).
  • anonymize_sync for scripts, Django/Flask views, Celery tasks and notebooks, await anonymizer.anonymize(...) in async code.
  • Polish ID numbers (PESEL, NIP, REGON, ID cards), IBANs and payment cards are validated by checksum, not just matched by regex.
  • polanon-gateway, a plain ASGI app on uvicorn and httpx, speaks the OpenAI and Anthropic APIs, so tools you can't change (Open WebUI, n8n, Claude Code) get masking by switching their base URL.
  • Typed (py.typed), Python 3.11+, MIT.

Target Audience

Developers who send user text to OpenAI, Anthropic or a local model, especially with Polish data: names are recognised in all seven grammatical cases ("Jan Kowalski", "Jana Kowalskiego", "z Janem Kowalskim" are one person). Beta (0.4.x), used as the masking engine of a few apps of mine (a self-hosted company chat, a CV writer). The API may still change between minor versions.

Comparison

  • Presidio is the general-purpose option, with many languages. On Polish data polanon was never tuned on (5,255 values, OpenPII test split), with the same spaCy model: 72% vs 66% of values masked whole and 306 vs 2,641 false alarms, a tie on people. Presidio wins on random codes written without a label, and restoring an LLM answer is not built into it. Method: https://github.com/boromat/polanon/blob/main/benchmarks/README.md
  • Baldanders (posted earlier in this thread) is a chat CLI in front of Claude Code and Codex, multilingual, with 55+ national ID formats. polanon is a library and an API gateway you put under your own code or your existing tools, and it goes deep on one language: a Polish name keeps one token across its cases, and the case travels with the token (<OSOBA_1:celownik>), so the restored answer can put the name in the right form.
  • pii-veil checks Polish identifiers too but does not detect names. piimask is language-neutral, with no Polish identifiers or names.

Limits: pseudonymisation, not anonymisation. In English text it catches e-mails, phones, cards, IPs, keys and passwords, but it is not an English name detector. Threat model: https://github.com/boromat/polanon/blob/main/docs/THREAT_MODEL.md

Source: https://github.com/boromat/polanon · pip install polanon · browser demo (Pyodide, nothing is sent): https://boromat.pl/en/demo/

Feedback welcome, especially inputs that get a value through unmasked.