LangChain Neo4j Vector Store Connection Issues in Dockerized Environment

I’m having trouble connecting LangChain to my Neo4j database running in Docker. The connection works fine with regular Neo4j driver but fails when using Neo4jVector.

Background

My setup was working perfectly when I used Neo4j cloud instance. But after switching to a local Docker setup, I keep getting connection refused errors specifically with LangChain’s vector store integration.

What Works

Direct Neo4j connection using the standard driver works without issues:

from neo4j import GraphDatabase

db_uri = "bolt://neo4j_database:7687"
user = "neo4j"
pass_word = "neo4j_admin"

connection = GraphDatabase.driver(db_uri, auth=(user, pass_word))

def insert_historical_data(transaction):
    # Adding some historical figures
    transaction.run("""
        CREATE (gandhi:Leader {name: 'Mahatma Gandhi', role: 'Freedom Fighter', period: '1869-1948'})
        CREATE (independence:Movement {name: 'Independence Movement', description: 'Indian freedom struggle', timeline: '1857-1947'})
        CREATE (nehru:Leader {name: 'Jawaharlal Nehru', role: 'First Prime Minister', term: '1947-1964'})
        CREATE (partition:Event {name: 'Partition of India', description: 'Division of British India', year: 1947})
        CREATE (quit_india:Campaign {name: 'Quit India Movement', description: 'Mass protest against British rule', year: 1942})
    """)
    
    # Creating connections between entities
    transaction.run("""
        MATCH (gandhi:Leader {name: 'Mahatma Gandhi'}),
              (nehru:Leader {name: 'Jawaharlal Nehru'}),
              (independence:Movement {name: 'Independence Movement'}),
              (partition:Event {name: 'Partition of India'}),
              (quit_india:Campaign {name: 'Quit India Movement'})
        CREATE (gandhi)-[:LED]->(independence)
        CREATE (gandhi)-[:LAUNCHED]->(quit_india)
        CREATE (nehru)-[:PARTICIPATED_IN]->(independence)
        CREATE (independence)-[:RESULTED_IN]->(partition)
        CREATE (nehru)-[:BECAME_PM_AFTER]->(partition)
    """)

try:
    with connection.session() as db_session:
        db_session.execute_write(insert_historical_data)
        print("Historical data inserted successfully!")
        print(f"Connected to: {db_uri}")
except Exception as error:
    print(f"Database error: {error}")
finally:
    connection.close()

What Doesn’t Work

LangChain integration fails with connection refused error:

from langchain_ollama import OllamaEmbeddings
from langchain_community.vectorstores.neo4j_vector import Neo4jVector
import os
from dotenv import load_dotenv
from langchain.schema import Document

load_dotenv()

DATABASE_URI = os.getenv("NEO4J_URL")
db_user = os.getenv("NEO4J_USERNAME")
db_password = os.getenv("NEO4J_PASSWORD")
documents_path = os.getenv("CHUNK_FOLDER_PATH")
vector_index = "embeddings"
text_index = "fulltext"

def load_text_files(folder_path):
    documents = []
    try:
        for file in os.listdir(folder_path):
            if file.endswith(".txt"):
                full_path = os.path.join(folder_path, file)
                with open(full_path, 'r', encoding='utf-8') as f:
                    text_content = f.read()
                    documents.append(Document(page_content=text_content, metadata={"source": file}))
    except Exception as err:
        print(f"Failed to load documents: {err}")
    return documents

def setup_vector_store(docs):
    try:
        embedder = OllamaEmbeddings(model="mxbai-embed-large")
        print("Embedding model ready.")
    except Exception as err:
        print(f"Embedding model error: {err}")
        return None
    
    try:
        vector_db = Neo4jVector.from_existing_index(
            embedding=embedder,
            url=DATABASE_URI,
            username=db_user,
            password=db_password,
            search_type="hybrid",
            index_name=vector_index,
            keyword_index_name=text_index,
            node_label=["Document", "Content"],
            embedding_node_property="vector",
        )
        print("Connected to existing vector index.")
        return vector_db
    except Exception as err:
        print(f"Existing index failed, creating new: {err}")

    docs = load_text_files(documents_path)
    if not docs:
        print("No documents available for indexing")
        return None
        
    try:
        vector_db = Neo4jVector.from_documents(
            embedding=embedder,
            documents=docs,
            url=DATABASE_URI,
            username=db_user,
            password=db_password,
            search_type="hybrid",
            index_name=vector_index,
            keyword_index_name=text_index,
            node_label=["Document", "Content"],
            embedding_node_property="vector",
        )
        print("New vector index created")
        return vector_db
    except Exception as creation_err:
        print(f"Vector index creation failed: {creation_err}")
        return None

