Skip to main content

Deterministic Unicode-aware input hardening for LLM applications.

Project description

llm-input-hardening

Deterministic Unicode-aware input hardening for LLM applications.

llm-input-hardening sits between untrusted text and your prompt or template layer. It normalizes Unicode, removes invisible or control characters that destabilize prompts, emits structured reports for logging and enforcement, and can add confusable mixed-script signals when you want stricter checks.

It is not prompt injection prevention. It addresses text-integrity problems, not model intent.

This repository keeps docs intentionally lightweight. The README is the main reference.

Install

pip install llm-input-hardening

Optional extras:

  • pip install "llm-input-hardening[security]" for the confusable_homoglyphs backend
  • pip install "llm-input-hardening[ftfy]" for pre-sanitize text repair

Import from llm_input_hardening.

Quickstart

from llm_input_hardening import sanitize

clean_text, report = sanitize(user_text, policy="balanced_chat")

Recursive JSON sanitization:

from llm_input_hardening import sanitize_json

clean_payload, report = sanitize_json(payload, policy="balanced_chat")

Enforcement-oriented usage:

from llm_input_hardening import sanitize_and_decide

clean_text, report, decision = sanitize_and_decide(
    user_text,
    sanitize_policy="strict_exec",
    confusables_backend="confusable_homoglyphs",
)

Policies

Policy Normalization Default-Ignorables Whitespace tidy Typical use
preserve NFC preserve no low-friction logging and observability
balanced_chat NFC preserve yes general chat and prompt input
strict_exec NFKC strip yes tool arguments and execution-adjacent paths
code_mode NFC strip no code snippets and syntax-sensitive text

Backward-compatible aliases are still accepted: balanced, strict, and code.

Scope

Use this library for:

  • user text before prompt interpolation
  • retrieved text before it reaches the model
  • tool and function argument strings
  • JSON payloads that contain prompt-bearing string leaves

Do not use it as a substitute for:

  • system prompt design
  • tool sandboxing and allowlists
  • output validation
  • human review for high-risk actions

CLI

llm-input-hardening sanitize --text "abc‮def"
llm-input-hardening inspect --text "abc‮def"
llm-input-hardening decide --text "abc‮def"

Development

uv sync --all-extras
uv run maturin develop --release
uv run python -m pytest -q

Benchmarks And Bakeoff

The repo has two different evaluation paths.

  • bench/run_benchmarks.py is a timing harness. It measures sanitizer latency and throughput on fixed synthetic inputs.
  • bench/run_bakeoff.py is an evaluation harness. It runs labeled corpus cases, checks expected detections and postconditions, and computes precision, recall, benign-change rate, and stability signals.

Use the Make targets below:

make bench
make bench-plots

make bakeoff-smoke
make bakeoff-smoke-assert

make bakeoff-full
make bakeoff-full-assert
make bakeoff-diff
make bakeoff-diff-assert

What they mean:

  • bakeoff-smoke is the fast local check. It evaluates only llm-input-hardening and writes results to bench/results/bakeoff_smoke_latest.json.
  • bakeoff-full is the heavyweight comparison run. It installs llm-guard and confusable-homoglyphs, then writes comparable results to bench/results/bakeoff_latest.json.
  • bakeoff-diff compares the current full run to bench/results/bakeoff_baseline_v1.0.0.json.

Generated reports stay under bench/results/.

Repo Notes

  • CONTRIBUTING.md covers contribution mechanics.
  • SECURITY.md covers reporting and threat assumptions.
  • SCOPE.md explains what the project is and is not trying to solve.

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_input_hardening-1.2.0.tar.gz (223.4 kB view details)

Uploaded Source

Built Distributions

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

llm_input_hardening-1.2.0-cp310-abi3-win_amd64.whl (274.0 kB view details)

Uploaded CPython 3.10+Windows x86-64

llm_input_hardening-1.2.0-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (418.5 kB view details)

Uploaded CPython 3.10+manylinux: glibc 2.17+ x86-64

llm_input_hardening-1.2.0-cp310-abi3-macosx_11_0_arm64.whl (374.9 kB view details)

Uploaded CPython 3.10+macOS 11.0+ ARM64

File details

Details for the file llm_input_hardening-1.2.0.tar.gz.

File metadata

  • Download URL: llm_input_hardening-1.2.0.tar.gz
  • Upload date:
  • Size: 223.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for llm_input_hardening-1.2.0.tar.gz
Algorithm Hash digest
SHA256 5cbb1de110e3372149ef97e3549be98199d9f68e0fdb62a5e10457e40caf17f6
MD5 a9b25c8c5be23e6d4d23e33ec893bde3
BLAKE2b-256 d253361b188601dea843211bc6d40d7cd447ec13c4b34e41f50d4a73503fc968

See more details on using hashes here.

Provenance

The following attestation bundles were made for llm_input_hardening-1.2.0.tar.gz:

Publisher: release.yml on davidwebstar34/llm-input-hardening

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file llm_input_hardening-1.2.0-cp310-abi3-win_amd64.whl.

File metadata

File hashes

Hashes for llm_input_hardening-1.2.0-cp310-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 98ab5ebf6633ee38443551e1f4fa97d8d815cc6d20bacd764944ca6d0dec5c65
MD5 951726fc682b6491e347a6d06d1ae7b5
BLAKE2b-256 1da5cca87a97180545169c2fa076f6a0522f31330b0b740a6e83ca7862c826c0

See more details on using hashes here.

Provenance

The following attestation bundles were made for llm_input_hardening-1.2.0-cp310-abi3-win_amd64.whl:

Publisher: release.yml on davidwebstar34/llm-input-hardening

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file llm_input_hardening-1.2.0-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for llm_input_hardening-1.2.0-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 423fde3bda7c00c5445125e14fc44b1ca6583dead648dae795aad91381765c35
MD5 ac831c2953f0c999c8d75d5710a53b88
BLAKE2b-256 bb4b409470b199b04f98923053229d71edb4f59a1c118fc9bd68ddbd69090d06

See more details on using hashes here.

Provenance

The following attestation bundles were made for llm_input_hardening-1.2.0-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: release.yml on davidwebstar34/llm-input-hardening

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file llm_input_hardening-1.2.0-cp310-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for llm_input_hardening-1.2.0-cp310-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 337b2e15c8d0a4a8d8bc33ae8160eb9ce1e0b5db57b9311fc4637677594bd768
MD5 a1b564d0bb101a103401962049032389
BLAKE2b-256 083940ab8096424714bf32108264f0d40eae16a761b012f86d6e9031b71d2c05

See more details on using hashes here.

Provenance

The following attestation bundles were made for llm_input_hardening-1.2.0-cp310-abi3-macosx_11_0_arm64.whl:

Publisher: release.yml on davidwebstar34/llm-input-hardening

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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