phiproxy 0.1.0a1__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (40) hide show
  1. phiproxy-0.1.0a1/.github/workflows/ci.yml +36 -0
  2. phiproxy-0.1.0a1/.github/workflows/release.yml +47 -0
  3. phiproxy-0.1.0a1/.gitignore +10 -0
  4. phiproxy-0.1.0a1/CONTRIBUTING.md +34 -0
  5. phiproxy-0.1.0a1/LICENSE +202 -0
  6. phiproxy-0.1.0a1/NOTICE +5 -0
  7. phiproxy-0.1.0a1/PKG-INFO +208 -0
  8. phiproxy-0.1.0a1/README.md +179 -0
  9. phiproxy-0.1.0a1/SECURITY.md +22 -0
  10. phiproxy-0.1.0a1/benchmark/README.md +38 -0
  11. phiproxy-0.1.0a1/benchmark/data/patient_comms_v0.jsonl +288 -0
  12. phiproxy-0.1.0a1/benchmark/generate.py +202 -0
  13. phiproxy-0.1.0a1/benchmark/results_v0.json +56 -0
  14. phiproxy-0.1.0a1/benchmark/score.py +133 -0
  15. phiproxy-0.1.0a1/deploy/.env.example +4 -0
  16. phiproxy-0.1.0a1/deploy/Dockerfile +20 -0
  17. phiproxy-0.1.0a1/deploy/docker-compose.yml +20 -0
  18. phiproxy-0.1.0a1/deploy/litellm.config.yaml +30 -0
  19. phiproxy-0.1.0a1/examples/e2e/config.yaml +17 -0
  20. phiproxy-0.1.0a1/examples/e2e/fake_llm.py +72 -0
  21. phiproxy-0.1.0a1/examples/e2e/run_e2e.py +135 -0
  22. phiproxy-0.1.0a1/pyproject.toml +46 -0
  23. phiproxy-0.1.0a1/src/phiproxy/__init__.py +30 -0
  24. phiproxy-0.1.0a1/src/phiproxy/dates.py +128 -0
  25. phiproxy-0.1.0a1/src/phiproxy/detectors/__init__.py +5 -0
  26. phiproxy-0.1.0a1/src/phiproxy/detectors/base.py +73 -0
  27. phiproxy-0.1.0a1/src/phiproxy/detectors/known.py +235 -0
  28. phiproxy-0.1.0a1/src/phiproxy/detectors/presidio.py +64 -0
  29. phiproxy-0.1.0a1/src/phiproxy/detectors/regex.py +276 -0
  30. phiproxy-0.1.0a1/src/phiproxy/entities.py +65 -0
  31. phiproxy-0.1.0a1/src/phiproxy/integrations/__init__.py +1 -0
  32. phiproxy-0.1.0a1/src/phiproxy/integrations/litellm.py +304 -0
  33. phiproxy-0.1.0a1/src/phiproxy/masker.py +282 -0
  34. phiproxy-0.1.0a1/src/phiproxy/vault.py +182 -0
  35. phiproxy-0.1.0a1/tests/test_dates.py +34 -0
  36. phiproxy-0.1.0a1/tests/test_known.py +53 -0
  37. phiproxy-0.1.0a1/tests/test_litellm.py +103 -0
  38. phiproxy-0.1.0a1/tests/test_masker.py +132 -0
  39. phiproxy-0.1.0a1/tests/test_regex.py +82 -0
  40. phiproxy-0.1.0a1/tests/test_vault.py +79 -0