def search_similar_content(vector_store, search_query):
    try:
        results = vector_store.similarity_search_with_score(search_query, k=3)
        for content, relevance in results:
            print(f"Content: {content.page_content}\nRelevance: {relevance}")
    except Exception as err:
        print(f"Search failed: {err}")

if __name__ == "__main__":
    search_term = "Who was the first king"
    documents = load_text_files(documents_path)
    if documents:
        store = setup_vector_store(documents)
        if store:
            search_similar_content(store, search_term)

Error Message

Embedding model ready.
Vector index creation failed: [Errno 111] Connection refused

Environment Setup

  • langchain == 0.3.13
  • neo4j == 5.27.0
  • langchain_ollama == 0.2.2

Environment variables:

NEO4J_URI=bolt://172.20.0.3:7687
NEO4J_USERNAME=neo4j
NEO4J_PASSWORD=neo4j_admin

Neo4j browser works at http://localhost:7474 and bolt connection at bolt://172.20.0.3:7687.

Why does direct Neo4j driver connection work but LangChain Neo4jVector fails? How can I fix this connection issue in my Docker environment?

I’ve hit this exact same headache. The problem isn’t just the env variable mismatch alexj spotted.

Neo4jVector uses different connection pooling internally - it creates more persistent connections and validates more strictly than the basic GraphDatabase driver.

Quick fix first - use the same URI format for both:

DATABASE_URI = "bolt://neo4j_database:7687"  # Same as your working example

If that doesn’t work, Neo4jVector might be hitting Docker’s networking differently. I’ve seen simple queries work fine while the complex connection pooling that vector stores need fails completely.

Add these to your Neo4jVector calls:

vector_db = Neo4jVector.from_documents(
    # ... your existing params
    database="neo4j",  # explicit database name
    # Add connection args if the library supports it
)

Also check if your Neo4j container has the APOC plugin. Neo4jVector sometimes needs it for vector operations even when it doesn’t complain about it being missing.

That IP address approach (172.20.0.3) in your env is brittle in Docker. Stick with service names.

Had this exact issue moving from Neo4j AuraDB to local Docker. Neo4jVector is way pickier about connections than the standard driver - it’s stricter with timeouts and pooling settings. Add explicit connection config to your Neo4jVector calls. I had to set connection timeouts and pool settings that the basic driver didn’t need. Check your Docker ports too - bolt protocol sometimes needs extra config in containers that HTTP doesn’t. Also, use the same URI format everywhere. Since your direct connection works with the service name, hardcode that same URI in your LangChain code first. That’ll tell you if it’s an environment variable problem or if Neo4jVector itself can’t connect.

Docker network isolation is probably what’s breaking this. Neo4jVector opens multiple connections at once for vector stuff, and Docker’s default networking blocks these even when single connections work fine.

Add explicit network config to your Neo4j container. Vector operations need bidirectional communication that basic queries don’t. I’ve fixed this before by making sure Neo4j exposes all ports and using --network host for testing.

Check your Neo4j logs while LangChain tries to connect. Vector store setup starts background processes that might fail silently. Run docker logs your_neo4j_container during your LangChain code - you’ll likely see failed connection attempts not showing in Python errors.

Also verify your Neo4j Docker image supports vectors. Some minimal images don’t have the extensions Neo4jVector needs, causing weird connection failures.

You’re using different URI variables in your examples. The first one has db_uri = "bolt://neo4j_database:7687" but your langchain code pulls DATABASE_URI = os.getenv("NEO4J_URL") while your env file shows NEO4J_URI. That’s your problem right there - fix the env variable name or stick with the same docker service name everywhere.

Everyone’s right about the connection issues, but you’re making this way harder with Docker networking headaches. Had the same mess last month - kept fighting Docker container communication for vector operations.

Skip debugging Docker networks and Neo4j connection pooling. Just automate the whole pipeline. Built a workflow that handles Neo4j vector operations without any connection reliability issues.

It connects to your Neo4j instance, processes documents, creates embeddings, and handles vector store operations. No more wondering if Neo4jVector works with Docker or if your connection pooling’s right.

Mine reads documents from a folder, chunks them, generates embeddings, and stores everything in Neo4j with proper indexing. Handles connection retries, batch processing, and monitors everything.

Best part - it runs separately from your main app. Your search queries just hit the populated vector store without the setup complexity.

Took me 20 minutes to build versus hours debugging Docker networking. Way more reliable than fighting LangChain’s connection quirks.