Skip to main content

Sanitize LLM outputs before HTML, SQL, shell, or markdown sinks. Python port of @mukundakatta/llm-output-sanitizer.

Project description

llm-output-sanitizer-py

PyPI Python License: MIT

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 a SanitizeResult dataclass; findings are frozen Finding dataclasses.
  • assert_safe() raises UnsafeOutputError (a ValueError subclass) instead of a plain Error with attached findings.
  • sink is a Python keyword arg, not an options object.

See the JS sibling's README for the full design notes.

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

llm_output_sanitizer_py-0.1.0.tar.gz (6.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

llm_output_sanitizer_py-0.1.0-py3-none-any.whl (6.2 kB view details)

Uploaded Python 3

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

Hashes for llm_output_sanitizer_py-0.1.0.tar.gz
Algorithm Hash digest
SHA256 30873d4eac861e8ced0a2e07ec22a961fc73367213791882d516de4cbe533857
MD5 361acc255b242ff399200284fba5bb7f
BLAKE2b-256 76fb1d4a7e89fed634681d334cddaa36f2394ec89a77ba9665acb301aae99c37

See more details on using hashes here.

File details

Details for the file llm_output_sanitizer_py-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for llm_output_sanitizer_py-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 36539cb6fb2a4f803eb9b88bf4644b2480427c29276695c2fcbdf53b71e88d29
MD5 9b147468a69bea87054dbd3bd2dcb001
BLAKE2b-256 22e4459276ea4374d76cdf3f7f70a3d682b19bb348789125e9ae87bac8c66a4c

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page