Sanitize LLM outputs before HTML, SQL, shell, or markdown sinks. Python port of @mukundakatta/llm-output-sanitizer.
Project description
llm-output-sanitizer-py
Sanitize LLM outputs before HTML, SQL, shell, or markdown sinks. Zero runtime dependencies.
Python port of @mukundakatta/llm-output-sanitizer. The JS sibling has the full design notes; this README sticks to the Python API.
Install
pip install llm-output-sanitizer-py
Usage
from llm_output_sanitizer import sanitize, assert_safe, UnsafeOutputError
text = '<script>alert("pwned")</script> Drop table users; rm -rf /'
result = sanitize(text, sink="markdown")
result.safe # False
result.text # '[removed:html]alert("pwned")[removed:html] [removed:sql] table users; [removed:shell]/'
result.findings # [Finding(kind='html', match='<script>'), ...]
# Tripwire pattern: raise on any flagged content right before sending to the sink.
try:
safe_text = assert_safe(text, sink="html")
except UnsafeOutputError as exc:
log.warning("blocked unsafe LLM output: %s", exc.findings)
Sinks
| Sink | Behavior |
|---|---|
markdown (default) |
Pattern pass only. |
html |
Pattern pass + entity-encode <, >, & in whatever is left. |
sql |
Pattern pass only. |
shell |
Pattern pass only. |
Bundled patterns
| Kind | Catches |
|---|---|
html |
<script>, <iframe>, <object>, <embed>, <form>, <meta>, <link> (open or close tags) |
sql |
drop, truncate, alter, delete from, insert into |
shell |
rm -rf, curl ... |, wget ... |, chmod 777, sudo |
API differences from the JS sibling
sanitize()returns aSanitizeResultdataclass; findings are frozenFindingdataclasses.assert_safe()raisesUnsafeOutputError(aValueErrorsubclass) instead of a plainErrorwith attachedfindings.sinkis a Python keyword arg, not anoptionsobject.
See the JS sibling's README for the full design notes.
License
MIT
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file llm_output_sanitizer_py-0.1.0.tar.gz.
File metadata
- Download URL: llm_output_sanitizer_py-0.1.0.tar.gz
- Upload date:
- Size: 6.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
30873d4eac861e8ced0a2e07ec22a961fc73367213791882d516de4cbe533857
|
|
| MD5 |
361acc255b242ff399200284fba5bb7f
|
|
| BLAKE2b-256 |
76fb1d4a7e89fed634681d334cddaa36f2394ec89a77ba9665acb301aae99c37
|
File details
Details for the file llm_output_sanitizer_py-0.1.0-py3-none-any.whl.
File metadata
- Download URL: llm_output_sanitizer_py-0.1.0-py3-none-any.whl
- Upload date:
- Size: 6.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
36539cb6fb2a4f803eb9b88bf4644b2480427c29276695c2fcbdf53b71e88d29
|
|
| MD5 |
9b147468a69bea87054dbd3bd2dcb001
|
|
| BLAKE2b-256 |
22e4459276ea4374d76cdf3f7f70a3d682b19bb348789125e9ae87bac8c66a4c
|