Skip to main content

Lightweight prompt-injection mitigation for LLM applications

Project description

LightShield

LightShield is a lightweight Python middleware that reduces prompt injection risk by tagging system and user content and enforcing a strict instruction hierarchy before prompts are sent to an LLM. It does not modify model weights or call a separate classifier—only prompt construction and response sanitization.

Install

pip install ollama   # required for the Ollama shield

Clone or install the LightShield package so you can import it.

Use (Ollama)

  1. Import and create a shield

    Choose the engine (e.g. "ollama"). The shield wraps that backend so every chat call is tagged and responses are sanitized.

    from lightshieldai import Shield
    
    shield = Shield()
    
  2. Call chat

    Pass the same model and messages you would use with Ollama. Use standard role and content keys. LightShield uses system and user messages only (RAG/retrieved layers are for a later release).

    response = shield.chat(
        model="qwen2.5",
        messages=[
            {"role": "system", "content": "You are a helpful assistant."},
            {"role": "user", "content": "What is 2+2?"},
        ],
    )
    
  3. Read the response

    The returned object has the same shape as Ollama’s response. The message content is sanitized so internal LightShield tags are not exposed.

    print(response["message"]["content"])
    
  4. Other Ollama features

    Other calls are passed through to the underlying backend (e.g. shield.list(), shield.pull("qwen2.5")). Only chat() is wrapped and sanitized. Streaming is disabled so that responses can be sanitized reliably.

What LightShield does

  • Before the call: Builds a system message that includes an authority/hierarchy paragraph and wraps your system and user text in unique tags so the model sees clear boundaries and priorities (system over user).
  • After the call: Strips those tags from the model’s reply so they never reach your application.

Building blocks (advanced)

If you want to plug LightShield into another API or build your own flow, you can use the lower-level pieces:

  • LayerPrompt — Creates one tag per layer (system, user, retrieved) and returns authority_text() plus a tags dict. Use tags["system"].wrap(...) and tags["user"].wrap(...) to build the strings you send.
  • Tag — Single tag with short_id and wrap(content) for <LS_id>content</LS_id>.

You would then call your own LLM with the wrapped prompts and implement sanitization (strip <LS_*> and </LS_*>) using the same tag ids from that LayerPrompt instance.

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

lightshieldai-0.1.0.tar.gz (6.3 kB view details)

Uploaded Source

Built Distribution

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

lightshieldai-0.1.0-py3-none-any.whl (7.5 kB view details)

Uploaded Python 3

File details

Details for the file lightshieldai-0.1.0.tar.gz.

File metadata

  • Download URL: lightshieldai-0.1.0.tar.gz
  • Upload date:
  • Size: 6.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.2

File hashes

Hashes for lightshieldai-0.1.0.tar.gz
Algorithm Hash digest
SHA256 895ca7f0353deb2e5f05b902add3e9e5f90166f2fc875aa7d08afb38f16b8223
MD5 265ace8dd091cfbd6621a5799f4ffe4e
BLAKE2b-256 02e8bd8e2444b95e26ed7c23ef9de11a2b5fb72611701d1a372883ae3387f17e

See more details on using hashes here.

File details

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

File metadata

  • Download URL: lightshieldai-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 7.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.2

File hashes

Hashes for lightshieldai-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c487c214a33e9f74900ce015a162821b36a123358f12b3b52be7d39fd661cadf
MD5 d76abd009d5769f4d29bbc2126da23fa
BLAKE2b-256 27407f561543fcd5edc21407b1e710ea591ea02b21651fde36f528977a9bbafd

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