Skip to main content

LlamaIndex TrustGuard

Security integration for LlamaIndex - protect your RAG pipeline from prompt injection and malicious content.

Installation

pip install llama-index-trustguard

Features

  • TrustGuardReader - Scan documents before indexing
  • TrustGuardNodePostprocessor - Scan retrieved nodes before using as context

Quick Start

Protected Document Loading

Scan documents for threats before indexing:

from llama_index.core import SimpleDirectoryReader, VectorStoreIndex
from llama_index_trustguard import TrustGuardReader

# Wrap your reader with threat protection
base_reader = SimpleDirectoryReader("./documents")
reader = TrustGuardReader(
    base_reader,
    api_key="ta_xxx...",
    on_threat="filter",  # Skip documents with threats
)

# Only safe documents are loaded
documents = reader.load_data()

# Build index with safe documents only
index = VectorStoreIndex.from_documents(documents)

Protected Query Engine

Scan retrieved nodes before using them as context:

from llama_index.core import VectorStoreIndex
from llama_index_trustguard import TrustGuardNodePostprocessor

# Create your index
index = VectorStoreIndex.from_documents(documents)

# Add TrustGuard postprocessor
postprocessor = TrustGuardNodePostprocessor(
    api_key="ta_xxx...",
    on_threat="filter",  # Filter out threatening nodes
)

# Create protected query engine
query_engine = index.as_query_engine(
    node_postprocessors=[postprocessor]
)

# Poisoned nodes are automatically filtered
response = query_engine.query("What is the company policy?")

Full Protected RAG Pipeline

from llama_index.core import SimpleDirectoryReader, VectorStoreIndex
from llama_index_trustguard import TrustGuardReader, TrustGuardNodePostprocessor

# 1. Protected document loading
reader = TrustGuardReader(
    SimpleDirectoryReader("./knowledge_base"),
    api_key="ta_xxx...",
    on_threat="filter",
)
documents = reader.load_data()

# 2. Build index
index = VectorStoreIndex.from_documents(documents)

# 3. Protected retrieval
postprocessor = TrustGuardNodePostprocessor(
    api_key="ta_xxx...",
    on_threat="filter",
)

query_engine = index.as_query_engine(
    node_postprocessors=[postprocessor]
)

# Your RAG pipeline is now protected at both indexing and retrieval
response = query_engine.query("Tell me about our products")

API Reference

TrustGuardReader

Wraps any LlamaIndex reader with threat scanning.

TrustGuardReader(
    reader: BaseReader,           # The reader to wrap
    api_key: str = None,          # TrustGuard API key
    on_threat: str = "warn",      # "block", "warn", "filter", "tag"
    min_block_level: ThreatLevel = ThreatLevel.HIGH,
    content_type: ContentSource = ContentSource.DOCUMENT,
)

on_threat options:

  • "block" - Raise ThreatInDocumentError on threat
  • "warn" - Log warning and continue
  • "filter" - Skip documents with threats
  • "tag" - Add threat info to document metadata

Methods:

documents = reader.load_data()      # Load and scan documents
stats = reader.get_stats()          # Get scanning statistics

TrustGuardNodePostprocessor

Postprocessor that scans retrieved nodes.

TrustGuardNodePostprocessor(
    api_key: str = None,          # TrustGuard API key
    on_threat: str = "warn",      # Same options as reader
    min_block_level: ThreatLevel = ThreatLevel.HIGH,
)

Usage:

query_engine = index.as_query_engine(
    node_postprocessors=[postprocessor]
)

# Or with a retriever
retriever = index.as_retriever(
    node_postprocessors=[postprocessor]
)

Examples

Web Content Scanning

from llama_index.readers.web import SimpleWebPageReader
from llama_index_trustguard import TrustGuardReader
from agent_trust import ContentSource

reader = TrustGuardReader(
    SimpleWebPageReader(),
    api_key="ta_xxx...",
    content_type=ContentSource.WEB,  # Optimized for web content
    on_threat="filter",
)

documents = reader.load_data(urls=["https://example.com/docs"])

Tagging Instead of Filtering

# Tag documents with threat info instead of filtering
reader = TrustGuardReader(
    base_reader,
    api_key="ta_xxx...",
    on_threat="tag",
)

documents = reader.load_data()

for doc in documents:
    if doc.metadata.get("trust_guard", {}).get("safe") == False:
        print(f"Document has threats: {doc.metadata['trust_guard']['threats']}")

Strict Mode (Block on Medium Threats)

from agent_trust import ThreatLevel

reader = TrustGuardReader(
    base_reader,
    api_key="ta_xxx...",
    on_threat="block",
    min_block_level=ThreatLevel.MEDIUM,  # Stricter blocking
)

Error Handling

from llama_index_trustguard import TrustGuardReader, ThreatInDocumentError

reader = TrustGuardReader(
    base_reader,
    api_key="ta_xxx...",
    on_threat="block",
)

try:
    documents = reader.load_data()
except ThreatInDocumentError as e:
    print(f"Threat in: {e.document_id}")
    print(f"Verdict: {e.guard_result.verdict}")
    print(f"Threats: {[t.pattern_name for t in e.guard_result.threats]}")

Statistics

# Reader stats
reader_stats = reader.get_stats()
print(f"Scanned: {reader_stats['scanned_count']}")
print(f"Threats: {reader_stats['threat_count']}")
print(f"Filtered: {reader_stats['filtered_count']}")

# Postprocessor stats
pp_stats = postprocessor.get_stats()
print(f"Nodes scanned: {pp_stats['scanned_count']}")

License

MIT License

Links

Metadata

Release files for llama-index-trustguard 0.1.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 llama-index-trustguard 0.1.0
File Size Uploaded Provenance Actions
llama_index_trustguard-0.1.0.tar.gz 6.8 kB Without provenance Details for llama_index_trustguard-0.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for llama-index-trustguard 0.1.0
File Interpreter ABI Platform Actions
llama_index_trustguard-0.1.0-py3-none-any.whl Python 3 none any Details for llama_index_trustguard-0.1.0-py3-none-any.whl

Total release size: 15.0 kB

Release files / llama_index_trustguard-0.1.0.tar.gz

Download URL llama_index_trustguard-0.1.0.tar.gz
Size 6.8 kB
Tags Source
SHA-256 checksum
How to use checksums
865da4cde139626faaa130063a0da2946d40b200bd170e2613ba994ac711629b
BLAKE2b-256 checksum
How to use checksums
1c997d20c27990fa9d85f56306c109a4503e73c587ab8707259e730682181c93
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.3

Release files / llama_index_trustguard-0.1.0-py3-none-any.whl

Download URL llama_index_trustguard-0.1.0-py3-none-any.whl
Size 8.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
532ee02cdc465279b45c983ee479a2952cb6ed453030411a71c52ed7648e9d1d
BLAKE2b-256 checksum
How to use checksums
67d3c6931bd74d5eb5c79c584c8ec840650cb5d2f01fcaa3c8ecbc0a7543a1fe
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.3

Security for llama-index-trustguard 0.1.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.

Release history Release notifications | RSS feed

This release

0.1.0 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