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

Rename Compatibility

The project was originally published as llm-input-harderning. The corrected package is llm-input-hardening, with the canonical import path llm_input_hardening.

The legacy llm_input_harderning import path remains available as a compatibility shim for existing code, but new code should use llm_input_hardening.

For PyPI migration, publish the corrected package first, then publish the compatibility meta-package in packaging/llm-input-harderning-shim so existing pip install llm-input-harderning users are moved onto 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 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.1.0.tar.gz (224.8 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.1.0-cp310-abi3-win_amd64.whl (275.2 kB view details)

Uploaded CPython 3.10+Windows x86-64

llm_input_hardening-1.1.0-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (419.7 kB view details)

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

llm_input_hardening-1.1.0-cp310-abi3-macosx_11_0_arm64.whl (376.1 kB view details)

Uploaded CPython 3.10+macOS 11.0+ ARM64

File details

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

File metadata

  • Download URL: llm_input_hardening-1.1.0.tar.gz
  • Upload date:
  • Size: 224.8 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.1.0.tar.gz
Algorithm Hash digest
SHA256 02ebdc54ecb5d3ce1363991d4602a3f02134ca87e9c233b7935e2e76f155e479
MD5 13abb06b1636306d1d8718742a3f37ff
BLAKE2b-256 33e1d87d8f2f5876ed3915ad26f80fd4974d0faae4c17a34c5e47924b51fe373

See more details on using hashes here.

Provenance

The following attestation bundles were made for llm_input_hardening-1.1.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.1.0-cp310-abi3-win_amd64.whl.

File metadata

File hashes

Hashes for llm_input_hardening-1.1.0-cp310-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 bcb82ef4d4b7e1652aad88f02c81f0a5875561b285f770775abd31a3783737ac
MD5 30fb495bb67f79890e514dec6f2b9e0f
BLAKE2b-256 ef28fdc3287edae9bf9af0968a33a52113cc6127ff971eeaf802c4a2f5c45d1d

See more details on using hashes here.

Provenance

The following attestation bundles were made for llm_input_hardening-1.1.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.1.0-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for llm_input_hardening-1.1.0-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 ef3ecc5d74fb88280b66fc38acb5c2959ce9104f1066055c9dbaddab7748d85f
MD5 bcac37bc5b12281f4ea72c4e19ceabbd
BLAKE2b-256 6f432c96e080ca997817ce28fbeff6e5ded7ff6145647093387df27d532a09cf

See more details on using hashes here.

Provenance

The following attestation bundles were made for llm_input_hardening-1.1.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.1.0-cp310-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for llm_input_hardening-1.1.0-cp310-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 3f4d2eef760359451ce4c048665a0d51bb53a2f34fce2a790ccc3dd6c0f9de10
MD5 740f8507e82cde9690970600a1854517
BLAKE2b-256 685a55e64b7f9d22b8cdf983675f523fe7d1e39245ac5cd4ddd96139b207f9f9

See more details on using hashes here.

Provenance

The following attestation bundles were made for llm_input_hardening-1.1.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