@@ -0,0 +1,36 @@
1
+ name: ci
2
+
3
+ on:
4
+ push:
5
+ branches: [main]
6
+ pull_request:
7
+
8
+ permissions:
9
+ contents: read
10
+
11
+ jobs:
12
+ test:
13
+ runs-on: ubuntu-latest
14
+ strategy:
15
+ matrix:
16
+ python: ["3.10", "3.12", "3.13"]
17
+ steps:
18
+ - uses: actions/checkout@v4
19
+ - uses: actions/setup-python@v5
20
+ with:
21
+ python-version: ${{ matrix.python }}
22
+ - run: pip install -e ".[dev,litellm]"
23
+ - run: pytest -q
24
+ - name: Benchmark (regex+known must stay at or above 85% recall)
25
+ run: python benchmark/score.py --config regex+known --min-recall 0.85
26
+
27
+ e2e:
28
+ runs-on: ubuntu-latest
29
+ steps:
30
+ - uses: actions/checkout@v4
31
+ - uses: actions/setup-python@v5
32
+ with:
33
+ python-version: "3.12"
34
+ - run: pip install -e ".[litellm]" "litellm[proxy]"
35
+ - name: Through a real LiteLLM proxy
36
+ run: python examples/e2e/run_e2e.py
@@ -0,0 +1,47 @@
1
+ name: release
2
+
3
+ # Publishes to PyPI when a GitHub release is published.
4
+ # Uses PyPI Trusted Publishing (OIDC): no API token is stored anywhere.
5
+ on:
6
+ release:
7
+ types: [published]
8
+
9
+ permissions:
10
+ contents: read
11
+
12
+ jobs:
13
+ build:
14
+ runs-on: ubuntu-latest
15
+ steps:
16
+ - uses: actions/checkout@v4
17
+ - uses: actions/setup-python@v5
18
+ with:
19
+ python-version: "3.12"
20
+ - name: Tag must match package version
21
+ run: |
22
+ v=$(python -c "import re;print(re.search(r'__version__ = \"(.+)\"', open('src/phiproxy/__init__.py').read()).group(1))")
23
+ test "v$v" = "${GITHUB_REF_NAME}" || { echo "tag ${GITHUB_REF_NAME} != v$v"; exit 1; }
24
+ - run: pip install -e ".[dev]" build twine
25
+ - run: pytest -q
26
+ - run: python benchmark/score.py --config regex+known --min-recall 0.85
27
+ - run: python -m build
28
+ - run: twine check --strict dist/*
29
+ - uses: actions/upload-artifact@v4
30
+ with:
31
+ name: dist
32
+ path: dist/
33
+
34
+ publish:
35
+ needs: build
36
+ runs-on: ubuntu-latest
37
+ environment:
38
+ name: pypi
39
+ url: https://pypi.org/p/phiproxy
40
+ permissions:
41
+ id-token: write
42
+ steps:
43
+ - uses: actions/download-artifact@v4
44
+ with:
45
+ name: dist
46
+ path: dist/
47
+ - uses: pypa/gh-action-pypi-publish@release/v1
@@ -0,0 +1,10 @@
1
+ .venv/
2
+ __pycache__/
3
+ *.pyc
4
+ .pytest_cache/
5
+ dist/
6
+ build/
7
+ *.egg-info/
8
+ deploy/.env
9
+ received.jsonl
10
+ .DS_Store
@@ -0,0 +1,34 @@
1
+ # Contributing
2
+
3
+ Thanks for helping. The most valuable contributions right now:
4
+
5
+ - **Benchmark data**: synthetic patient messages, call transcripts and referral notes with labelled identifiers, especially in languages other than English. All values must be invented.
6
+ - **Detection misses**: a realistic message where phiproxy leaks an identifier. Report it privately if it is a leak in a common format (see SECURITY.md); otherwise open an issue with a synthetic example.
7
+ - **Adapters** for other gateways and SDKs.
8
+
9
+ ## Ground rules
10
+
11
+ - **No real patient data anywhere**: not in code, tests, issues, PRs or benchmark files. Use 555-01xx phone numbers, `example.com` emails and made-up names.
12
+ - Detection changes must come with tests and must not lower benchmark recall: `python benchmark/score.py --config regex+known --min-recall 0.85`.
13
+ - Prefer recall over precision. Masking a non-identifier costs some answer quality; missing an identifier leaks PHI.
14
+ - Nothing in the detection path may call an external service.
15
+
16
+ ## Development
17
+
18
+ ```bash
19
+ python -m venv .venv && . .venv/bin/activate
20
+ pip install -e ".[dev,litellm]"
21
+ pytest -q
22
+ python benchmark/score.py
23
+ pip install "litellm[proxy]" && python examples/e2e/run_e2e.py # optional, slower
24
+ ```
25
+
26
+ ## Sign-off (DCO)
27
+
28
+ Every commit must be signed off to certify the [Developer Certificate of Origin](https://developercertificate.org/):
29
+
30
+ ```bash
31
+ git commit -s -m "your message"
32
+ ```
33
+
34
+ This adds a `Signed-off-by: Your Name <email>` line. By signing off you confirm you have the right to submit the change under the project's Apache-2.0 license.
@@ -0,0 +1,202 @@
1
+
2
+ Apache License
3
+ Version 2.0, January 2004
4
+ http://www.apache.org/licenses/
5
+
6
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7
+
8
+ 1. Definitions.
9
+
10
+ "License" shall mean the terms and conditions for use, reproduction,
11
+ and distribution as defined by Sections 1 through 9 of this document.
12
+
13
+ "Licensor" shall mean the copyright owner or entity authorized by
14
+ the copyright owner that is granting the License.
15
+
16
+ "Legal Entity" shall mean the union of the acting entity and all
17
+ other entities that control, are controlled by, or are under common
18
+ control with that entity. For the purposes of this definition,
19
+ "control" means (i) the power, direct or indirect, to cause the
20
+ direction or management of such entity, whether by contract or
21
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
22
+ outstanding shares, or (iii) beneficial ownership of such entity.
23
+
24
+ "You" (or "Your") shall mean an individual or Legal Entity
25
+ exercising permissions granted by this License.
26
+
27
+ "Source" form shall mean the preferred form for making modifications,
28
+ including but not limited to software source code, documentation
29
+ source, and configuration files.
30
+
31
+ "Object" form shall mean any form resulting from mechanical
32
+ transformation or translation of a Source form, including but
33
+ not limited to compiled object code, generated documentation,
34
+ and conversions to other media types.
35
+
36
+ "Work" shall mean the work of authorship, whether in Source or
37
+ Object form, made available under the License, as indicated by a
38
+ copyright notice that is included in or attached to the work
39
+ (an example is provided in the Appendix below).
40
+
41
+ "Derivative Works" shall mean any work, whether in Source or Object
42
+ form, that is based on (or derived from) the Work and for which the
43
+ editorial revisions, annotations, elaborations, or other modifications
44
+ represent, as a whole, an original work of authorship. For the purposes
45
+ of this License, Derivative Works shall not include works that remain
46
+ separable from, or merely link (or bind by name) to the interfaces of,
47
+ the Work and Derivative Works thereof.
48
+
49
+ "Contribution" shall mean any work of authorship, including
50
+ the original version of the Work and any modifications or additions
51
+ to that Work or Derivative Works thereof, that is intentionally
52
+ submitted to Licensor for inclusion in the Work by the copyright owner
53
+ or by an individual or Legal Entity authorized to submit on behalf of
54
+ the copyright owner. For the purposes of this definition, "submitted"
55
+ means any form of electronic, verbal, or written communication sent
56
+ to the Licensor or its representatives, including but not limited to
57
+ communication on electronic mailing lists, source code control systems,
58
+ and issue tracking systems that are managed by, or on behalf of, the
59
+ Licensor for the purpose of discussing and improving the Work, but
60
+ excluding communication that is conspicuously marked or otherwise
61
+ designated in writing by the copyright owner as "Not a Contribution."
62
+
63
+ "Contributor" shall mean Licensor and any individual or Legal Entity
64
+ on behalf of whom a Contribution has been received by Licensor and
65
+ subsequently incorporated within the Work.
66
+
67
+ 2. Grant of Copyright License. Subject to the terms and conditions of
68
+ this License, each Contributor hereby grants to You a perpetual,
69
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
+ copyright license to reproduce, prepare Derivative Works of,
71
+ publicly display, publicly perform, sublicense, and distribute the
72
+ Work and such Derivative Works in Source or Object form.
73
+
74
+ 3. Grant of Patent License. Subject to the terms and conditions of
75
+ this License, each Contributor hereby grants to You a perpetual,
76
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
+ (except as stated in this section) patent license to make, have made,
78
+ use, offer to sell, sell, import, and otherwise transfer the Work,
79
+ where such license applies only to those patent claims licensable
80
+ by such Contributor that are necessarily infringed by their
81
+ Contribution(s) alone or by combination of their Contribution(s)
82
+ with the Work to which such Contribution(s) was submitted. If You
83
+ institute patent litigation against any entity (including a
84
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
85
+ or a Contribution incorporated within the Work constitutes direct
86
+ or contributory patent infringement, then any patent licenses
87
+ granted to You under this License for that Work shall terminate
88
+ as of the date such litigation is filed.
89
+
90
+ 4. Redistribution. You may reproduce and distribute copies of the
91
+ Work or Derivative Works thereof in any medium, with or without
92
+ modifications, and in Source or Object form, provided that You
93
+ meet the following conditions:
94
+
95
+ (a) You must give any other recipients of the Work or
96
+ Derivative Works a copy of this License; and
97
+
98
+ (b) You must cause any modified files to carry prominent notices
99
+ stating that You changed the files; and
100
+
101
+ (c) You must retain, in the Source form of any Derivative Works
102
+ that You distribute, all copyright, patent, trademark, and
103
+ attribution notices from the Source form of the Work,
104
+ excluding those notices that do not pertain to any part of
105
+ the Derivative Works; and
106
+
107
+ (d) If the Work includes a "NOTICE" text file as part of its
108
+ distribution, then any Derivative Works that You distribute must
109
+ include a readable copy of the attribution notices contained
110
+ within such NOTICE file, excluding those notices that do not
111
+ pertain to any part of the Derivative Works, in at least one
112
+ of the following places: within a NOTICE text file distributed
113
+ as part of the Derivative Works; within the Source form or
114
+ documentation, if provided along with the Derivative Works; or,
115
+ within a display generated by the Derivative Works, if and
116
+ wherever such third-party notices normally appear. The contents
117
+ of the NOTICE file are for informational purposes only and
118
+ do not modify the License. You may add Your own attribution
119
+ notices within Derivative Works that You distribute, alongside
120
+ or as an addendum to the NOTICE text from the Work, provided
121
+ that such additional attribution notices cannot be construed
122
+ as modifying the License.
123
+
124
+ You may add Your own copyright statement to Your modifications and
125
+ may provide additional or different license terms and conditions
126
+ for use, reproduction, or distribution of Your modifications, or
127
+ for any such Derivative Works as a whole, provided Your use,
128
+ reproduction, and distribution of the Work otherwise complies with
129
+ the conditions stated in this License.
130
+
131
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
132
+ any Contribution intentionally submitted for inclusion in the Work
133
+ by You to the Licensor shall be under the terms and conditions of
134
+ this License, without any additional terms or conditions.
135
+ Notwithstanding the above, nothing herein shall supersede or modify
136
+ the terms of any separate license agreement you may have executed
137
+ with Licensor regarding such Contributions.
138
+
139
+ 6. Trademarks. This License does not grant permission to use the trade
140
+ names, trademarks, service marks, or product names of the Licensor,
141
+ except as required for reasonable and customary use in describing the
142
+ origin of the Work and reproducing the content of the NOTICE file.
143
+
144
+ 7. Disclaimer of Warranty. Unless required by applicable law or
145
+ agreed to in writing, Licensor provides the Work (and each
146
+ Contributor provides its Contributions) on an "AS IS" BASIS,
147
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
+ implied, including, without limitation, any warranties or conditions
149
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
+ PARTICULAR PURPOSE. You are solely responsible for determining the
151
+ appropriateness of using or redistributing the Work and assume any
152
+ risks associated with Your exercise of permissions under this License.
153
+
154
+ 8. Limitation of Liability. In no event and under no legal theory,
155
+ whether in tort (including negligence), contract, or otherwise,
156
+ unless required by applicable law (such as deliberate and grossly
157
+ negligent acts) or agreed to in writing, shall any Contributor be
158
+ liable to You for damages, including any direct, indirect, special,
159
+ incidental, or consequential damages of any character arising as a
160
+ result of this License or out of the use or inability to use the
161
+ Work (including but not limited to damages for loss of goodwill,
162
+ work stoppage, computer failure or malfunction, or any and all
163
+ other commercial damages or losses), even if such Contributor
164
+ has been advised of the possibility of such damages.
165
+
166
+ 9. Accepting Warranty or Additional Liability. While redistributing
167
+ the Work or Derivative Works thereof, You may choose to offer,
168
+ and charge a fee for, acceptance of support, warranty, indemnity,
169
+ or other liability obligations and/or rights consistent with this
170
+ License. However, in accepting such obligations, You may act only
171
+ on Your own behalf and on Your sole responsibility, not on behalf
172
+ of any other Contributor, and only if You agree to indemnify,
173
+ defend, and hold each Contributor harmless for any liability
174
+ incurred by, or claims asserted against, such Contributor by reason
175
+ of your accepting any such warranty or additional liability.
176
+
177
+ END OF TERMS AND CONDITIONS
178
+
179
+ APPENDIX: How to apply the Apache License to your work.
180
+
181
+ To apply the Apache License to your work, attach the following
182
+ boilerplate notice, with the fields enclosed by brackets "[]"
183
+ replaced with your own identifying information. (Don't include
184
+ the brackets!) The text should be enclosed in the appropriate
185
+ comment syntax for the file format. We also recommend that a
186
+ file or class name and description of purpose be included on the
187
+ same "printed page" as the copyright notice for easier
188
+ identification within third-party archives.
189
+
190
+ Copyright [yyyy] [name of copyright owner]
191
+
192
+ Licensed under the Apache License, Version 2.0 (the "License");
193
+ you may not use this file except in compliance with the License.
194
+ You may obtain a copy of the License at
195
+
196
+ http://www.apache.org/licenses/LICENSE-2.0
197
+
198
+ Unless required by applicable law or agreed to in writing, software
199
+ distributed under the License is distributed on an "AS IS" BASIS,
200
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201
+ See the License for the specific language governing permissions and
202
+ limitations under the License.
@@ -0,0 +1,5 @@
1
+ phiproxy
2
+ Copyright 2026 Decision Tree Technology
3
+
4
+ This product includes software developed by Decision Tree Technology
5
+ (https://decisiontree.tech/).
@@ -0,0 +1,208 @@
1
+ Metadata-Version: 2.5
2
+ Name: phiproxy
3
+ Version: 0.1.0a1
4
+ Summary: Your patients' stand-in on the way to any LLM, and back: reversible PHI masking proxy (LiteLLM guardrail + Python library).
5
+ Project-URL: Homepage, https://github.com/DecisionTreeTechnology/phiproxy
6
+ Project-URL: Issues, https://github.com/DecisionTreeTechnology/phiproxy/issues
7
+ Author: Decision Tree Technology
8
+ License-Expression: Apache-2.0
9
+ License-File: LICENSE
10
+ License-File: NOTICE
11
+ Keywords: de-identification,guardrail,hipaa,litellm,llm,phi,phipa,pii,privacy
12
+ Classifier: Development Status :: 2 - Pre-Alpha
13
+ Classifier: Intended Audience :: Developers
14
+ Classifier: Intended Audience :: Healthcare Industry
15
+ Classifier: Programming Language :: Python :: 3
16
+ Classifier: Topic :: Scientific/Engineering :: Medical Science Apps.
17
+ Classifier: Topic :: Security
18
+ Requires-Python: >=3.10
19
+ Provides-Extra: dev
20
+ Requires-Dist: pytest-asyncio>=0.23; extra == 'dev'
21
+ Requires-Dist: pytest>=8; extra == 'dev'
22
+ Provides-Extra: litellm
23
+ Requires-Dist: litellm>=1.70; extra == 'litellm'
24
+ Provides-Extra: presidio
25
+ Requires-Dist: presidio-analyzer>=2.2; extra == 'presidio'
26
+ Provides-Extra: redis
27
+ Requires-Dist: redis>=5; extra == 'redis'
28
+ Description-Content-Type: text/markdown
29
+
30
+ # phiproxy
31
+
32
+ **Your patients' stand-in on the way to any LLM, and back.**
33
+
34
+ phiproxy is a proxy in two senses:
35
+
36
+ 1. **A proxy for the patient.** Before a prompt leaves your network, every identifier (name, date of birth, phone, health card number...) is replaced by a stand-in such as `[NAME_1]`. The AI provider only ever sees the stand-ins.
37
+ 2. **A proxy server.** It sits between your apps and every LLM provider as a [LiteLLM](https://github.com/BerriAI/litellm) guardrail, so there is one enforcement point for OpenAI, Anthropic, Azure, Bedrock and Gemini. It also works as a plain Python library.
38
+
39
+ When the reply comes back, phiproxy swaps the stand-ins for the real values, so your users and your tools see the right patient. The round trip is the point: the model can still reason about the conversation without ever seeing who it is about.
40
+
41
+ ```
42
+ "Hi, this is Sarah Chen, DOB 1990-03-05. Call me at 416-555-0142."
43
+ │ phiproxy (inside your network)
44
+ ▼
45
+ "Hi, this is [NAME_1], DOB [DATE_1]. Call me at [PHONE_1]." ──► any LLM provider
46
+ ▲
47
+ │ reply: "Thanks [NAME_1], we'll call [PHONE_1] today."
48
+ "Thanks Sarah Chen, we'll call 416-555-0142 today." ◄──
49
+ ```
50
+
51
+ > **Status: pre-alpha (0.1.0a1).** APIs will change. Do not rely on it as your only control.
52
+
53
+ ## Read this first
54
+
55
+ - **phiproxy reduces how much PHI reaches an LLM provider. It does not make an unapproved provider compliant.** No detector catches 100% of identifiers; every miss is a disclosure. Send PHI only to providers you have a BAA (HIPAA) or equivalent agreement (e.g. PHIPA agent agreement) with, and use phiproxy as an extra layer on top.
56
+ - Audio, images and files cannot be masked as text. phiproxy **blocks** them by default.
57
+ - Get privacy or legal review before relying on de-identification for any workload.
58
+
59
+ ## What makes it different
60
+
61
+ - **Your own records as a detector.** Pass the identifiers you already hold for the patient (name, DOB, phone, health card). Exact and one-typo matching against them catches lowercase names, nicknames in context and misspellings that pattern matching misses. On the benchmark below this lifts recall from 61% to 86%.
62
+ - **Stable tokens per conversation.** The same value gets the same token on every turn of a session, so the model can follow who is who and provider prompt caching keeps working. Tokens are sequential, not derived from the value (see 45 CFR 164.514(c)).
63
+ - **Fail closed.** If masking errors, or a request holds media the policy does not allow, the request is rejected and nothing is sent. Clients cannot switch the guardrail off through request metadata.
64
+ - **Clinic-specific identifiers.** Canadian health card numbers (OHIP, RAMQ), SIN, donor and embryo IDs, spoken numbers and dates from call transcripts.
65
+ - **Date shifting (optional).** Instead of `[DATE_1]`, move every date in a session by the same random offset. Intervals stay true (retrieval → transfer 5 days later) and the reply is shifted back.
66
+ - **Streaming and tool calls.** Tokens split across stream chunks are restored correctly; tool-call arguments are masked going out and restored coming back, so your tools receive real values.
67
+
68
+ ## Quick start: library
69
+
70
+ ```bash
71
+ pip install phiproxy # library
72
+ pip install "phiproxy[litellm]" # + LiteLLM guardrail
73
+ ```
74
+
75
+ ```python
76
+ from phiproxy import Masker, KnownEntities
77
+
78
+ masker = Masker()
79
+ known = KnownEntities(names=["Sarah Chen"], phones=["416-555-0142"], dates_of_birth=["1990-03-05"])
80
+
81
+ masked = masker.mask("hi it's sarah, dob march 5 1990", session_id="conv-42", known=known)
82
+ masked.text # 'hi it's [NAME_1], dob [DATE_1]'
83
+ masked.summary() # {'NAME': 1, 'DATE': 1} - safe to log; masked.entities is not
84
+
85
+ reply = call_your_llm(masked.text)
86
+ masker.unmask(reply, session_id="conv-42")
87
+ ```
88
+
89
+ Whole payloads (OpenAI or Anthropic message lists, tool calls, tool results):
90
+
91
+ ```python
92
+ safe_messages = masker.mask_payload(messages, session_id="conv-42", known=known)
93
+ restored = masker.unmask_payload(response_message, session_id="conv-42")
94
+ ```
95
+
96
+ Streaming:
97
+
98
+ ```python
99
+ u = masker.stream_unmasker("conv-42")
100
+ for chunk in stream:
101
+ print(u.feed(chunk), end="")
102
+ print(u.flush())
103
+ ```
104
+
105
+ ## Quick start: LiteLLM gateway
106
+
107
+ ```yaml
108
+ # config.yaml
109
+ guardrails:
110
+ - guardrail_name: phiproxy
111
+ litellm_params:
112
+ guardrail: phiproxy.integrations.litellm.PhiproxyGuardrail
113
+ mode: [pre_call, post_call]
114
+ default_on: true
115
+ redis_url: os.environ/PHIPROXY_REDIS_URL # optional; in-memory if unset
116
+
117
+ litellm_settings:
118
+ turn_off_message_logging: true # otherwise LiteLLM logs hold the restored reply
119
+ ```
120
+
121
+ Clients pass the conversation id and, optionally, known identifiers in `metadata`:
122
+
123
+ ```python
124
+ client.chat.completions.create(
125
+ model="gpt-4o",
126
+ messages=[...],
127
+ extra_body={"metadata": {
128
+ "session_id": "conv-42",
129
+ "phiproxy_known": {"names": ["Sarah Chen"], "phones": ["416-555-0142"]},
130
+ }},
131
+ )
132
+ ```
133
+
134
+ `phiproxy_known` is removed from the request before anything is forwarded. Instead of sending identifiers, you can configure `known_entities_provider: "yourapp.phi:lookup"`, an (async) function that receives `session_id` and `metadata` (e.g. a patient id) and returns them from your own system.
135
+
136
+ A ready-to-run gateway (LiteLLM + phiproxy + Redis vault) is in [`deploy/`](https://github.com/DecisionTreeTechnology/phiproxy/tree/main/deploy):
137
+
138
+ ```bash
139
+ cp deploy/.env.example deploy/.env # add provider keys
140
+ docker compose -f deploy/docker-compose.yml up --build
141
+ ```
142
+
143
+ `examples/e2e/run_e2e.py` runs the real LiteLLM proxy against a fake provider that records every request. It checks that the provider received only tokens, that the client got the PHI back (plain and streaming), that images are blocked and that client opt-out is ignored. It runs in CI.
144
+
145
+ ## What it detects
146
+
147
+ | Type | Examples | Layer |
148
+ |---|---|---|
149
+ | `NAME` | "this is Maria Lopez", "my husband Dave", any name from your records (any case, one typo) | regex context + known |
150
+ | `PHONE` | 416-555-0142, (416) 555 0142, +44 20 7946 0958, "four one six five five five…" | regex + known |
151
+ | `EMAIL`, `URL`, `IP_ADDRESS` | | regex |
152
+ | `DATE` | 2026-11-03, 11/3, March 5, 1990, Nov 8th, "march fifteenth 1988" (year alone is not masked) | regex + known DOB |
153
+ | `AGE` | ages over 89 only (Safe Harbor) | regex |
154
+ | `ADDRESS`, `POSTAL_CODE`, `ZIP` | 22 Elm Street apt 4, PO Box 12, M5V 2T6, TX 78701 | regex + known |
155
+ | `HEALTH_CARD`, `SIN`, `SSN` | OHIP 1234-567-890-AB, RAMQ, keyword + number, Luhn-checked SIN | regex + known |
156
+ | `MRN`, `ACCOUNT`, `DONOR_ID`, `ID` | "MRN: 00482913", "policy number: XK-99812", "donor D-4521" | regex + known |
157
+ | `PROVIDER` | "Dr. Patel" (passes by default, as clinician names are not patient identifiers; enable with `Policy(disabled_types=set())`) | regex |
158
+
159
+ Optional NER layer: `pip install phiproxy[presidio]` and add `PresidioDetector()` to the detector list. It runs in-process; nothing is sent anywhere to detect PHI.
160
+
161
+ ## Benchmark
162
+
163
+ `benchmark/` holds a synthetic **patient communications** set: SMS, web chat, call transcripts and referral notes, the kind of text clinics actually send to LLMs. Most existing de-identification work targets clinical notes instead. All people and numbers are invented. Run it with `python benchmark/score.py`.
164
+
165
+ v0 results (288 messages, 560 labelled spans):
166
+
167
+ | config | recall | missed |
168
+ |---|---|---|
169
+ | regex | 61.4% | 216 |
170
+ | regex + known entities | **85.7%** | 80 |
171
+
172
+ | recall by channel | regex | regex + known |
173
+ |---|---|---|
174
+ | SMS | 55% | 95% |
175
+ | web chat | 74% | 84% |
176
+ | call transcripts | 50% | 80% |
177
+ | referral notes | 63% | 79% |
178
+
179
+ **How to read this honestly:**
180
+ - v0 was written by the same team that wrote the detectors. Expect real-world recall to be **lower**. An independent, larger set is the next milestone, and contributions are welcome.
181
+ - Precision is reported as 100% on v0, but v0 has almost no non-PHI distractor text, so that number means little yet.
182
+ - Known misses today: **cities and workplaces** ("I live in Sudbury", "I work at the Tim Hortons on King") and **names not in your records written in lowercase** ("my husband ravi"). These need the NER layer or a local model, which is on the roadmap.
183
+
184
+ ## Deployment shapes
185
+
186
+ - **Gateway:** LiteLLM + phiproxy as one container inside your private network, Redis as the shared vault. Apps change only their base URL. This gives one enforcement and audit point.
187
+ - **Library:** call `mask` / `unmask` in your own code. It is the smallest footprint, but every code path must remember to call it.
188
+ - **Sidecar (planned):** a small HTTP wrapper for apps in other languages (.NET, Java, Node).
189
+
190
+ The vault holds real values. Protect Redis like the PHI store it is: private network, auth, TLS, short TTL, no persistence.
191
+
192
+ ## Roadmap
193
+
194
+ - Larger independent benchmark with distractor text, plus multilingual sets (French, Polish, Hebrew, Spanish)
195
+ - Local small-model detector for names, places and workplaces
196
+ - Gazetteer for cities and regions
197
+ - HTTP sidecar; Anthropic `/v1/messages` streaming
198
+ - Masked logging hook for LiteLLM
199
+
200
+ ## Contributing and security
201
+
202
+ See [CONTRIBUTING.md](https://github.com/DecisionTreeTechnology/phiproxy/blob/main/CONTRIBUTING.md) (DCO sign-off required). Report vulnerabilities and detection misses that could leak PHI privately, as described in [SECURITY.md](https://github.com/DecisionTreeTechnology/phiproxy/blob/main/SECURITY.md). **Never put real patient data in issues, tests or benchmark files.**
203
+
204
+ ## License
205
+
206
+ Apache-2.0. Copyright 2026 Decision Tree Technology.
207
+
208
+ Built by [Decision Tree Technology](https://decisiontree.tech/), the team behind [Fertiligent](https://fertiligent.ai/).