Skip to main content

LLM security middleware and risk analysis layer for prompt injection detection

Project description

PromptGate

PromptGate is an open-source Python middleware library that sits between a user and an LLM and detects prompt injection risk before the model sees the input. It is a risk classifier, not a moral judge: it accumulates explainable signals and applies policy thresholds.

Architecture

user_input
  → parser          (normalize text, metadata)
  → rule_based      (substring pattern matching)
  → aggregator      (map signals → threat categories)
  → scorer          (sum unique signal severities, cap at 1.0)
  → policy          (ALLOW / FLAG / REVIEW / BLOCK)
  → response        (structured, explainable output)

The LLM never receives blocked prompts when PromptGate is placed in front of the request path.

Installation

From the project root (promptgate/):

pip install -e ".[dev]"

Editable install keeps pattern files and source in sync during development.

Usage

from promptgate import PromptGate

gate = PromptGate()
result = gate.check("Please ignore previous instructions.")

print(result["decision"])      # BLOCK
print(result["confidence"])    # 0.95
print(result["message"])       # Human-readable explanation
print(result["signals"])       # Matched risk signals

Custom policy thresholds:

gate = PromptGate(thresholds={"block": 0.80, "review": 0.60, "flag": 0.35})
result = gate.check(user_input)

Explainability Philosophy

Every response includes:

  • decision — ALLOW, FLAG, REVIEW, or BLOCK
  • confidence — equals the accumulated risk score (0.0 when safe)
  • signals — what matched, with severity and pattern text
  • signals_checked — signal types, categories, and pattern files scanned
  • message — plain-language explanation of why the decision was made

ALLOW responses explicitly state that no injection patterns or manipulation framing were detected above thresholds. Restricted responses name matched signals and categories.

Signal accumulation is required. One weak signal alone (for example, sympathy framing) does not block. Multiple signals combine via score = min(1.0, sum(severities)).

Threat Categories

Category Example signals
direct_injection instruction_override, data_exfiltration
jailbreak jailbreak_persona
system_override system_override, system_prompt_leak
social_engineering authority_claim, secrecy_request, urgency_framing
encoding_attack encoding_trick

Severity values and mappings live in promptgate/config.py.

Default Policy Thresholds

Score range Decision
0.00 – 0.29 ALLOW
0.30 – 0.54 FLAG
0.55 – 0.74 REVIEW
0.75 – 1.00 BLOCK

Local Testing

cd promptgate
pip install -e ".[dev]"
pytest -v

Project Layout

promptgate/
├── promptgate/       # Python package
├── data/patterns/   # Seed pattern files
├── tests/
├── pyproject.toml
└── README.md

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

promptgate_llm-0.5.0.tar.gz (80.9 kB view details)

Uploaded Source

Built Distribution

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

promptgate_llm-0.5.0-py3-none-any.whl (60.9 kB view details)

Uploaded Python 3

File details

Details for the file promptgate_llm-0.5.0.tar.gz.

File metadata

  • Download URL: promptgate_llm-0.5.0.tar.gz
  • Upload date:
  • Size: 80.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.0

File hashes

Hashes for promptgate_llm-0.5.0.tar.gz
Algorithm Hash digest
SHA256 5953cf98e92766449e6975123ac943c8b6d25e2e3d9333b76a35d866974623c1
MD5 493f0d69b05136cb6c39e5f757793c6d
BLAKE2b-256 3f2a611f96e35e1ea161cc3f6cf3eeb0204a17a1256ecc3cf203cc44a54eb990

See more details on using hashes here.

File details

Details for the file promptgate_llm-0.5.0-py3-none-any.whl.

File metadata

  • Download URL: promptgate_llm-0.5.0-py3-none-any.whl
  • Upload date:
  • Size: 60.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.0

File hashes

Hashes for promptgate_llm-0.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 93b69513df1b8bdd2a8d01ce50c90b9fc46792e85526d2bd208e5e6f2f39336e
MD5 301ff67e5c8fd46a096d4fab12772332
BLAKE2b-256 552b1d7ec913f8a30aa751ebc6f62c11954692d917acf89b167b3340420315d7

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