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)
-
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()
-
Call chat
Pass the same
modelandmessagesyou would use with Ollama. Use standardroleandcontentkeys. 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?"}, ], )
-
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"])
-
Other Ollama features
Other calls are passed through to the underlying backend (e.g.
shield.list(),shield.pull("qwen2.5")). Onlychat()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 returnsauthority_text()plus atagsdict. Usetags["system"].wrap(...)andtags["user"].wrap(...)to build the strings you send.Tag— Single tag withshort_idandwrap(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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
895ca7f0353deb2e5f05b902add3e9e5f90166f2fc875aa7d08afb38f16b8223
|
|
| MD5 |
265ace8dd091cfbd6621a5799f4ffe4e
|
|
| BLAKE2b-256 |
02e8bd8e2444b95e26ed7c23ef9de11a2b5fb72611701d1a372883ae3387f17e
|
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c487c214a33e9f74900ce015a162821b36a123358f12b3b52be7d39fd661cadf
|
|
| MD5 |
d76abd009d5769f4d29bbc2126da23fa
|
|
| BLAKE2b-256 |
27407f561543fcd5edc21407b1e710ea591ea02b21651fde36f528977a9bbafd
|