ValueError
llama_index.embeddings.base.ValueError
Stack trace
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.") 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
No embedding model instance was provided when initializing the LlamaIndex service or client.
Instantiate and pass a valid embedding model (e.g., OpenAIEmbedding) to the LlamaIndex constructor or embedding parameter before calling embedding methods.
Embedding model variable was declared but never assigned or was set to None.
Ensure the embedding model variable is properly assigned with a valid embedding model object before use.
Using an outdated or incompatible LlamaIndex version that requires explicit embedding model setup.
Upgrade to a compatible LlamaIndex version and follow the updated embedding model initialization pattern.
Code: broken vs fixed
from llama_index import ServiceContext
service = ServiceContext() # embedding model not set
embeddings = service.embed_documents(docs) # triggers ValueError here 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') 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.