Skip to main content

🦜🔗 Graphiti for LangChain

PyPI version Test and Lint License: MIT

langchain-graphiti is a production-grade, high-quality LangChain integration for the Graphiti knowledge graph system. It provides a robust set of tools and retrievers that empower LangChain agents with long-term, evolving memory and sophisticated graph-based reasoning capabilities.

This library allows your agents to dynamically build and query a knowledge graph, transforming unstructured information into structured, queryable memory that persists across sessions.


Core Concepts

At its core, this integration bridges the gap between LangChain's powerful agentic frameworks and Graphiti's ability to create real-time knowledge graphs.

  • Graphiti: An open-source framework that ingests unstructured data (like conversations or documents) and uses LLMs to automatically extract entities and relationships, building a rich knowledge graph.
  • LangChain Integration: This library exposes Graphiti's functionality through standard LangChain interfaces (BaseTool, BaseRetriever), allowing agents to seamlessly read from, write to, and manage the knowledge graph as part of their workflow.

The result is an AI agent that doesn't just operate on short-term context but learns, remembers, and reasons over a persistent, interconnected web of information.

Features

  • Comprehensive Toolset: A full suite of tools for adding, searching, and managing information in the knowledge graph.
  • Advanced Retrievers: Powerful retrievers that leverage Graphiti's hybrid search (text, vector, and graph traversal) for highly relevant and context-aware information retrieval.
  • Robust Client: A thread-safe, async-first client with built-in health checks, connection management, and error handling.
  • Production-Ready: Designed with a focus on quality, robustness, and meticulous engineering for reliable deployment.
  • Seamless Integration: Built to feel like a native part of the LangChain ecosystem.

Installation

pip install langchain-graphiti

You will also need to install extras depending on the LLM and database providers you intend to use:

# Example for OpenAI and Neo4j
pip install "langchain-graphiti[openai,neo4j]"

See pyproject.toml for a full list of available extras.

Quick Start

Here's a simple example of how to get up and running with langchain-graphiti. This example uses langchain-openai and a local Neo4j database.

import asyncio
import os
# Note: This must be set due to Graphiti's limitations. Will be fixed when Graphiti fixes it.
# Of course, you can set it in ".env" as well, but you must load it before importing langchain_graphiti.
os.environ["DEFAULT_DATABASE"] = "default_db" 

from langchain_openai import ChatOpenAI
from langgraph.prebuilt import create_react_agent

from langchain_graphiti import GraphitiClient
from langchain_graphiti.tools import create_basic_agent_tools
from langchain_graphiti.config import LLMProvider, DriverProvider, OpenAIConfig, Neo4jConfig

# 1. Configure your providers
llm_config = OpenAIConfig(
    api_key=os.getenv("OPENAI_API_KEY"),
    model="gpt-4o",  # Use the model you prefer
    small_model="gpt-3.5-turbo",  # Optional: for smaller tasks
    embedding_model="text-embedding-3-small",
    embedding_dim=1536,  # Optional: dimension of the embeddings
)
driver_config = Neo4jConfig(
    uri=os.getenv("NEO4J_URI", "bolt://localhost:7687"),
    user=os.getenv("NEO4J_USER", "neo4j"),
    password=os.getenv("NEO4J_PASSWORD", "password")
)

# 2. Create the GraphitiClient
# This client manages the connection to your knowledge graph
client = GraphitiClient.from_factory(
    llm_provider=LLMProvider.OPENAI,
    driver_provider=DriverProvider.NEO4J,
    llm_config=llm_config,
    driver_config=driver_config,
)

# 3. Create the agent tools
# These are the functions the agent can call to interact with the graph
tools = create_basic_agent_tools(client)

# 4. Set up your LangChain agent
llm = ChatOpenAI(model="gpt-4o")

# I recommend using more advanced agents than `ReAct` for production use, but this is a simple example.
# This is because agents may (rarely) make mistakes during tool calls, but they will get it right in
# usually the second try.
agent_executor = create_react_agent(llm, tools)

async def main():
    # Let's teach the agent something new
    await agent_executor.ainvoke({
        "messages": [("user", "Add this fact: 'Project Phoenix is managed by Alice.'")]
    })

    # Now, let's ask a question that requires recalling that fact
    response = await agent_executor.ainvoke({
        "messages": [("user", "Who manages Project Phoenix?")]
    })

    print(response["messages"][-1].content)
    # Expected output will mention that Alice manages Project Phoenix.

    # Clean up the client connection
    await client.close()

if __name__ == "__main__":
    asyncio.run(main())

Advanced Usage

Using the GraphitiRetriever

The GraphitiRetriever is a powerful tool for fetching documents from your knowledge graph. It can be used in any standard LangChain RAG (Retrieval-Augmented Generation) chain.

from langchain_core.runnables import RunnablePassthrough
from langchain_core.output_parsers import StrOutputParser
from langchain_core.prompts import ChatPromptTemplate
from langchain_graphiti import GraphitiRetriever

# Assume 'client' is an initialized GraphitiClient
retriever = GraphitiRetriever(client=client, k=5)

template = """
Answer the question based only on the following context:
{context}

Question: {question}
"""
prompt = ChatPromptTemplate.from_template(template)

rag_chain = (
    {"context": retriever, "question": RunnablePassthrough()}
    | prompt
    | llm
    | StrOutputParser()
)

# This will search the graph for context related to the question
result = rag_chain.invoke("What were the key outcomes of the Q3 budget meeting?")
print(result)

Using the Full Toolset

For agents that require more advanced control over the knowledge graph, you can use the create_agent_tools factory to get the complete set of tools.

from langchain_graphiti.tools import create_agent_tools

# Get all available tools
advanced_tools = create_agent_tools(client)

# Create an agent with full capabilities
advanced_agent = create_react_agent(llm, advanced_tools)

# This agent can now perform advanced operations like:
# - Building communities
# - Removing specific episodes
# - Adding structured triplets directly
# - Optimizing the database with indices

Contributing

We welcome contributions to langchain-graphiti! Whether it's reporting a bug, suggesting a new feature, or submitting a pull request, your help is valued.

Please see the CONTRIBUTING.md file for detailed guidelines on how to contribute to the project.

Release files for langchain-graphiti 0.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for langchain-graphiti 0.1.1
File Size Uploaded
langchain_graphiti-0.1.1.tar.gz 28.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for langchain-graphiti 0.1.1
File Interpreter ABI Platform
langchain_graphiti-0.1.1-py3-none-any.whl Python 3 none any Details

Total release size: 58.6 kB

Release files / langchain_graphiti-0.1.1.tar.gz

Download URL langchain_graphiti-0.1.1.tar.gz
Size 28.7 kB
Tags Source
SHA-256 checksum
How to use checksums
5cf549bd9c18b15ae93e448a6b212ef9802be97f5f501a0945109e4f06e9ff76
BLAKE2b-256 checksum
How to use checksums
2a9ad5c257b33078b3f7eac5c534e27501d57b4a31751e752a153bd970989da5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.12.9

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jul 11, 2025.

Transparency log

Release files / langchain_graphiti-0.1.1-py3-none-any.whl

Download URL langchain_graphiti-0.1.1-py3-none-any.whl
Size 29.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
81c559780a0432ddad69bc30119c5f50e45651e130bf9bb7a3966c87ed6b2129
BLAKE2b-256 checksum
How to use checksums
113cf3ef6ed36ec352f05aaa3027596d9e9ddee4be2ebf934f2455d2e31ff9e9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.12.9

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jul 11, 2025.

Transparency log

Release history Release notifications | RSS feed

This release

0.1.1 This release

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page