Integration details
This notebook illustrates how to integrate Permit.io permissions into LangChain retrievers. We provide two custom retrievers:- PermitSelfQueryRetriever – Uses a self-query approach to parse the user’s natural-language prompt, fetch the user’s permitted resource IDs from Permit, and apply that filter automatically in a vector store search.
- PermitEnsembleRetriever – Combines multiple underlying retrievers (e.g., BM25 + Vector) via LangChain’s EnsembleRetriever, then filters the merged results with Permit.io.
Setup
Install the package with the command:Installation
Environment Variables
- A running Permit PDP. See Permit docs for details on setting up your policy and container.
- A vector store or multiple retrievers that we can wrap.
Instantiation
PermitSelfQueryRetriever
Basic Explanation
- Retrieves permitted document IDs from Permit.
- Uses an LLM to parse your query and build a “structured filter,” ensuring only docs with those permitted IDs are considered.
Basic Usage
PermitEnsembleRetriever
Basic Explanation
- Uses LangChain’s EnsembleRetriever to gather documents from multiple sub-retrievers (e.g., vector-based, BM25, etc.).
- After retrieving documents, it calls filter_objects on Permit to eliminate any docs the user isn’t allowed to see.
Basic Usage
Demo Scripts
For more complete demos, check out the/langchain_permit/examples/demo_scripts folder:
- demo_self_query.py – Demonstrates PermitSelfQueryRetriever.
- demo_ensemble.py – Demonstrates PermitEnsembleRetriever.
Conclusion
With these custom retrievers, you can seamlessly integrate Permit.io’s permission checks into LangChain’s retrieval workflow. You can keep your application’s vector search logic while ensuring only authorized documents are returned. For more details on setting up Permit policies, see the official Permit docs. If you want to combine these with other tools (like JWT validation or a broader RAG pipeline), check out our docs/tools.ipynb in the examples folder.Usage
Use within a chain
Like other retrievers, PermitRetriever can be incorporated into LLM applications via chains. We will need a LLM or chat model:API reference
For detailed documentation of all PermitRetriever features and configurations head to the Repo.Connect these docs programmatically to Claude, VSCode, and more via MCP for real-time answers.