Skip to main content
The SpiceDBRetriever is a LangChain BaseRetriever that wraps any existing retriever with SpiceDB authorization. It follows the post-filter authorization pattern: retrieve documents based on semantic search first, then filter by user permissions.

Installation

Setup

This retriever requires a running SpiceDB instance. See the SpiceDB provider page for setup instructions.

Environment setup

Initialization

Parameters

  • base_retriever (BaseRetriever): The underlying retriever to wrap with authorization (required)
  • subject_id (str): User ID to check permissions for (required)
  • spicedb_endpoint (str): SpiceDB server address (default: “localhost:50051”)
  • spicedb_token (str): Pre-shared key for SpiceDB authentication (default: “sometoken”)
  • resource_type (str): SpiceDB resource type, e.g., “document”, “article” (default: “document”)
  • subject_type (str): SpiceDB subject type, e.g., “user” (default: “user”)
  • permission (str): Permission to check, e.g., “view”, “edit” (default: “view”)
  • resource_id_key (str): Key in document metadata containing resource ID (default: “resource_id”)
  • fail_open (bool): If True, allow access on errors; if False, deny on errors (default: False)
  • use_tls (bool): Whether to use TLS for SpiceDB connection (default: False)
All parameters are required for SpiceDB to make access decisions. While some have defaults, you should explicitly set them to match your SpiceDB schema.

Usage

Basic RAG pipeline

Vector store compatibility

The retriever works with any LangChain-compatible vector store:

FAISS

Chroma

Pinecone

Weaviate

Document metadata requirements

Documents must include the resource ID in their metadata:
If a document is missing the resource ID in metadata, it will be filtered out (treated as unauthorized).

Authorization flow

The retriever follows this flow:
  1. Semantic Search: Base retriever performs semantic search and returns top K documents
  2. Extract Resource IDs: Extract resource IDs from document metadata
  3. Bulk Permission Check: Check all permissions in a single SpiceDB API call
  4. Filter: Return only documents the user is authorized to view
  5. Metrics: Track authorization rate, latency, and denied resources

Performance

The retriever uses SpiceDB’s native CheckBulkPermissionsRequest API for optimal performance:
  • Single API Call: All permissions checked in one request, not N separate calls
  • Efficient: Significantly faster than individual permission checks
  • Scalable: Handles hundreds of documents efficiently

Example performance

Error handling

Fail closed (default)

By default, the retriever fails closed - if there’s an error checking permissions, documents are filtered out:

Fail open

For development or specific use cases:

Complete example: Multi-user RAG

API reference

SpiceDBRetriever

Inherits from: BaseRetriever Methods:
  • invoke(query: str) -> List[Document]: Synchronously retrieve authorized documents
  • ainvoke(query: str) -> List[Document]: Asynchronously retrieve authorized documents
  • with_config(subject_id: str, **kwargs) -> SpiceDBRetriever: Create new retriever with updated config
Properties:
  • base_retriever: The wrapped retriever
  • subject_id: Current user ID
  • spicedb_endpoint: SpiceDB server address
  • resource_type: Resource type for permissions
  • permission: Permission being checked