Welcome to pytector’s documentation!
pytector is a Python package for detecting prompt injections in text using Open-Source Large Language Models (LLMs), designed to provide immediate security controls beyond foundation model defaults.
Contents:
- Installation
- Quick Start Guide
- API Reference
- LangChain Integration
- Examples
- Basic Detection Examples
- Batch Processing
- Custom Thresholds
- Different Model Types
- Keyword-Based Security Blocking
- Custom Keyword Lists for Specific Use Cases
- Dynamic Security Policy Updates
- LangChain LCEL Guardrail
- Input Sanitization
- PII Detection
- Toxicity Detection
- Regex Scanner (Customizable)
- Canary Tokens (System Prompt Leak Detection)
- Error Handling
- Integration Examples
- Command Line Usage
- Contributing to pytector
Features
Prompt Injection Detection: Uses open-source language models for prompt injection detection
Content Safety: Support for Groq-hosted safeguard models for safety detection
Keyword-Based Blocking: Restrictive keyword filtering for immediate security control
Input Sanitization: Six-strategy pipeline to clean injection content from user input (encoding detection, unicode normalization, pattern removal, sentence scoring, fuzzy matching, keyword stripping) with zero additional dependencies
PII Detection: NER-based PII scanning using PasteProof PII Detector (ModernBERT, F1 0.97) covering 27 entity types — financial, credential, healthcare, GDPR, identity, contact, and address data
Toxicity Detection: Multilingual toxicity classification using citizenlab DistilBERT (F1 0.94, 10 languages)
Regex Scanner: Customizable rule-based pattern matching for PII and credentials (email, phone, SSN, credit card, IP, API keys, JWT) using pure Python stdlib
Canary Tokens: System prompt leak detection — inject a secret token and verify the model never repeats it. Zero dependencies, zero calibration
Multiple Model Backends: Support for Hugging Face Transformers and GGUF models
Rapid Deployment: Designed for quick integration into projects needing immediate security layers
Configurable: Customizable detection parameters, thresholds, and security policies
LangChain Integration: LCEL-compatible guardrail runnable for pre-model prompt checks
Quick Start
Install the package:
pip install pytector
Basic usage:
from pytector import PromptInjectionDetector
detector = PromptInjectionDetector()
is_injection, probability = detector.detect_injection("Hello, how are you?")
print(f"Injection detected: {is_injection}")
For more detailed information, see the Quick Start Guide guide.