Registry /
llm-agents / langchain-graph-retriever
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
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}")
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.