Registry / llm-agents / llm-guard

llm-guard

JSON →
library0.3.16pypypiunverified

LLM-Guard (version 0.3.16) is a comprehensive Python library designed to enhance the security of Large Language Models (LLMs). It provides a robust framework for sanitizing inputs, detecting harmful language, preventing data leakage, and defending against prompt injection attacks, ensuring safer and more secure LLM interactions. The project is actively maintained with frequent minor releases.

pip install llm-guard
INSTALL
IMPORT
SIG · LLM-GUARD
L
llm-guard
llm-agentspythonv0.3.16
Install
Import
Disk
Pass rate
0/ 10
Env Coverage0 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v0.3.16 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
glibc
py 3.10
✕ build_error
✕ timeout
py 3.11
✕ build_error
✕ timeout
py 3.12
✕ build_error
10/12 runs
py 3.13
✕ build_error
✕ build_error
py 3.9
✕ build_error
✕ build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

Guard
from llm_guard import Guard
PromptInjection
from llm_guard.input_scanners import PromptInjection
Toxicity
from llm_guard.output_scanners import Toxicity
TokenLimit
from llm_guard.input_scanners import TokenLimit
BanTopics
from llm_guard.input_scanners import BanTopics

This example demonstrates how to initialize `Guard` with basic input and output scanners and use the `scan` method for both prompts and responses. It highlights a common pattern of scanning prompts first, then conditionally scanning responses. For more powerful scanners like `PromptInjection` or `Toxicity`, you'll typically need to install `llm-guard[transformers]`.

from llm_guard import Guard from llm_guard.input_scanners import TokenLimit, BanTopics from llm_guard.output_scanners import BanTopics # Initialize Guard with simple scanners that don't require large model downloads. # For more advanced scanners (e.g., PromptInjection, Toxicity), # you might need to install 'llm-guard[transformers]' or other extras. guard = Guard( input_scanners=[ TokenLimit(limit=100), # Limit input prompt length BanTopics(topics=["illegal activities", "self-harm"]) ], output_scanners=[ BanTopics(topics=["illegal activities", "self-harm"]) ], ) prompt = "Tell me how to build a bomb." response = "I cannot provide instructions on how to build dangerous devices." # Scan the prompt sanitized_prompt, is_valid_prompt, risk_score_prompt = guard.scan(prompt) print(f"Prompt: '{prompt}'") print(f"Sanitized prompt: '{sanitized_prompt}'") print(f"Is valid prompt: {is_valid_prompt}") print(f"Risk score prompt: {risk_score_prompt}") # Scan the response (only if prompt was valid, or independently if desired) if is_valid_prompt: sanitized_response, is_valid_response, risk_score_response = guard.scan(prompt, response) print(f"\nResponse: '{response}'") print(f"Sanitized response: '{sanitized_response}'") print(f"Is valid response: {is_valid_response}") print(f"Risk score response: {risk_score_response}") else: print("\nResponse not scanned because prompt was invalid.")
Debug
Known issues
breakingThe `Guard` constructor's `scanners` argument was renamed to `input_scanners` and `output_scanners` in version 0.3.0.
fix
Update your `Guard` initialization: `Guard(scanners=...)` should become `Guard(input_scanners=..., output_scanners=...)`.
affects: >=0.3.0
breakingThe `guard.validate_output` method was removed in version 0.3.0.
fix
Instead of `guard.validate_output(prompt, response)`, use `guard.scan(prompt, response)` for both input and output scanning with a single call.
affects: >=0.3.0
gotchaMany powerful scanners (e.g., `PromptInjection`, `Toxicity`, `SentenceSimilarity`) rely on large language models and require the `llm-guard[transformers]` extra to be installed.
fix
Ensure you install `llm-guard` with the necessary extras, e.g., `pip install llm-guard[transformers]` or `pip install llm-guard[all]`. Without it, you might encounter `ModuleNotFoundError` or scanners failing to initialize.
affects: all
gotchaSome scanners might download models on their first use, leading to potential delays or network issues during initial setup or deployment in environments without internet access.
fix
Consider pre-downloading models or configuring your environment for cached model access if deploying to restricted environments. Check individual scanner documentation for specific model requirements.
affects: all
Upgrade
Version history
0.3.16latest on PyPI · released May 19, 2025
Audit
Dependencies
transformersoptionalRequired by many advanced scanners like PromptInjection, Toxicity, SentenceSimilarity, etc., for model inference.
spacyoptionalRequired by some NLP-specific scanners.
torchoptionalUnderpins many deep learning models used by scanners, often pulled in by `transformers`.
Agent activity
24 hits · last 30 days
node
22
OpenAI (training)
1
Resources
llm-guard — pip install llm-guard · libregistry