Local embeddings let a retrieval application turn text into vectors without sending source material to a hosted API. In LangChain, the Ollama embedding integration calls a local Ollama server and returns vector lists that can later feed retrievers and vector stores.
The Python wrapper lives in the separate langchain-ollama package. Ollama still needs an embedding-capable model loaded locally; chat-only models can answer prompts but may fail when the embed endpoint asks for numeric vectors.
The smoke test uses all-minilm because it is a small embedding model and prints the dimensions for one query plus a two-document batch. Matching dimensions confirm that LangChain reached Ollama and received vectors from the selected model before the same object is used in a retriever or vector store.
Steps to generate Ollama embeddings in LangChain:
- Open an activated Python project environment.
- Pull the small Ollama embedding model.
$ ollama pull all-minilm success
all-minilm is an embedding-only model. Use another pulled embedding model when retrieval quality, language coverage, or vector size requirements differ.
- Install the LangChain Ollama integration.
$ python3 -m pip install --upgrade langchain-ollama
langchain-ollama provides OllamaEmbeddings and uses the local Ollama API by default.
Related: How to install LangChain with pip - Create the embedding smoke-test script.
$ cat > langchain-ollama-embedding.py <<'PY' import os from langchain_ollama import OllamaEmbeddings model = os.environ.get("OLLAMA_EMBEDDING_MODEL", "all-minilm") embeddings = OllamaEmbeddings( model=model, base_url=os.environ.get("OLLAMA_BASE_URL", "http://localhost:11434"), ) texts = [ "Password reset tickets route to the identity queue.", "Invoice export tickets route to the billing queue.", ] query_vector = embeddings.embed_query("Where should password reset tickets go?") document_vectors = embeddings.embed_documents(texts) print(f"model: {model}") print(f"query dimensions: {len(query_vector)}") print(f"documents encoded: {len(document_vectors)}") print(f"first document dimensions: {len(document_vectors[0])}") print(f"first values: {[round(value, 4) for value in query_vector[:3]]}") PYembed_query() sends one search string. embed_documents() sends a list of document strings and should return one vector per document.
- Run the embedding smoke test.
$ python3 langchain-ollama-embedding.py model: all-minilm query dimensions: 384 documents encoded: 2 first document dimensions: 384 first values: [0.005, -0.0435, -0.0524]
The dimension count comes from the selected model. Rebuild stored vectors with the same embedding model and dimensions that the retriever will use.
- Remove the temporary script.
$ rm langchain-ollama-embedding.py
Mohd Shakir Zakaria is a cloud architect with deep roots in software development and open-source advocacy. Certified in AWS, Red Hat, VMware, ITIL, and Linux, he specializes in designing and managing robust cloud and on-premises infrastructures.