{"id":7352,"library":"langchain-neo4j","title":"LangChain Neo4j Integration","description":"LangChain Neo4j is an integration package connecting the LangChain framework with the Neo4j graph database. It provides functionalities for building LLM applications, including graph data interaction via `Neo4jGraph`, semantic search with `Neo4jVector`, conversational memory using `Neo4jChatMessageHistory`, and LangGraph checkpoint savers for state persistence. The current version is 0.9.0, with regular updates providing new features and compatibility improvements.","status":"active","version":"0.9.0","language":"en","source_language":"en","source_url":"https://github.com/langchain-ai/langchain-neo4j","tags":["langchain","neo4j","graph database","vector store","LLM","RAG","LangGraph"],"install":[{"cmd":"pip install -U langchain-neo4j","lang":"bash","label":"Install core package"},{"cmd":"pip install -U 'langchain-neo4j[mmr]' # For Maximal Marginal Relevance search","lang":"bash","label":"Install with MMR dependencies"}],"dependencies":[{"reason":"Required for compatibility with LangChain 1.0.0+ as legacy functionality moved from `langchain` to `langchain-classic`.","package":"langchain-classic","optional":false},{"reason":"Added as a dependency in v0.4.0 for enhanced RAG capabilities.","package":"neo4j-graphrag","optional":true},{"reason":"Commonly used with `Neo4jVector` and `GraphCypherQAChain` for embeddings and LLM functionality.","package":"openai","optional":true},{"reason":"The underlying Python driver for Neo4j database interaction.","package":"neo4j","optional":false}],"imports":[{"symbol":"Neo4jGraph","correct":"from langchain_neo4j import Neo4jGraph"},{"symbol":"Neo4jVector","correct":"from langchain_neo4j import Neo4jVector"},{"symbol":"Neo4jChatMessageHistory","correct":"from langchain_neo4j import Neo4jChatMessageHistory"},{"note":"While `GraphCypherQAChain` might have existed directly in `langchain.chains` in older LangChain versions, for `langchain-neo4j` it's correctly imported from `langchain_neo4j`.","wrong":"from langchain.chains import GraphCypherQAChain","symbol":"GraphCypherQAChain","correct":"from langchain_neo4j import GraphCypherQAChain"},{"symbol":"Neo4jSaver","correct":"from langchain_neo4j import Neo4jSaver"},{"symbol":"AsyncNeo4jSaver","correct":"from langchain_neo4j import AsyncNeo4jSaver"}],"quickstart":{"code":"import os\nfrom langchain_neo4j import Neo4jGraph, GraphCypherQAChain\nfrom langchain_openai import ChatOpenAI\n\n# Set environment variables for Neo4j connection\nNEO4J_URI = os.environ.get('NEO4J_URI', 'bolt://localhost:7687')\nNEO4J_USERNAME = os.environ.get('NEO4J_USERNAME', 'neo4j')\nNEO4J_PASSWORD = os.environ.get('NEO4J_PASSWORD', 'password')\nOPENAI_API_KEY = os.environ.get('OPENAI_API_KEY', '')\n\nif not OPENAI_API_KEY:\n    raise ValueError(\"OPENAI_API_KEY environment variable not set.\")\n\n# Initialize Neo4jGraph connection\ngraph = Neo4jGraph(url=NEO4J_URI, username=NEO4J_USERNAME, password=NEO4J_PASSWORD)\n\n# Optional: Insert some sample movie data if the database is empty\ngraph.query(\n    \"\"\"\n    MERGE (m:Movie {title:'The Matrix'}) WITH m\n    UNWIND ['Keanu Reeves', 'Laurence Fishburne', 'Carrie-Anne Moss'] AS actor\n    MERGE (a:Actor {name:actor})\n    MERGE (a)-[:ACTED_IN]->(m)\n    \"\"\"\n)\n\n# Initialize LLM\nllm = ChatOpenAI(temperature=0, api_key=OPENAI_API_KEY)\n\n# Create GraphCypherQAChain\n# IMPORTANT: allow_dangerous_requests=True is used for demonstration. \n# In production, ensure narrowly-scoped credentials and careful prompt engineering.\nchain = GraphCypherQAChain.from_llm(\n    llm=llm,\n    graph=graph,\n    verbose=True,\n    allow_dangerous_requests=True\n)\n\n# Invoke the chain with a natural language query\nresult = chain.invoke({\"query\": \"Who acted in The Matrix?\"})\nprint(result['result'])","lang":"python","description":"This quickstart demonstrates how to connect to a Neo4j database using `Neo4jGraph`, optionally load sample data, and then use `GraphCypherQAChain` with an OpenAI LLM to answer natural language questions by generating and executing Cypher queries against the graph. Ensure `NEO4J_URI`, `NEO4J_USERNAME`, `NEO4J_PASSWORD`, and `OPENAI_API_KEY` environment variables are set."},"warnings":[{"fix":"Ensure your environment uses Python >=3.10 and `pip install -U langchain-classic` alongside `langchain-neo4j` if you're migrating from older LangChain versions.","message":"LangChain v1.0.0+ requires `langchain-classic` and drops Python 3.9 support. `langchain-neo4j` v0.6.0+ is updated to reflect this change, replacing `langchain` with `langchain-classic` as a dependency and removing Python 3.9 support.","severity":"breaking","affected_versions":">=0.6.0"},{"fix":"Upgrade to `langchain-neo4j` v0.9.0 or later to ensure broader compatibility, especially if using custom `GraphStore` implementations.","message":"The `GraphCypherQAChain` was fixed in v0.9.0 to improve compatibility with non-Neo4j `GraphStore` implementations by no longer requiring the private `_enhanced_schema` attribute.","severity":"breaking","affected_versions":"==0.9.0"},{"fix":"Always use narrowly-scoped database credentials with minimal permissions. Carefully evaluate the necessity of `allow_dangerous_requests=True` in production and prefer `False` where possible, or implement strong prompt engineering and input sanitization.","message":"The `GraphCypherQAChain` constructor includes an `allow_dangerous_requests` parameter. Setting this to `True` can expose your database to potential data corruption or loss if the LLM generates malicious queries.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Update your LangChain core dependencies (`pip install -U langchain-community langchain-openai`) and adjust imports to their new homes (e.g., `from langchain_openai import ChatOpenAI`).","message":"When migrating to LangChain v0.1.0 and beyond, many core LangChain classes (e.g., `ChatOpenAI`, `OpenAIEmbeddings`, `initialize_agent`) moved to `langchain-community` or `langchain-openai` packages. Older `langchain.*` imports will trigger deprecation warnings.","severity":"deprecated","affected_versions":"All versions when used with older LangChain core"},{"fix":"Ensure you are using `langchain-neo4j` v0.5.0 or newer. Verify connectivity to the correct database using standard Neo4j Python driver methods if issues persist.","message":"The `Neo4jGraph` class in older versions (prior to fix) might default to the 'neo4j' database even when a different database name is specified, leading to connection or data access issues.","severity":"gotcha","affected_versions":"<0.5.0"}],"env_vars":null,"last_verified":"2026-04-16T00:00:00.000Z","next_check":"2026-07-15T00:00:00.000Z","problems":[{"fix":"Double-check `NEO4J_USERNAME`, `NEO4J_PASSWORD`, or `NEO4J_TOKEN` values. For Neo4j Desktop, ensure the database-specific password is correct and reset it if necessary from the project details.","cause":"Incorrect Neo4j username, password, or token, or the Neo4j database requires a password reset (e.g., in Neo4j Desktop projects).","error":"neo4j.exceptions.AuthError: {code: Neo.ClientError.Security.Unauthorized} {message: The client is unauthorized due to authentication failure.}"},{"fix":"Verify Neo4j instance is running and accessible at the specified URI. Check firewall rules to ensure port 7687 is open. Test network connectivity (e.g., `ping` or `telnet` to the Neo4j host/port). Use `NEO4J_URI` with the correct protocol (e.g., `bolt://` or `neo4j://`).","cause":"Neo4j database not running, incorrect URI, firewall blocking port 7687, or network connectivity issues (e.g., VPN blocking, DNS resolution failure).","error":"ServiceUnavailable: Failed to establish connection to 'bolt://localhost:7687' (reason: None)"},{"fix":"Update your Neo4j database instance to a recent version (Neo4j 4.x or 5.x are generally compatible with `langchain-neo4j`). If using Docker, try a newer image.","cause":"An older Neo4j database version might have an incompatible Cypher syntax for schema introspection queries used by `Neo4jGraph`.","error":"CypherSyntaxError: Invalid input 'schema': expected 'MATCH', 'CREATE', 'RETURN'..."},{"fix":"Update your import statements to use the new package structure, e.g., `from langchain_openai import ChatOpenAI` instead of `from langchain.chat_models import ChatOpenAI`. Install `langchain-openai` if not already installed.","cause":"Using an older import path for LLM/embedding classes after upgrading LangChain to v0.1.0+.","error":"LangChainDeprecationWarning: The class `ChatOpenAI` was deprecated in LangChain 0.1.0 and will be removed in 0.2.0. Use langchain_openai.ChatOpenAI instead."}]}