LlamaIndex Embeddings Langchain Integration
The `llama-index-embeddings-langchain` library provides an integration layer to use LangChain's embedding models within the LlamaIndex framework. It acts as a wrapper, allowing users to leverage the wide array of embedding models available in LangChain for LlamaIndex's indexing and retrieval functionalities. The current version is 0.5.0, and as part of the broader LlamaIndex ecosystem, it typically sees active development and frequent releases in conjunction with LlamaIndex core.
Common errors
-
ImportError: cannot import name 'LangchainEmbedding' from 'llama_index'
cause The `LangchainEmbedding` class is not directly available at the top-level `llama_index` package. It resides within the `llama_index.embeddings.langchain` submodule.fixChange your import statement to `from llama_index.embeddings.langchain import LangchainEmbedding`. -
ModuleNotFoundError: No module named 'langchain.embeddings.base'
cause This error typically means the `langchain` package (or `langchain-community` for newer versions) is not installed or the installed version is incompatible with LlamaIndex's bridge.fixInstall or update LangChain: `pip install langchain` or `pip install langchain-community`. If the issue persists, try pinning an older, known-compatible version of `langchain` (e.g., `langchain==0.0.153` as seen in older issues) and then gradually upgrade. -
AttributeError: 'HuggingFaceEmbeddings' object has no attribute 'aembed_query'
cause Some underlying LangChain embedding models might not have asynchronous embedding methods implemented, leading to this error when LlamaIndex attempts to use them asynchronously. The `LangchainEmbedding` wrapper handles this by falling back to synchronous calls, but you might see warnings.fixThis is often handled gracefully by the `LangchainEmbedding` wrapper, falling back to sync. If you strictly require async, ensure your specific LangChain embedding model explicitly supports `aembed_query` and `aembed_documents` methods.
Warnings
- breaking Both LlamaIndex and LangChain are rapidly evolving libraries. Import paths and class locations within `langchain` (e.g., moving to `langchain-community`) have changed frequently, leading to `ImportError` or `ModuleNotFoundError` if versions are not compatible or imports are not updated.
- gotcha The `llama-index-embeddings-langchain` package is merely a wrapper. You *must* install the underlying LangChain package that provides the actual embedding model you intend to use (e.g., `langchain-community` for `HuggingFaceEmbeddings`). Not installing this dependency will lead to runtime errors when the wrapped model is initialized.
- gotcha When using local HuggingFace embedding models via LangChain, ensure the model weights are downloaded correctly and that there are no network issues if it's the first time using a specific model. Indefinite loading times can indicate a problem with model download.
Install
-
pip install llama-index-embeddings-langchain -
pip install langchain-community
Imports
- LangchainEmbedding
from llama_index import LangchainEmbedding
from llama_index.embeddings.langchain import LangchainEmbedding
- HuggingFaceEmbeddings
from langchain.embeddings import HuggingFaceEmbeddings
from langchain_community.embeddings import HuggingFaceEmbeddings
Quickstart
import os
from llama_index.embeddings.langchain import LangchainEmbedding
from llama_index.core import Settings
from langchain_community.embeddings import HuggingFaceEmbeddings
# Ensure the necessary LangChain package is installed
# pip install langchain-community
# 1. Initialize a LangChain embedding model
# Using a local model for demonstration, no API key needed
lc_embed_model = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
# 2. Wrap the LangChain embedding model with LlamaIndex's LangchainEmbedding wrapper
embed_model = LangchainEmbedding(lc_embed_model)
# 3. Set the global embedding model for LlamaIndex
Settings.embed_model = embed_model
# 4. Example usage: get an embedding
text = "This is a test sentence for embedding."
embedding = Settings.embed_model.get_text_embedding(text)
print(f"Embedding length: {len(embedding)}")
print(f"First 10 dimensions of embedding: {embedding[:10]}")
# You can also use it directly without setting global settings
# direct_embedding = embed_model.get_text_embedding("Another sentence.")
# print(f"Direct embedding length: {len(direct_embedding)}")