Skip to main content

LLM Input Sanitizer

A Python package for sanitizing and securing user inputs before sending them to large language models (LLMs).

Features

  • PII Detection & Masking: Automatically detects and masks emails, phone numbers, SSNs, and credit card numbers
  • Profanity Filtering: Removes or masks profanity and inappropriate language
  • Input Truncation: Prevents excessively long inputs
  • Unicode Normalization: Handles special characters and ensures consistent text encoding
  • Prompt Injection Defense: Detects and blocks common LLM prompt injection attacks
  • Jailbreak Prevention: Identifies attempts to bypass LLM safety measures

Installation

pip install llm-input-sanitizer

Quick Start

from llm_input_sanitizer import InputSanitizer, prepare_llm_messages, is_input_appropriate

# Initialize the sanitizer
sanitizer = InputSanitizer(max_length=1000)

# Sanitize user input
user_input = "My email is john@example.com and my phone is 555-123-4567"
sanitized_input = sanitizer.sanitize_input(user_input)
# Result: "My email is [EMAIL] and my phone is [PHONE]"

# Check if input is appropriate (no injection attempts)
if is_input_appropriate(sanitized_input):
    # Prepare messages for the LLM
    messages = prepare_llm_messages(
        sanitized_input, 
        system_message="You are a helpful assistant."
    )
    # Send messages to your LLM
else:
    print("Potentially harmful input detected")

Custom Profanity list

sanitizer = InputSanitizer(profanity_file="path/to/profanity_words.txt")

Custom forbidden paths

from llm_input_sanitizer import is_input_appropriate

my_patterns = [
    r'custom_pattern_1',
    r'custom_pattern_2',
]

is_safe = is_input_appropriate(text, forbidden_patterns=my_patterns)

Integration with OpenAI

import openai
from llm_input_sanitizer import InputSanitizer, prepare_llm_messages, is_input_appropriate

sanitizer = InputSanitizer()

def safe_llm_call(user_input):
    # Sanitize the input
    clean_input = sanitizer.sanitize_input(user_input)
    
    # Check if appropriate
    if not is_input_appropriate(clean_input):
        return "I'm sorry, I can't process that request."
    
    # Prepare messages
    messages = prepare_llm_messages(clean_input)
    
    # Call the LLM API
    response = openai.ChatCompletion.create(
        model="gpt-4",
        messages=messages
    )
    
    return response.choices[0].message.content

This package provides a baseline of protection against common attacks but is not a complete security solution. Always implement defense in depth for production systems:

  • Server-side validation
  • Rate limiting
  • Monitoring for unusual patterns
  • Regular updates to security patterns

Metadata

Release files for llm-input-sanitizer 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 llm-input-sanitizer 0.2.1
File Size Uploaded Provenance
llm_input_sanitizer-0.2.1.tar.gz 23.2 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for llm-input-sanitizer 0.2.1
File Interpreter ABI Platform
llm_input_sanitizer-0.2.1-py3-none-any.whl Python 3 none any Details

Total release size: 47.6 kB

Release files / llm_input_sanitizer-0.2.1.tar.gz

Download URL llm_input_sanitizer-0.2.1.tar.gz
Size 23.2 kB
Tags Source
SHA-256 checksum
How to use checksums
e540e6812deead83aa08af67df1eebb06c3c0f90bbd67517a2b811c377dee85e
BLAKE2b-256 checksum
How to use checksums
673a9d93f05c7517e240bb665252c69cab99f2088eb8e042917a076b8f46ed1c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.1

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

Download URL llm_input_sanitizer-0.2.1-py3-none-any.whl
Size 24.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
ad4fe9a4a390cbc9d315935bf204ee9b4143a9dcdeb5f85fcb611f7335ca3d9a
BLAKE2b-256 checksum
How to use checksums
5d350a14db3a4ab838b313a077c795f946a24cb1ea54e822d9bdf829f556e190
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.1

Security for llm-input-sanitizer 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.

No provenance found

None of the files in this release were uploaded with provenance metadata.

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