Most AI privacy leaks happen before the model call. I built a Python layer to mask PII first

Discussion

I kept seeing teams obsess over prompt quality and model choice, but sending raw customer data straight to LLM APIs. So I built a small Python package called ShieldPrompt to handle one boring but critical thing: mask sensitive data before it leaves your app, then restore it in the final response. The flow is simple:

  1. Detect PII (regex by default, optional NER)
  2. Replace with tokens like [EMAIL_ADDRESS_1]
  3. Send masked text to LLM
  4. Unmask final response using a per-request vault What I wanted was flexibility, so I added multiple integration points:
  • Decorator: @mask_pii(...) for drop-in function wrapping
  • Core engine: direct Shield().mask() / unmask()
  • FastAPI middleware: masks request JSON string fields + unmasks response text
  • CLI for mask/unmask/inspect workflows
  • MCP server tools for agent workflows A couple implementation details that mattered a lot:
  • Right-to-left replacement while masking (prevents index corruption)
  • Length-sorted token restore while unmasking (prevents partial token collisions)
  • Context-local vault isolation for concurrent requests
  • Graceful fallback to regex-only if NER deps aren’t installed It’s open source and still early, so I’d love practical feedback from people running LLM apps in production: How are you currently handling PII in prompts/responses without adding a ton of complexity?

Posted Apr 9, 2026, 9:11 PM UTC

11
Loading comments…