Skip to main content

RAGDefender

Efficient post-retrieval defense against knowledge corruption attacks on Retrieval-Augmented Generation (RAG) systems. Filters out adversarial passages injected by PoisonedRAG / GARAG / Tan et al. before they reach your generator, without retraining or extra LLM calls.

Official artifact for "Rescuing the Unpoisoned: Efficient Defense against Knowledge Corruption Attacks on RAG Systems" — Kim, Lee, Koo (Sungkyunkwan University), ACSAC 2025. DOI: 10.1109/ACSAC67867.2025.00093.

Install

pip install ragdefender

Use

from ragdefender import RAGDefender

defender = RAGDefender(task_type="single_hop")  # or "multi_hop" for HotpotQA-style queries

safe_passages = defender.defend(
    query="What is the capital of France?",
    R=[
        "Paris is the capital of France, on the Seine.",
        "Lyon is the capital of France per 2024 records.",   # adversarial
        "Tourists visit Paris, the capital of France.",
        "The capital of France is Lyon, a major city.",      # adversarial
    ],
)

safe_passages contains the survivors after Stage 1 (estimate $N_{adv}$, paper §4.1) and Stage 2 (pair-frequency TopK ranking, paper §4.2) drop the detected adversarial passages.

Inside your RAG framework

pip install "ragdefender[langchain]"     # or: llama-index | haystack | guardrails | integrations (all four)
from ragdefender.integrations import RAGDefenderCompressor     # LangChain   BaseDocumentCompressor
from ragdefender.integrations import RAGDefenderPostprocessor  # LlamaIndex  BaseNodePostprocessor
from ragdefender.integrations import RAGDefenderFilter         # Haystack 2  @component
from ragdefender.integrations import RAGDefenderValidator      # Guardrails  Validator

Framework-native import paths are available too: pip install llama-index-postprocessor-ragdefender gives llama_index.postprocessor.ragdefender.RAGDefenderPostprocessor, and the [guardrails] extra ships guardrails_ai.ragdefender.RAGDefenderValidator.

Each adapter takes task_type=... (or a pre-built defender=), keeps your framework's document objects and retrieval order, and slots in right after the retriever. Guide: https://github.com/SecAI-Lab/RAGDefender/blob/main/docs/integrations.md.

CLI

ragdefender info
ragdefender defend --query "..." --corpus passages.json --task-type single_hop
ragdefender evaluate --test-data test.json --attack poisonedrag --task-type single_hop

Migrating from v0.1.1

mode='multihop' → task_type='multi_hop', similarity_model= → embedder=, --attack blind → --attack tan-et-al. Old spellings still work but emit DeprecationWarning. Full rename table: https://github.com/SecAI-Lab/RAGDefender/blob/main/docs/migration-0.1-to-0.2.md.

Documentation

Citation

@inproceedings{kim2025ragdefender,
  title     = {Rescuing the Unpoisoned: Efficient Defense against
               Knowledge Corruption Attacks on RAG Systems},
  author    = {Kim, Minseok and Lee, Hankook and Koo, Hyungjoon},
  booktitle = {Annual Computer Security Applications Conference (ACSAC)},
  year      = {2025},
  doi       = {10.1109/ACSAC67867.2025.00093},
}

License

MIT. Intended for research and defensive use only.

Metadata

Release files for ragdefender 0.3.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for ragdefender 0.3.0
File Size Uploaded Provenance Actions
ragdefender-0.3.0.tar.gz 32.3 kB Without provenance Details for ragdefender-0.3.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for ragdefender 0.3.0
File Interpreter ABI Platform Actions
ragdefender-0.3.0-py3-none-any.whl Python 3 none any Details for ragdefender-0.3.0-py3-none-any.whl

Total release size: 72.0 kB

Release files / ragdefender-0.3.0.tar.gz

Download URL ragdefender-0.3.0.tar.gz
Size 32.3 kB
Tags Source
SHA-256 checksum
How to use checksums
d04905219563b105531ca0cf45f131f8023bc4eef804302d7c387028fb188d52
BLAKE2b-256 checksum
How to use checksums
28f465d9e20c9dcf507f7306af71ffd75d4bffeec26eac7514143b5e109e0243
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.10.12

Release files / ragdefender-0.3.0-py3-none-any.whl

Download URL ragdefender-0.3.0-py3-none-any.whl
Size 39.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
4325775f546f0c3b8ad07bb21cf98cb7e02ce1c10797c7beea28efba56c03805
BLAKE2b-256 checksum
How to use checksums
e3b519d0f427db71ec0a0e7a6125106cc7fb77dfebec83ea9c8a18b50ea7268b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.10.12

Security for ragdefender 0.3.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page