What is .embed_documents()?
.embed_documents() is the batch embedding method on all LangChain embedding model classes. It accepts a List[str] and returns a parallel List[List[float]]—one dense vector per input text. Issuing a single batched call is far more efficient than looping over .embed_query() because it combines the inputs into the minimum number of API requests the provider allows, eliminating per-request overhead, connection setup, and often qualifying for bulk pricing.
The output dimensionality depends on the model: OpenAI text-embedding-3-small returns 1536-dimensional vectors by default (tunable via the dimensions= parameter on newer v3 models), while text-embedding-3-large returns up to 3072-d. HuggingFace sentence-transformers return whatever the underlying architecture defines—check the model card. Downstream similarity search requires that the query vector comes from the same model, same dimensions setting, and same configuration as the stored document vectors. Mixing these produces silently wrong results.
For production-scale indexing, pair .embed_documents() with CacheBackedEmbeddings to avoid re-embedding identical text on subsequent runs. The async variant .aembed_documents() fires sub-batches concurrently and can cut wall-clock time significantly when the provider supports parallel requests. Most LangChain vector store factories (Chroma.from_documents(), FAISS.from_documents()) call .embed_documents() internally, so you only call it directly when you need the raw vectors.
Use Cases
- • Index a document collection for a vector store
- • Batch encode chunks after text splitting
- • Pre-compute embeddings and cache to disk
- • Feed raw vectors to FAISS or a custom ANN index
- • Evaluate embedding quality across a corpus
- • Async pipeline embedding with aembed_documents()
Key Features
- ✓ Single batched API call for all inputs
- ✓ Far cheaper than looping embed_query()
- ✓ Returns parallel list of vectors (one per input)
- ✓ Async variant aembed_documents() for concurrent pipelines
- ✓ Used internally by all vector store factory methods
- ✓ Compatible with CacheBackedEmbeddings for disk caching
When NOT to Use
For a single real-time query — use embed_query() instead. Never call embed_documents() in a loop with one text per call; pass the entire list at once.
Notes
Rate limits apply at the token level
OpenAI enforces tokens-per-minute (TPM) limits, not just requests-per-minute. Embedding 10,000 long documents in one call still gets chunked by the SDK, but a single overly large sub-batch can hit the per-request token cap. Tune chunk_size on OpenAIEmbeddings(chunk_size=500) to control sub-batch size.
Dimensions parameter on OpenAI v3 models
OpenAIEmbeddings(model="text-embedding-3-large", dimensions=256) truncates the output vector to 256 dimensions. Shorter vectors reduce storage and ANN search cost but may hurt retrieval recall. You must use the same dimensions= setting at both indexing and query time — mismatches cause silent retrieval failures.
CacheBackedEmbeddings eliminates redundant API calls
Wrap any embedder with CacheBackedEmbeddings.from_bytes_store(embedder, store) to cache vectors keyed on a hash of the text content. Subsequent .embed_documents() calls with the same strings skip the API entirely — essential when iterating on a RAG pipeline without changing the corpus.
HuggingFace local models ignore dimensions and run on-device
HuggingFaceEmbeddings runs the model locally. Expect 100–1000 ms per batch depending on hardware, versus 10–100 ms for an OpenAI API call. Local embeddings have no rate limits and zero API cost, but require CPU/GPU memory for the model weights.
Method Signature
vectors = embeddings.embed_documents(texts)
Parameters
| Parameter | Type | Required | Purpose |
|---|---|---|---|
| texts | List[str] | Yes | List of text strings to embed in batch |
Return Value
Type:
List[List[float]]
Description:
One embedding vector per input text, in the same order
Example Output:
[[0.012, -0.034, ...], [0.087, 0.003, ...]]
Code Examples
Batch embed a list of documents
from langchain_openai import OpenAIEmbeddings
embeddings = OpenAIEmbeddings(model='text-embedding-3-small')
docs = [
'LangChain is a framework for building LLM applications.',
'RAG pipelines combine retrieval with language generation.',
'Vector stores hold dense embeddings for similarity search.',
]
vectors = embeddings.embed_documents(docs)
print(f'Embedded {len(vectors)} docs')
print(f'Each vector has {len(vectors[0])} dimensions')
Cache embeddings to avoid re-embedding on re-runs
from langchain_openai import OpenAIEmbeddings
from langchain.embeddings import CacheBackedEmbeddings
from langchain.storage import LocalFileStore
store = LocalFileStore('./embed_cache')
base_embedder = OpenAIEmbeddings(model='text-embedding-3-small')
cached_embedder = CacheBackedEmbeddings.from_bytes_store(
base_embedder, store, namespace=base_embedder.model
)
docs = ['LangChain intro text...', 'RAG explainer...']
# First call hits the API
vecs = cached_embedder.embed_documents(docs)
# Second call returns from disk cache — no API call
vecs = cached_embedder.embed_documents(docs)
Async batch embedding for FastAPI pipelines
import asyncio
from langchain_openai import OpenAIEmbeddings
embeddings = OpenAIEmbeddings(model='text-embedding-3-small')
async def embed_batch(texts):
return await embeddings.aembed_documents(texts)
docs = ['doc one', 'doc two', 'doc three']
vectors = asyncio.run(embed_batch(docs))
print(len(vectors)) # 3
Common Mistakes
❌ for doc in docs: embeddings.embed_query(doc) # N separate API calls, N× slower and N× more expensive
✅ embeddings.embed_documents(docs) # One batched API call
Related LangChain References
Browse the full LangChain API reference index to explore more classes, methods, and decorators, or start with the LangChain introduction tutorial for end-to-end context on building with .embed_documents() and the wider framework.
.embed_documents() FAQ
What does .embed_documents() do in LangChain?
Embed a list of texts in one batched API call, returning one dense vector per input. .embed_documents() is the batch embedding method on all LangChain embedding model classes. It accepts a List[str] and returns a parallel List[List[float]]—one dense vector per input text. Issuing a single batched call is far more efficient than looping over .embed_query() because it combines the inputs into the minimum number of API requests the provider allows, eliminating per-request overhead, connection setup, and often qualifying for bulk pricing. The output dimensionali…
Which LangChain classes support .embed_documents()?
.embed_documents() is available on Embedding models. Pin your installed LangChain version and verify the method exists in that release before deploying.
When should I use .embed_documents()?
Use .embed_documents() when your LangChain chains, agents, or pipelines need the behavior described in this guide.
What does .embed_documents() return?
.embed_documents() returns a List[List[float]]. One embedding vector per input text, in the same order
Does .embed_documents() have an async equivalent?
Yes — use aembed_documents() for async contexts such as FastAPI handlers or asyncio-based pipelines. It is the non-blocking counterpart to .embed_documents().
Where can I explore more LangChain API reference pages?
Open the LangChain API reference index on DevShelfHub to browse classes, methods, and decorators, each with runnable examples, parameters, common mistakes, and cross-links.