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
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)
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:Authorization flow
The retriever follows this flow:- Semantic Search: Base retriever performs semantic search and returns top K documents
- Extract Resource IDs: Extract resource IDs from document metadata
- Bulk Permission Check: Check all permissions in a single SpiceDB API call
- Filter: Return only documents the user is authorized to view
- Metrics: Track authorization rate, latency, and denied resources
Performance
The retriever uses SpiceDB’s nativeCheckBulkPermissionsRequest 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 documentsainvoke(query: str) -> List[Document]: Asynchronously retrieve authorized documentswith_config(subject_id: str, **kwargs) -> SpiceDBRetriever: Create new retriever with updated config
base_retriever: The wrapped retrieversubject_id: Current user IDspicedb_endpoint: SpiceDB server addressresource_type: Resource type for permissionspermission: Permission being checked
Related components
Connect these docs to Claude, VSCode, and more via MCP for real-time answers.

