📘 Introduction

Embeddings turn text into vectors, but those vectors need somewhere to live. If your RAG application has hundreds or thousands of document chunks, it needs a way to store embeddings and quickly find the chunks that are closest to a user question.

In this tutorial, you will learn how LangChain vector stores help store and search embeddings for RAG. We will use OpenAIEmbeddings and InMemoryVectorStore to create a small semantic search workflow, add documents with metadata, run similarity search, and connect the result to retrievers.

🎯
Vector stores are the searchable layer of many RAG systems. They help your application find relevant document chunks by meaning instead of exact keywords.

💡 What is a LangChain vector store?

A vector store stores embedded data and performs similarity search. In simple terms, it keeps document vectors and lets you ask, “Which stored texts are closest in meaning to this query?”

LangChain provides a common interface for different vector store backends. In this beginner tutorial, we use InMemoryVectorStore, which is useful for learning, local demos, and small examples because it stores everything in memory while the Python process is running.

ComponentRoleBeginner-friendly meaning
Embedding modelCreates vectors from textTurns meaning into numbers
Vector storeStores vectors and documentsKeeps searchable chunks
Similarity searchFinds close vectorsReturns texts related to a question
RetrieverWraps search for chainsFeeds relevant context into a RAG app

🧠 Why vector stores matter for RAG

RAG works by retrieving useful context before the model generates an answer. A vector store makes that retrieval possible by comparing the user question with stored document vectors and returning the most relevant chunks.

Documents -> Embeddings -> Vector store -> Similarity search -> Relevant documents -> RAG answer
💡
For production systems, you usually use a persistent vector database. For learning, an in-memory vector store is a clean way to understand the core idea first.

✅ Prerequisites

Before getting started, make sure you have:

☑️ Python installed
☑️ Basic Python knowledge
☑️ An OpenAI API key
☑️ Basic understanding of embeddings
☑️ A terminal or command prompt

⚙️1️⃣ Create a project folder

Create a new local project folder for this tutorial:

mkdir langchain-vector-stores-rag
cd langchain-vector-stores-rag

🐍2️⃣ Create a virtual environment

Create and activate a virtual environment:

python -m venv .venv
source .venv/bin/activate

On Windows, activate it with:

.venv\Scripts\activate

📦3️⃣ Install libraries

Install the packages we need for embeddings, documents, and the in-memory vector store:

pip install -U langchain-core langchain-openai

🔐4️⃣ Set your API key

Set your OpenAI API key as an environment variable. Replace the placeholder with your own key and never commit real secrets to a repository.

export OPENAI_API_KEY="your_api_key_here"

On Windows PowerShell, use:

$env:OPENAI_API_KEY="your_api_key_here"
🔐
Use placeholders in tutorials and environment variables in real projects. Do not write private API keys directly into Python files.

Create a file named vector_store_search.py. This first example creates documents, embeds them, stores them in an in-memory vector store, and searches for the most relevant matches.

from langchain_core.documents import Document
from langchain_core.vectorstores import InMemoryVectorStore
from langchain_openai import OpenAIEmbeddings

embeddings = OpenAIEmbeddings(model="text-embedding-3-small")

documents = [
    Document(
        page_content="Annual plans can be refunded within 14 days of purchase.",
        metadata={"source": "billing_policy.txt", "topic": "billing"},
    ),
    Document(
        page_content="Users can reset their password from the account settings page.",
        metadata={"source": "account_help.txt", "topic": "account"},
    ),
    Document(
        page_content="CSV exports are available for Pro and Enterprise customers.",
        metadata={"source": "features.txt", "topic": "exports"},
    ),
]

vector_store = InMemoryVectorStore(embedding=embeddings)
vector_store.add_documents(documents)

query = "Can I get my money back after buying a yearly plan?"
results = vector_store.similarity_search(query, k=2)

for index, document in enumerate(results, start=1):
    print(f"Result {index}")
    print(document.page_content)
    print(document.metadata)
    print("-" * 40)

vector_store_search.py

Run the script:

python vector_store_search.py

The refund document should appear near the top because it is semantically closest to the question, even though the wording is different.

Result 1
Annual plans can be refunded within 14 days of purchase.
{'source': 'billing_policy.txt', 'topic': 'billing'}
----------------------------------------
Result 2
CSV exports are available for Pro and Enterprise customers.
{'source': 'features.txt', 'topic': 'exports'}
----------------------------------------
🎓
Want the full RAG building-block workflow? In the Academy section, we inspect metadata, compare direct search with retrievers, and discuss when to move from in-memory storage to a persistent vector database.

You can view this post with the tier: Academy Membership

Join academy now to read the post and get access to the full library of premium posts for academy members only.

Join Academy Already have an account? Sign In