Registry / llm-agents / langchain-graph-retriever

langchain-graph-retriever

JSON →
library0.8.0pypypiunverified

langchain-graph-retriever is a specialized LangChain retriever for traversing document graphs built on top of vector-based similarity search. It enables RAG applications to perform contextual retrieval by following relationships between documents, moving beyond simple similarity. Currently at version 0.8.0, it maintains a frequent release cadence, typically with minor updates every few weeks.

pip install langchain-graph-retriever "astrapy[graphs]" langchain-community openai
INSTALL
IMPORT
SIG · LANGCHAIN-GRAPH-RE
L
langchain-graph-retriever
llm-agentspythonv0.8.0
Install
23.4s avg
Import
2423ms
Disk
333MB
Pass rate
8/ 10
Env Coverage8 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v0.8.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
glibc
py 3.10
✓ —
✓ 26.7s
py 3.11
✓ —
✓ 24.1s
py 3.12
✓ —
✓ 21.25s
py 3.13
✓ —
✓ 21.45s
py 3.9
✕ build_error
✕ build_error
333MB installed
● package 333MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

GraphRetriever
from langchain_graph_retriever import GraphRetriever
from langchain.graph_retriever import GraphRetriever
The package uses its own namespace `langchain_graph_retriever`, not `langchain.graph_retriever`.

This quickstart demonstrates how to initialize and use the `GraphRetriever` with an in-memory `Chroma` vector store and `OpenAIEmbeddings`. In a production environment, you would typically use a persistent vector store (like AstraDB as intended by the library) and populate it with documents and their defined graph relationships. Ensure you have installed `langchain-community` and `openai` (e.g., `pip install langchain-community openai`) in addition to `langchain-graph-retriever`.

import os from langchain_community.vectorstores import Chroma from langchain_community.embeddings import OpenAIEmbeddings from langchain_core.documents import Document from langchain_graph_retriever import GraphRetriever # Set your OpenAI API key. This example uses a mock key if not set in environment. # For actual use, ensure OPENAI_API_KEY is properly configured. os.environ["OPENAI_API_KEY"] = os.environ.get("OPENAI_API_KEY", "sk-YOUR_OPENAI_API_KEY") if not os.environ["OPENAI_API_KEY"].startswith("sk-") or os.environ["OPENAI_API_KEY"] == "sk-YOUR_OPENAI_API_KEY": print("OPENAI_API_KEY not set or is placeholder. Skipping quickstart execution.") print("Please set the OPENAI_API_KEY environment variable for a functional example.") else: try: # 1. Initialize Embeddings (requires 'openai' package) embeddings = OpenAIEmbeddings() # 2. Create a dummy vector store (in-memory Chroma for demonstration, requires 'langchain-community' package) # In a real application, this would be populated with documents and their graph relationships documents = [ Document(page_content="The quick brown fox jumps over the lazy dog.", metadata={"doc_id": "doc1"}), Document(page_content="The dog is lazy and enjoys napping.", metadata={"doc_id": "doc2"}), Document(page_content="A fox is a small omnivorous mammal.", metadata={"doc_id": "doc3"}), Document(page_content="Dogs are domesticated canids.", metadata={"doc_id": "doc4"}) ] vectorstore = Chroma.from_documents(documents, embeddings) # 3. Initialize the GraphRetriever # The GraphRetriever works by taking initial search results from the vector store # and then traversing the graph of related documents. The 'k' and 'depth' parameters # control the initial vector search and subsequent graph traversal depth. retriever = GraphRetriever( vectorstore=vectorstore, k=2, # Number of initial documents to retrieve from the vectorstore depth=1 # How many levels deep to traverse the graph from the initial documents # (requires graph relationships to be defined in your actual vector store/graph DB) ) # 4. Perform a retrieval query = "Tell me about animals." relevant_docs = retriever.get_relevant_documents(query) print(f"\nRetrieved {len(relevant_docs)} documents:") for i, doc in enumerate(relevant_docs): print(f"--- Document {i+1} ---") print(f"Content: {doc.page_content}") print(f"Metadata: {doc.metadata}") except Exception as e: print(f"An error occurred during quickstart execution: {e}")
Debug
Known issues
breakingMajor changes to strategy design and parameter names in v0.5.0. The internal architecture for traversal and node selection was refactored, leading to updated API signatures for strategy configuration.
fix
Refer to the v0.5.0 migration guide (if available, check project's GitHub) or updated documentation for current strategy configuration parameters. Old parameters like `strategy_type` may no longer be valid.
affects: >=0.5.0
gotchaThe `Id()` class for representing document IDs on edges was replaced by directly passing the `'$id'` string in v0.6.0.
fix
When defining graph edges or relationships, use the string `'$id'` instead of instantiating an `Id()` object for document identifiers. For example, `edge_data={'$id': 'doc_id_string'}`.
affects: >=0.6.0
gotchaThe `k` parameter (number of initial documents to retrieve) was affected by the v0.5.0 strategy changes and then restored in v0.5.1.
fix
If experiencing issues with the `k` parameter after upgrading to v0.5.0, upgrade to v0.5.1 or later where it was restored. For versions after 0.5.1, `k` should function as expected for controlling initial vector store retrieval.
affects: 0.5.0
Upgrade
Version history
0.8.0latest on PyPI · released Apr 4, 2025
Audit
Dependencies
langchain-corerequiredCore LangChain functionalities required by the retriever.
astrapyrequiredRequired for integration with AstraDB as the underlying graph and vector store.
langchain-communityoptionalCommonly used for various vector stores (e.g., Chroma) and embeddings providers within the LangChain ecosystem. Used in quickstart.
openaioptionalProvides OpenAI embeddings, a common choice for vectorizing documents. Used in quickstart.
Agent activity
31 hits · last 30 days
node
28
OpenAI (training)
1
Resources
langchain-graph-retriever — pip install langchain-graph-retriever · libregistry