Skip to main content
┌─────────────────────────────────────────────────────┐
│                                                     │
│   🛡️  AzureAICommunity PII Security Middleware       │
│                                                     │
│   PII detection & blocking for AI agent pipelines  │
│                                                     │
└─────────────────────────────────────────────────────┘

PyPI version Python License: MIT PyPI Downloads

Intercept, detect, and block sensitive personal data before it reaches your LLM — with zero friction.

Getting Started · Profiles · LLM Validation · Contributing


Overview

azureaicommunity-agent-pii-middleware is a plug-and-play security layer for AI agent pipelines built on agent-framework. It scans every user message for PII using Microsoft's Recognizers Text library and can optionally route ambiguous detections through a secondary LLM for a second opinion.

User message
     │
     ▼
┌────────────────────┐
│  PII Detection     │  ← emails, phones, credit cards, SSNs…
│  (Recognizers NLP) │
└────────┬───────────┘
         │ blocked entity found?
         ▼
┌────────────────────┐
│  LLM Validation    │  ← optional secondary agent review
│  (allow / block)   │
└────────┬───────────┘
         │
    ┌────┴────┐
    ▼         ▼
 BLOCKED    ALLOWED
 ← 🚫        → LLM

✨ Features

Feature
🔍 PII detection — emails, phones, IPs, credit cards, SSNs, dates, numbers, units
🎛️ Profile-based config — one-line setup with strict, standard, financial, healthcare
🔧 Builder pattern — fluent API to compose and customize middleware pipelines
🤖 LLM validation — route edge cases through a secondary agent to reduce false positives
🔌 Framework integration — drops directly into agent-framework middleware pipelines

📦 Installation

pip install azureaicommunity-agent-pii-middleware

🚀 Quick Start

import asyncio
from agent_framework.ollama import OllamaChatClient
from agent_framework import Agent
from pii_middleware import PIIMiddleware

# Build a middleware pipeline using the "standard" profile
middleware = (
    PIIMiddleware
        .profile("standard")
        .build()
)

async def main():
    client = OllamaChatClient(model="gemma3:4b")
    agent = Agent(client)

    result = await agent.run("My email is user@example.com", middleware=middleware)
    print(result.text)
    # → "Message blocked: sensitive information detected (email)."

asyncio.run(main())

🎛️ Security Profiles

Choose a pre-built profile to get started instantly:

Profile Blocked Allowed
strict email phone_number ip credit_card datetime number
standard email phone_number datetime number unit
financial credit_card ssn account_number email datetime
healthcare patient_id ssn email phone_number datetime unit
# Built-in profile
middleware = PIIMiddleware.profile("strict").build()

# Custom profile dict
middleware = (
    PIIMiddleware
        .profile({"block": ["email", "ssn"], "allow": ["datetime"]})
        .build()
)

🔧 Custom Entity Lists

Fine-tune the block/allow lists after applying any profile:

middleware = (
    PIIMiddleware
        .profile("standard")
        .block_entities(["email", "phone_number", "credit_card"])
        .allow_entities(["datetime", "number"])
        .build()
)

🤖 LLM-Assisted Validation

Attach a secondary LLM agent that makes the final allow/block decision when PII is detected:

from agent_framework.ollama import OllamaChatClient
from agent_framework import Agent

validator = Agent(OllamaChatClient(model="gemma3:4b"))

middleware = (
    PIIMiddleware
        .profile("standard")
        .llm_agent(validator)
        .build()
)

The validator receives the message and the list of detected entities, and responds with allow or block. This significantly reduces false positives on ambiguous inputs like dates or reference numbers.


⚙️ How It Works

1. Intercept   →  middleware captures the last user message
2. Detect      →  Recognizers Text extracts entity types
3. Filter      →  entities not in allow_list are candidates
4. Match       →  candidates matched against block_list
5. Validate    →  (optional) LLM agent makes final decision
6. Block / Pass →  blocked messages short-circuit the pipeline
                   — the primary LLM is never called

🤝 Contributing

Contributions are welcome! Please open an issue to discuss what you'd like to change before submitting a pull request.

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/my-feature)
  3. Commit your changes (git commit -m 'Add my feature')
  4. Push to the branch (git push origin feature/my-feature)
  5. Open a Pull Request

📄 License

MIT

Metadata

Release files for azureaicommunity-agent-pii-middleware 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 azureaicommunity-agent-pii-middleware 0.1.0
File Size Uploaded Provenance Actions
azureaicommunity_agent_pii_middleware-0.1.0.tar.gz 5.5 kB Without provenance Details for azureaicommunity_agent_pii_middleware-0.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for azureaicommunity-agent-pii-middleware 0.1.0
File Interpreter ABI Platform Actions
azureaicommunity_agent_pii_middleware-0.1.0-py3-none-any.whl Python 3 none any Details for azureaicommunity_agent_pii_middleware-0.1.0-py3-none-any.whl

Total release size: 11.7 kB

Release files / azureaicommunity_agent_pii_middleware-0.1.0.tar.gz

Download URL azureaicommunity_agent_pii_middleware-0.1.0.tar.gz
Size 5.5 kB
Tags Source
SHA-256 checksum
How to use checksums
6ac33977cf5eeeba2e43449b85b205841caf23701f6d17902485788d59410c7b
BLAKE2b-256 checksum
How to use checksums
844f80f65047bb15230f7a1688f824be9b4ed165a32be3f1083ef5abca497256
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.2

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

Download URL azureaicommunity_agent_pii_middleware-0.1.0-py3-none-any.whl
Size 6.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
fa8ec648c76ffdc2bab2e9459a1a4bc0a51a0ba6e166c9ebf2120d998ba5ab90
BLAKE2b-256 checksum
How to use checksums
df9e66b53b626dcc6d1887f74902290d3bb2bea8e51421c011a57da33d61dd8e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.2

Security for azureaicommunity-agent-pii-middleware 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.

No provenance found

None of the files in this release were uploaded with provenance metadata.

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 No 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