Langtrace now supports Neo4j and Neo4j-GraphRAG

Obinna Okafor

Software Engineer

Apr 28, 2025

We're excited to announce that Langtrace now officially supports Neo4j and Neo4j-GraphRAG, bringing comprehensive observability to your graph-based AI applications.

Langtrace + Neo4j: Observability for Graph Database Operations

Neo4j has established itself as the leading graph database platform, allowing teams to model, store, and query complex relationships in their data. Its property graph model and Cypher query language make it ideal for applications where connections between entities are as important as the entities themselves — from fraud detection and recommendation engines to knowledge graphs and identity management.

With this new integration, Langtrace now traces every aspect of your graph database operations:

  • Query execution and performance: Track Cypher queries and their execution times.

  • Data flow visualization: See how data moves through your graph database operations.

  • Error detection: Quickly identify issues in database interactions.

  • Pattern analysis: Understand common query patterns and optimize accordingly.

import os
from langtrace_python_sdk import langtrace
from langtrace_python_sdk.utils.with_root_span import with_langtrace_root_span
from neo4j import GraphDatabase

# Initialize Langtrace
langtrace.init(api_key=os.getenv("LANGTRACE_API_KEY"))

# Connect to Neo4j
AUTH = (os.getenv("NEO4J_USERNAME"), os.getenv("NEO4J_PASSWORD"))
driver = GraphDatabase.driver(os.getenv("NEO4J_URI"), auth=AUTH)

# Trace your Neo4j operations
@with_langtrace_root_span("neo4j_person_query")
def find_person_by_age(age):
    records, summary, keys = driver.execute_query(
        "MATCH (p:Person {age: $age}) RETURN p.name AS name",
        age=age,
        database_="neo4j",
    )
    
    # Process results
    results = [person["name"] for person in records]
    
    # Execution metrics are automatically traced
    print(f"Query returned {len(records)} records in {summary.result_available_after} ms")
    
    return results


Extending Observability to Neo4j-GraphRAG

Neo4j-GraphRAG combines the power of graph databases with retrieval-augmented generation to create more contextually aware AI applications. By leveraging graph relationships during retrieval, Neo4j-GraphRAG provides LLMs with richer context that captures the connections between pieces of information.

Our integration with Neo4j-GraphRAG traces the entire RAG pipeline:

  • Knowledge graph construction: Monitor how documents are processed into knowledge graphs.

  • Vector indexing and retrieval: Track embedding creation and similarity search performance.

  • Graph traversal operations: Visualize how the system follows relationships during context retrieval.

  • LLM interactions: See the exact context provided to models and their responses.

Here's a simple example of building a traced Neo4j-GraphRAG application:

import os
import asyncio
from langtrace_python_sdk import langtrace
from langtrace_python_sdk.utils.with_root_span import with_langtrace_root_span
from neo4j import GraphDatabase
from neo4j_graphrag.generation import GraphRAG
from neo4j_graphrag.indexes import create_vector_index
from neo4j_graphrag.llm import OpenAILLM
from neo4j_graphrag.embeddings import OpenAIEmbeddings
from neo4j_graphrag.retrievers import VectorRetriever
from neo4j_graphrag.experimental.pipeline.kg_builder import SimpleKGPipeline

# Initialize Langtrace
langtrace.init(api_key=os.getenv("LANGTRACE_API_KEY"))

# Neo4j connection
AUTH = (os.getenv("NEO4J_USERNAME"), os.getenv("NEO4J_PASSWORD"))
neo4j_driver = GraphDatabase.driver(os.getenv("NEO4J_URI"), auth=AUTH)

embedder = OpenAIEmbeddings()
llm = OpenAILLM(model_name="gpt-4o")

@with_langtrace_root_span("contract_analysis")
async def analyze_contract():
    # 1. Build knowledge graph from PDF
    kg_builder = SimpleKGPipeline(
        llm=OpenAILLM(
            model_name="gpt-4o-mini",
            model_params={"response_format": {"type": "json_object"}, "temperature": 0}
        ),
        driver=neo4j_driver,
        embedder=embedder,
        from_pdf=True
    )
    
    # Trace KG construction
    await kg_builder.run_async(file_path='data/contract.pdf')
    
    # 2. Create vector index
    create_vector_index(
        neo4j_driver, 
        name="text_embeddings", 
        label="Chunk",
        embedding_property="embedding", 
        dimensions=1536, 
        similarity_fn="cosine"
    )

    # 3. Set up retriever and GraphRAG
    vector_retriever = VectorRetriever(
        neo4j_driver,
        index_name="text_embeddings",
        embedder=embedder
    )
    
    rag = GraphRAG(llm=llm, retriever=vector_retriever)

    # 4. Run query with tracing
    response = rag.search("Summarize the termination clause from the contract.")
    return response.answer

# Run the traced analysis
result = asyncio.run(analyze_contract())


The Value of End-to-End Tracing

With Langtrace's Neo4j and Neo4j-GraphRAG integrations, teams now have complete observability across their entire graph-based RAG systems with benefits such as:

During Development:
  • Faster debugging: Pinpoint exactly where issues occur in your graph operations or RAG pipeline

  • Performance optimization: Identify bottlenecks in graph queries or retrieval processes

  • Content verification: Ensure the right nodes and relationships are being accessed for context

In Production:
  • System monitoring: Track response times, success rates, and resource utilization

  • Quality assurance: Monitor the relevance of retrieved context and quality of generated responses

  • Anomaly detection: Get alerts when graph operations or RAG processes deviate from normal patterns

Getting Started

To start using these integrations:

  1. Install the required packages:

    pip install -U langtrace-python-sdk neo4j neo4j-graphrag


  2. Initialize Langtrace in your application:

    from langtrace_python_sdk import langtrace
    langtrace.init(api_key="your-api-key")


  3. Wrap your key functions with with_langtrace_root_span decorators to create meaningful trace contexts.

For detailed documentation, visit our Neo4j integration guide and Neo4j-GraphRAG integration guide.

Conclusion

Neo4j GraphRAG represents a significant advancement in RAG technology, leveraging the power of knowledge graphs to enhance context retrieval and reasoning capabilities. When combined with Langtrace's observability features, developers can gain deep insights into their RAG pipelines, helping them build more accurate, explainable, and performant AI applications.

While traditional vector-based RAG systems like LangChain and LlamaIndex excel in many scenarios, knowledge graph-enhanced approaches provide unique advantages for complex domains with rich entity relationships.

Regardless of which RAG approach you choose, adding observability using Langtrace will help you understand and optimize your AI applications better, transforming development from guesswork to data-driven decision making.


Ready to try Langtrace?

Try out the Langtrace SDK with just 2 lines of code.

Ready to deploy?

Try out the Langtrace SDK with just 2 lines of code.

Want to learn more?

Check out our documentation to learn more about how langtrace works

Join the Community

Check out our Discord community to ask questions and meet customers