Skip to main content

swapback

Your data stays yours. A local proxy that swaps names, emails, phones and keys for placeholders before a prompt reaches an LLM provider, and puts them back in the reply.

PyPI version tests License: MIT Python versions Downloads

Terminal: pip install swapback, then swapback demo. The app sends a made-up name, email and phone; the model receives PERSON_1, EMAIL_1 and PHONE_1 placeholders; the app gets the original values back. PASS.

Quickstart

pip install swapback
swapback demo
swapback init && swapback-proxy

Python 3.11 or newer. swapback demo runs a fake model with made-up data: no key, no network. The proxy listens on 127.0.0.1:8787; point your client at it and change nothing else:

from openai import OpenAI
client = OpenAI(
    base_url="http://127.0.0.1:8787/v1")

Set it up for a real provider

swapback init writes swapback.yaml and known.yaml. Put the names, emails, phones and IDs that must never reach a provider in known.yaml (or build it from a contacts export with swapback known init --vcf contacts.vcf), set upstream: in swapback.yaml, run swapback check-config, then start swapback-proxy. The full steps, including OpenRouter and Anthropic base URLs, are in the guide.

Works with

  • OpenClaw: tested end to end (2026.10.7) with one provider block; its exec, web_fetch, web_search, browser and message tools keep placeholders by default. Config.
  • Any OpenAI-compatible client: OpenAI Chat Completions and Responses (the official SDKs, OpenRouter, LiteLLM clients). Set the base URL, change nothing else.
  • Anthropic Messages: the Anthropic SDK, plain and streamed, including tool_use.
  • LangChain 1.0+: thin reversible SwapbackMiddleware (pip install 'swapback[langchain]') that swaps PII on the wire and restores replies and allowed tool calls. Guide.
  • Tool-policy recipes for Claude Code and Codex CLI: framework recipes.

How it works

flowchart LR
    A["Your app or agent"] -- "Email Jane Q Doe" --> S["swapback (127.0.0.1:8787)"]
    S -- "Email «PERSON_1»" --> P["LLM provider"]
    P -- "Hi «PERSON_1» ..." --> S
    S -- "Hi Jane Q Doe ..." --> A
  1. Swap. Values from your own list are matched in their common spellings (case, separators, escapes, nested JSON); detectors catch phones, emails, cards, IBANs and API keys you did not list. Each becomes a typed placeholder.
  2. Check. An egress re-check scans the outgoing request again. If anything listed is still there, or the scrub fails, the request is blocked (503). It fails closed.
  3. Restore. Placeholders in the reply are put back byte-exact, in plain and streamed replies and in tool-call arguments. Network, shell and send-style tool calls keep the placeholder unless you allow the tool, so a prompt-injected web_fetch cannot carry your email out.

No model to download by default: exact matching plus patterns, about 7,100 lines of Python, 3 core dependencies. Need to catch unlisted names or multilingual PII? Opt in to the optional local NER backend (pip install 'swapback[newmean]', 2.6–15.5 MB, 54 languages, powered by newmean.ai newmean-pii, Apache-2.0). Re-run the leak test yourself with swapback selftest (made-up data, 7 request paths, about 12 s).

Documentation

Topic Where
Why swapback, setup, agents, tool calls, configuration, hooks docs/guide.md
What it does not catch, and security notes docs/limits.md
Leak test, performance, detection score docs/testing.md
LangChain 1.0+ reversible PII middleware & proxy setup docs/langchain.md
Comparison with PasteGuard, Maskit, Veil, piighost and Kiji, plus Presidio, LLM Guard, Skyflow and Nightfall docs/comparison.md
Changes per release CHANGELOG.md
Reporting a vulnerability SECURITY.md

Project facts: Python 3.11+, about 7,100 lines (343 KB), 3 core dependencies (PyYAML, phonenumbers, pyahocorasick), stdlib HTTP server, MIT.

Development

pip install -e ".[test]"
pytest -q                      # the full suite, including the holdout score as a regression floor
python evals/run_holdout.py    # the detection score table in docs/testing.md (--misses lists the cases not caught)
python examples/smoke.py       # end-to-end: mock upstream + proxy, plain and streamed

The vendored rule sets live in swapback/vendor (see THIRD_PARTY_NOTICES.md); tools/build_vendor.py regenerates the Presidio patterns.

See CONTRIBUTING.md and the changelog.

Roadmap

  • Restore Anthropic Messages streams. Done in 0.1.1.
  • Catch separator and case variants of listed values (NBSP, ., _, -, %20, all caps) under one placeholder. Done in 0.1.1.
  • Restore OpenAI Responses API streams. Done in 0.3.0.
  • Keep placeholders in tool calls that could send data out. Done in 0.3.0.
  • Build the known-values file from a contacts export. Done in 0.3.0 (swapback known init).
  • Optional, pluggable NER for names you never listed, off by default. Optional swapback[newmean] backend (powered by newmean.ai newmean-pii, Apache-2.0).
  • More national ID and address patterns, with tests from synthetic data.
  • Release on PyPI. Done: pip install swapback.
  • Hooks for spend caps, local fallback and usage accounting. Done in 0.4.0.
  • A public, re-runnable leak test. Done in 0.4.2 (swapback selftest).

Ideas and pull requests are welcome; open an issue first for anything large.

License

MIT, see LICENSE. Bundled rule data: gitleaks (MIT) and Microsoft Presidio (MIT), see THIRD_PARTY_NOTICES.md.

Metadata

Release files for swapback 0.5.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 swapback 0.5.0
File Size Uploaded Provenance Actions
swapback-0.5.0.tar.gz 422.2 kB With provenance Details for swapback-0.5.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for swapback 0.5.0
File Interpreter ABI Platform Actions
swapback-0.5.0-py3-none-any.whl Python 3 none any Details for swapback-0.5.0-py3-none-any.whl

Total release size: 739.1 kB

Release files / swapback-0.5.0.tar.gz

Download URL swapback-0.5.0.tar.gz
Size 422.2 kB
Tags Source
SHA-256 checksum
How to use checksums
716c2eba6e948acfd0afa75f464e7395b310295cfba3fffb92291d9816164bb9
BLAKE2b-256 checksum
How to use checksums
518c9836d0861e3a772eefad2ed2cd2b9e00efb1b612d3c4381438e1ffd426cc
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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 Nov 24, 2026.

Transparency log

Release files / swapback-0.5.0-py3-none-any.whl

Download URL swapback-0.5.0-py3-none-any.whl
Size 317.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
bc894fd4e58dd32592be17c368be55182ef0d5295e8b7343f3c23ad477318f30
BLAKE2b-256 checksum
How to use checksums
b3d23de4d5ef15e4ec2751f6d2c74204b3593d2d57e9621cd713126a5efc9a0c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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 Nov 24, 2026.

Transparency log

Security for swapback 0.5.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.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for swapback-0.5.0.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 Nov 24, 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