How to beginner · 3 min read

How to use Ollama embeddings in LangChain

Quick answer
Use the OllamaEmbeddings class from langchain_community.embeddings.ollama to generate embeddings with Ollama models in LangChain. Instantiate it and call embed_documents or embed_query to get vector embeddings for your text data.

PREREQUISITES

  • Python 3.8+
  • pip install langchain_community
  • Ollama installed and running locally or accessible
  • pip install langchain

Setup

Install the required packages and ensure Ollama is installed and running locally or accessible via its API. Ollama embeddings require the langchain_community package which contains the OllamaEmbeddings wrapper.

bash
pip install langchain langchain_community

Step by step

Use the OllamaEmbeddings class to generate embeddings for documents or queries. Below is a complete example that embeds a list of texts and prints the resulting vectors.

python
from langchain_community.embeddings.ollama import OllamaEmbeddings

# Initialize Ollama embeddings (default model can be specified)
embeddings = OllamaEmbeddings(model="llama2")

# Sample documents to embed
texts = [
    "LangChain makes working with LLMs easier.",
    "Ollama provides local LLM and embedding models.",
    "Embeddings are useful for semantic search."
]

# Generate embeddings for documents
vectors = embeddings.embed_documents(texts)

for i, vec in enumerate(vectors):
    print(f"Embedding for text {i+1}:", vec[:5], "... (truncated)")
output
Embedding for text 1: [0.0123, -0.0456, 0.0789, -0.0345, 0.0567] ... (truncated)
Embedding for text 2: [0.0234, -0.0678, 0.0890, -0.0123, 0.0456] ... (truncated)
Embedding for text 3: [0.0345, -0.0789, 0.0567, -0.0234, 0.0678] ... (truncated)

Common variations

You can specify different Ollama models by passing the model parameter to OllamaEmbeddings. For async usage, wrap calls in async functions and use await if supported. Ollama embeddings integrate seamlessly with LangChain vectorstores like FAISS or Chroma for semantic search.

python
from langchain_community.embeddings.ollama import OllamaEmbeddings

# Use a different model
embeddings = OllamaEmbeddings(model="llama2-13b")

# Example embedding a query
query_vector = embeddings.embed_query("What is LangChain?")
print(query_vector[:5], "... (truncated)")
output
[0.0156, -0.0345, 0.0678, -0.0234, 0.0456] ... (truncated)

Troubleshooting

  • If you get connection errors, ensure Ollama is running locally or your API endpoint is reachable.
  • Verify the model name is correct and supported by your Ollama installation.
  • Check your Python environment has langchain_community installed.
  • Embedding vectors are typically high-dimensional floats; if output is empty or errors occur, confirm your input texts are non-empty strings.

Key Takeaways

  • Use OllamaEmbeddings from langchain_community to generate embeddings in LangChain.
  • Specify the Ollama model via the model parameter for flexibility.
  • Integrate embeddings with LangChain vectorstores for semantic search workflows.
Verified 2026-04 · llama2, llama2-13b
Verify ↗