Skip to main content

RagContextGuard

Prevent sensitive information leakage in RAG pipelines.

RagContextGuard is a lightweight Python middleware that analyzes retrieved document chunks before they reach your LLM. It detects when innocent-looking chunks combine to reveal secrets.

Installation

pip install -e .

This installs rag-context-guard with its dependencies (pyyaml).

Quick Demo

Run the included demo to see RagContextGuard in action:

python demo.py

The demo simulates a RAG retrieval with 3 chunks — one combines financial data with an entity identifier, triggering a policy violation.

Running Tests

The project includes a comprehensive test suite. Run it to verify the installation:

python test_comprehensive.py

Expected output: [PASS] All tests passed! (9/9 tests). If you've added custom policies or modified the engine, run this to verify correctness.

How It Works

  1. Classifications: Each chunk carries one or more labels (e.g., "financial", "pii", "confidential"). These come from your existing metadata or an optional LLM classifier you provide.
  2. Set Analysis: RagContextGuard checks whether the union of all chunk classifications satisfies any policy rule's trigger.all_present condition.
  3. Path Analysis: Multi-hop collusion detection finds when a shared classification bridges two otherwise-safe rules, creating a dangerous combination.
  4. Actions: Rules can block (halt pipeline) or warn (log only).

Policy Format

Policies are YAML files. Create your own or use the built-ins.

rules:
  - name: financial_entity_reveal
    description: "Block combining financial metrics with entity identifiers"
    action: block   # or "warn"
    trigger:
      all_present:
        - financial
        - entity_identifier
    message: "Financial data combined with entity identifier — client financials exposed"

Matching: A rule fires when all classifications listed in trigger.all_present appear across the retrieved chunk set (any chunk, any order). A chunk may carry multiple classifications.

Usage

from rag_context_guard import GuardMiddleware, policies

# Use built-in policy
guard = GuardMiddleware(policies.DEFAULT)

# Or your custom file
# guard = GuardMiddleware("my_policy.yaml")

Chunk format

Each chunk is a dict with:

  • text (str): The document content
  • meta (dict): At minimum, include classification (str or list) or classifications (list)

Any extra metadata fields are ignored by the policy engine.

Built-in Policies

Policy Use case Rules
policies.DEFAULT General data leakage prevention 7
policies.FINANCE Financial services confidentiality 6
policies.HIPAA Protected Health Information (PHI) 6
policies.GDPR EU personal data compliance 6
from rag_context_guard import GuardMiddleware, policies

guard = GuardMiddleware(policies.FINANCE)

Optional: LLM-Based Classification

If your chunks lack pre-computed classification metadata, you can provide a classifier function that runs an LLM to tag chunks:

from rag_context_guard import GuardMiddleware
from langchain_ollama import OllamaLLM   # pip install langchain-ollama

The classifier is called only for chunks without existing classifications.

Running Tests

The project includes a comprehensive test suite:

python test_comprehensive.py

Expected output: [PASS] All tests passed! (9/9 tests). If you've added custom policies or modified the engine, run this to verify correctness.

API Reference

GuardMiddleware

Main entry point.

GuardMiddleware(policy_path=None, classifier=None)
  • policy_path: BuiltinPolicy constant or path to YAML file (default: built-in DEFAULT)
  • classifier: Optional callable (str) -> str | list[str] | None

Methods:

  • analyze(chunks: List[dict]) -> AnalysisResult
  • get_rules() -> List[PolicyRule]

AnalysisResult

Returned by analyze():

  • is_safe: bool — True if no block violations
  • violations: List[Violation] — all blocking issues
  • warnings: List[Violation] — all warn-level issues
  • safe_subset: List[Chunk] — chunks not involved in any block violation
  • risk_explanation: str — human-readable summary

Violation

  • rule_name: str
  • message: str
  • triggering_chunks: List[Chunk]
  • action: Action (Action.BLOCK or Action.WARN)

Creating Custom Policies

Copy src/rag_context_guard/policies/example.yaml and adapt:

rules:
  - name: healthcare_pii_exposure
    action: block
    trigger:
      all_present: [healthcare, pii]
    message: "Healthcare data combined with PII — exposure risk"

Then load it:

guard = GuardMiddleware("my_policy.yaml")

Project Structure

rag-context-guard/
├── src/rag_context_guard/
│   ├── guard.py           # GuardMiddleware
│   ├── graph_analyzer.py  # Policy evaluation engine
│   ├── policy.py          # YAML policy loader
│   ├── models.py          # Dataclasses (Chunk, Violation, AnalysisResult)
│   └── policies/          # Built-in YAML policies
│       ├── default.yaml
│       ├── finance.yaml
│       ├── hipaa.yaml
│       └── gdpr.yaml
├── demo.py                # Example usage (no external LLM needed)
├── test_comprehensive.py  # Full test suite
├── pyproject.toml         # Dependency + build config
└── README.md

License

MIT

Metadata

Release files for rag-context-guard 0.1.1

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

Source distribution (sdist)

Source distribution for rag-context-guard 0.1.1
File Size Uploaded Provenance Actions
rag_context_guard-0.1.1.tar.gz 13.7 kB Without provenance Details for rag_context_guard-0.1.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for rag-context-guard 0.1.1
File Interpreter ABI Platform Actions
rag_context_guard-0.1.1-py3-none-any.whl Python 3 none any Details for rag_context_guard-0.1.1-py3-none-any.whl

Total release size: 28.3 kB

Release files / rag_context_guard-0.1.1.tar.gz

Download URL rag_context_guard-0.1.1.tar.gz
Size 13.7 kB
Tags Source
SHA-256 checksum
How to use checksums
4155e315c9f3c690d8d85a13027481b7c4d9b575903b3ec5c6ebe05cccef9221
BLAKE2b-256 checksum
How to use checksums
e1582d134fc341a24af3df20b1e8054d0c0e8aef18331fbf3cb40de33b5b5bc2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.4

Release files / rag_context_guard-0.1.1-py3-none-any.whl

Download URL rag_context_guard-0.1.1-py3-none-any.whl
Size 14.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
525fb543a2618fd9d220f3b03d08bc4cc9565d3edb2c82fd6663885c4269a133
BLAKE2b-256 checksum
How to use checksums
48e5e0f3e160fb829a8c65a390479e914d36dba585314c20876d5a292a598d25
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.4

Security for rag-context-guard 0.1.1

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.

Release history Release notifications | RSS feed

This release

0.1.1 This release

2 release files No provenance

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