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-guardVerified import paths — ran on the pinned version, not inferred.
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]`.
Update your `Guard` initialization: `Guard(scanners=...)` should become `Guard(input_scanners=..., output_scanners=...)`.
Instead of `guard.validate_output(prompt, response)`, use `guard.scan(prompt, response)` for both input and output scanning with a single call.
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.
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.