Skip to main content

maskflow-gateway

A drop-in OpenAI / Anthropic-compatible proxy that detects PII in a request, replaces it with reversible typed placeholders before the request reaches the provider, and restores the originals in the response — including mid-stream.

Point your existing client's base URL at the gateway. No SDK change, no code change.

from openai import OpenAI

client = OpenAI(
    base_url="http://localhost:8000/v1",  # <- the gateway
    api_key="sk-...",  # <- your real OpenAI key, passed straight through
)
client.chat.completions.create(
    model="gpt-4o",
    messages=[
        {"role": "user", "content": "Email Rahul at rahul.sharma@example.com about PAN ABCPE1234F"}
    ],
)
# OpenAI sees: "Email Rahul at <EMAIL_1> about PAN <PAN_1>"
# You get back: the model's reply with <EMAIL_1>/<PAN_1> restored to the real values

Endpoints

Route What it does
POST /v1/chat/completions OpenAI chat, streaming + non-streaming, tool calls
POST /v1/messages Anthropic Messages, streaming + non-streaming, tool use
POST /v1/embeddings masks each input before it is embedded — the RAG path
POST /v1/mask / POST /v1/unmask direct masking, no upstream call
GET /healthz liveness (never touches Redis)
GET /readyz readiness — 503 fail-closed if Redis maxmemory-policy != noeviction
GET /metrics Prometheus
GET /v1/entities every PII type the loaded packs detect

Streaming unmask

The model streams the reply in arbitrary chunks and a placeholder like <PERSON_NAME_1> can be split across two SSE frames. The gateway parses the provider's SSE, keeps a rolling buffer + a trie of the session's active placeholders, and emits the longest prefix that is certain — a completed placeholder (replaced) or a character that cannot begin any placeholder — retaining only the tail that could still grow into one. A two-layer decoder handles chunk splits mid-UTF-8. Property: for any chunking of a masked reply, the concatenated stream equals the non-streaming unmask result (fuzz-tested at every byte boundary).

Sessions (multi-turn / tool loops)

Send X-Maskflow-Session: <your opaque id> to keep <PHONE_1> meaning the same number across every turn and tool call of one agent run. Without the header each request is masked and unmasked in isolation.

  • X-Maskflow-Session-TTL: <seconds> overrides the default (3600), capped at MASKFLOW_GATEWAY_SESSION_TTL_MAX_SECONDS (86400).
  • Keyed sessions need Redis (MASKFLOW_GATEWAY_REDIS_URL). Mappings are encrypted with AES-256-GCM (MASKFLOW_GATEWAY_SESSION_KEY, 32 random bytes hex) before they touch Redis, with a mandatory TTL.
  • Redis must run maxmemory-policy noeviction — an evicted session mid-conversation means unmask finds nothing and a user sees raw <PERSON_NAME_1> text. /readyz returns 503 until this is fixed.

Configuration

Every setting is an environment variable prefixed MASKFLOW_GATEWAY_:

Variable Default Notes
OPENAI_BASE_URL https://api.openai.com/v1 must include the version segment
ANTHROPIC_BASE_URL https://api.anthropic.com/v1
UPSTREAM_API_KEY (unset) set → gateway injects it; unset → client's own key is forwarded, nothing stored
NER 0 1 enables the spaCy pass (bare Indian names & addresses); much slower — see loadtest/
MIN_CONFIDENCE 0.5 detection threshold
REDIS_URL (unset) unset → in-process ephemeral sessions only (single replica)
SESSION_KEY (unset) hex, 32 bytes; required when REDIS_URL is set
SESSION_TTL_SECONDS / SESSION_TTL_MAX_SECONDS 3600 / 86400
REQUIRE_MAXMEMORY_NOEVICTION true
MAX_REQUEST_BYTES 2000000
UPSTREAM_TIMEOUT_SECONDS / UPSTREAM_CONNECT_TIMEOUT_SECONDS 120 / 10
RATE_LIMIT_PER_MINUTE / RATE_LIMIT_BURST 0 (off) keyed by a hash of the client's Authorization
TOOL_CALL_MAX_DEPTH / TOOL_CALL_MAX_ITEMS 32 / 10000 bound the tool-argument JSON walk
JSON_LOGS 1 structured logs; every record also passes MaskFlow's PII scrub filter
CORS_ALLOW_ORIGINS [] JSON list

The gateway's masking config comes only from these variables — it does not read a .maskflowrc.

Run

pip install "maskflow-gateway[redis]"
export MASKFLOW_GATEWAY_REDIS_URL=redis://localhost:6379/0
export MASKFLOW_GATEWAY_SESSION_KEY=$(python -c "import os;print(os.urandom(32).hex())")
maskflow-gateway --host 0.0.0.0 --port 8000 --workers 4

Deploy artifacts in deploy/: multi-arch Dockerfile, docker-compose.yml (gateway + noeviction Redis), a Helm chart (deploy/helm/maskflow-gateway, with HPA / PDB / ServiceMonitor), and Fly / Render / Railway templates.

Throughput

Published honestly, with the hardware, in loadtest/README.md. Rough laptop floor: ~430 req/s pattern-only, ~90 req/s NER-enabled (4 workers, Intel i7-9750H).

License

MIT — like the rest of MaskFlow. No license gates, no paid flags, no telemetry.

Metadata

Release files for maskflow-gateway 0.2.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for maskflow-gateway 0.2.1
File Size Uploaded Provenance Actions
maskflow_gateway-0.2.1.tar.gz 43.1 kB With provenance Details for maskflow_gateway-0.2.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for maskflow-gateway 0.2.1
File Interpreter ABI Platform Actions
maskflow_gateway-0.2.1-py3-none-any.whl Python 3 none any Details for maskflow_gateway-0.2.1-py3-none-any.whl

Total release size: 79.3 kB

Release files / maskflow_gateway-0.2.1.tar.gz

Download URL maskflow_gateway-0.2.1.tar.gz
Size 43.1 kB
Tags Source
SHA-256 checksum
How to use checksums
426f6bbc4611df4632a5038d04c0fa5cac3b352bffeda4e1fe090cac9a358e2b
BLAKE2b-256 checksum
How to use checksums
ec4c81fdcfdaa536cf82aa35fbefbaa0386b409b9ad87fbcb38b849b4eea0cba
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

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 Oct 6, 2026.

Transparency log

Release files / maskflow_gateway-0.2.1-py3-none-any.whl

Download URL maskflow_gateway-0.2.1-py3-none-any.whl
Size 36.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
e0c22ee7f00d8f14ab1977710a5744284990d997f428e7a55512a5116e127fb9
BLAKE2b-256 checksum
How to use checksums
f825530eace46426dd513ca3b65757a62ea027b1a094950e2358721b6b2f42bf
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

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 Oct 6, 2026.

Transparency log

Security for maskflow-gateway 0.2.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.

Select from 2 files

Attestation for maskflow_gateway-0.2.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 Oct 6, 2026.

Transparency log

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.

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