run-llama/llama_index · error · ValueError
The embedding file {file_path} is empty.
Error message
The embedding file {file_path} is empty. What it means
load_embedding(file_path) reads a CSV-of-floats file produced by save_embedding() and returns the first line. If the file exists but contains zero lines (empty file), the for-loop body never executes and the function raises ValueError — the file was created but no embedding was ever written to it.
Source
Thrown at llama-index-core/llama_index/core/embeddings/utils.py:27
from llama_index.core.callbacks import CallbackManager
from llama_index.core.embeddings.mock_embed_model import MockEmbedding
from llama_index.core.utils import get_cache_dir
EmbedType = Union[BaseEmbedding, "LCEmbeddings", str]
def save_embedding(embedding: List[float], file_path: str) -> None:
"""Save embedding to file."""
with open(file_path, "w", encoding="utf-8") as f:
f.write(",".join([str(x) for x in embedding]))
def load_embedding(file_path: str) -> List[float]:
"""Load embedding from file. Will only return first embedding in file."""
with open(file_path, encoding="utf-8") as f:
for line in f:
return [float(x) for x in line.strip().split(",")]
raise ValueError(f"The embedding file {file_path} is empty.")
def resolve_embed_model(
embed_model: Optional[EmbedType] = None,
callback_manager: Optional[CallbackManager] = None,
) -> BaseEmbedding:
"""Resolve embed model."""
from llama_index.core.settings import Settings
try:
from llama_index.core.bridge.langchain import Embeddings as LCEmbeddings
except ImportError:
LCEmbeddings = None # type: ignore
if embed_model == "default":
if os.getenv("IS_TESTING"):
embed_model = MockEmbedding(embed_dim=8)
embed_model.callback_manager = callback_manager or Settings.callback_managerView on GitHub (pinned to afd0fef371)
Solutions
- Check size before loading: if os.path.getsize(path) == 0: regenerate the embedding and save_embedding(vec, path)
- Re-run the code that produces the embedding (save_embedding) so the file contains one comma-separated line
- Treat empty cache entries as cache misses and recompute, then overwrite the file
Example fix
// before
vec = load_embedding("emb.txt") # file is empty -> ValueError
// after
import os
if os.path.getsize("emb.txt") == 0:
vec = embed_model.get_text_embedding(text)
save_embedding(vec, "emb.txt")
else:
vec = load_embedding("emb.txt") Defensive patterns
Strategy: validation
Validate before calling
import os
if not os.path.exists(path) or os.path.getsize(path) == 0:
vec = embed_model.get_text_embedding(text)
save_embedding(vec, path)
vec = load_embedding(path) Try / catch
try:
vec = load_embedding(path)
except ValueError as e:
if "is empty" in str(e):
vec = embed_model.get_text_embedding(text)
save_embedding(vec, path)
else:
raise Prevention
- Treat embedding files as a cache: validate size before reading, recompute on miss
- Write cache files atomically (write temp then rename) so crashes never leave empty files
- Wrap save/load in a small EmbeddingCache class that owns both directions
When it happens
Trigger: Calling load_embedding on a file that is 0 bytes (created by open(..., 'w') without a write, an interrupted save_embedding, or touch); pointing at a truncated/corrupted cache file.
Common situations: A crashed or killed embedding-save job left empty files; filesystem issues truncating cache files; code that pre-creates placeholder files then fails to populate them.
Related errors
- embeddings_cache must be of type BaseKVStore
- embeddings_cache must be defined
- No embeddings to aggregate
- Embedding loading requires a class_name
- Invalid Embedding name: {name}
AI-assisted analysis of run-llama/llama_index@afd0fef371 (2026-08-15).
Data as JSON: /api/errors/7a14bdff1436fcc3.
Report an issue: GitHub.