High severity beginner · Fix: 2-5 min

ValueError

llama_index.embeddings.base.ValueError

What this error means
LlamaIndex raises this error when you try to create embeddings without specifying or initializing an embedding model.

Stack trace

traceback
ValueError: Embedding model not set. Please specify an embedding model before calling embed_documents or embed_query.
  File "app.py", line 42, in main
    embeddings = service.embed_documents(docs)  # triggers error
  File "llama_index/embeddings/base.py", line 123, in embed_documents
    raise ValueError("Embedding model not set. Please specify an embedding model before calling embed_documents or embed_query.")
QUICK FIX
Instantiate and assign a valid embedding model to your LlamaIndex client before calling embed_documents or embed_query.

Why it happens

LlamaIndex requires an embedding model instance to be set before embedding documents or queries. This error occurs if you call embedding methods without initializing or passing a valid embedding model, leaving the internal embedding attribute unset.

Detection

Check if your embedding model variable or parameter is None or uninitialized before calling embed_documents or embed_query, or catch ValueError and log the missing embedding model state.

Causes & fixes

1

No embedding model instance was provided when initializing the LlamaIndex service or client.

✓ Fix

Instantiate and pass a valid embedding model (e.g., OpenAIEmbedding) to the LlamaIndex constructor or embedding parameter before calling embedding methods.

2

Embedding model variable was declared but never assigned or was set to None.

✓ Fix

Ensure the embedding model variable is properly assigned with a valid embedding model object before use.

3

Using an outdated or incompatible LlamaIndex version that requires explicit embedding model setup.

✓ Fix

Upgrade to a compatible LlamaIndex version and follow the updated embedding model initialization pattern.

Code: broken vs fixed

Broken - triggers the error
python
from llama_index import ServiceContext

service = ServiceContext()  # embedding model not set
embeddings = service.embed_documents(docs)  # triggers ValueError here
Fixed - works correctly
python
import os
from llama_index import ServiceContext
from llama_index.embeddings.openai import OpenAIEmbedding

os.environ['OPENAI_API_KEY'] = os.getenv('OPENAI_API_KEY')  # ensure env var is set
embedding_model = OpenAIEmbedding()
service = ServiceContext.from_defaults(embed_model=embedding_model)  # embedding model set
embeddings = service.embed_documents(docs)  # works now
print('Embeddings generated successfully')
Added explicit OpenAIEmbedding instantiation and passed it to ServiceContext to ensure the embedding model is set before embedding calls.

Workaround

Wrap embed_documents calls in try/except ValueError, and if caught, initialize a default embedding model on the fly before retrying the call.

Prevention

Always initialize and pass a valid embedding model instance when creating LlamaIndex service or client objects to avoid unset embedding errors.

Python 3.9+ · llama-index >=0.5.0 · tested on 0.6.x
Verified 2026-04
Verify ↗

Community Notes

No notes yetBe the first to share a version-specific fix or tip.