Dev.to Security πŸ” Cybersecurity πŸ‘ 1 πŸ“– 1 min read

How to detect and remove PII from any text payload in Python

PII leaking into logs, LLM prompts, and audit trails is one of the most common and costly compliance failures. In this post I'll show you how to detect and strip PII from any text payload in Python β€” names, emails, SSN,

PII leaking into logs, LLM prompts, and audit trails is one of the most common and costly compliance failures.
In this post I'll show you how to detect and strip PII from any text payload in Python β€” names, emails, SSN, CPF, credit cards β€” using a production REST API built in Rust with sub-15ms latency.

The problem

Most teams realize PII is leaking too late β€” after a breach, after an audit, or after the data lands in an LLM training set.

The solution

One API call before your data touches anything sensitive:

import requests

def anonymize(text: str, api_key: str) -> dict:
    response = requests.post(
        "https://vortex-dfs.onrender.com/v1/shield/anonymize",
        headers={
            "Authorization": f"Bearer {api_key}",
            "Content-Type": "application/json"
        },
        json={"content": text}
    )
    return response.json()

# Example
result = anonymize(
    "John Smith, SSN 123-45-6789, card 4111-1111-1111-1111",
    api_key="your_key_here"
)

print(result["sanitized"])
# β†’ "[NAME] [SSN] [CARD]"

print(result["risk_score"])
# β†’ 0.94

print(result["latency_ms"])
# β†’ 12.3

Integrate before your LLM pipeline****

def safe_llm_call(ticket_content: str, api_key: str) -> str:
    # Step 1 β€” strip PII
    clean = anonymize(ticket_content, api_key)

    # Step 2 β€” safe to send now
    prompt = f"Summarize this support ticket: {clean['sanitized']}"
    response = openai.chat.completions.create(
        model="gpt-4",
        messages=[{"role": "user", "content": prompt}]
    )
    return response.choices[0].message.content


What gets detected

You can. But:

  • Regex misses context β€” 123-45-6789 alone vs inside a sentence
  • No risk scoring β€” you don't know how sensitive the payload is
  • No token map β€” you can't reverse the anonymization if needed
  • Maintenance burden β€” every new pattern is a new regex

The API handles all of this and returns an encrypted token map if you need to deanonymize later.

Get your API key

Starts at $9/week. Key delivered instantly after payment.
HereπŸ‘‰

πŸ“° Read the original article on Dev.to Security

Originally published by Dev.to Security. Aggregated on AIWithGhost for educational purposes β€” full credit and traffic to the original publisher.