The ErrorWhen running evaluation metrics in Ragas (Retrieval Augmented Generation Assessment), you might encounter a crash during the evaluate() call. The traceback usually looks like this:
ValueError: Dataset does not contain required column ['contexts']
This happens even if you feel your dataset is complete. The Ragas library is strict about its input schema. If a single required column is missing or misspelled, the evaluation process halts immediately.
Root CauseRagas metrics require specific column names in your Hugging Face Dataset object to perform calculations. Depending on the metrics you choose (like context_precision or context_recall), the library expects a column literally named contexts.
Common reasons for this error include:
- Naming Mismatch: Your dataset uses
context(singular),source_documents, orretrieved_chunksinstead ofcontexts.- Data Type Issues: Thecontextscolumn must be alist of strings(List[str]), not a single string or a list of objects.- Missing Dictionary Keys: When converting a Python dictionary to a Hugging Face Dataset, the key for your retrieved documents doesn't match what Ragas expects.## Step-by-Step Fixes### Approach 1: Renaming Columns in Hugging Face DatasetIf you already have a dataset object but the names are wrong, use therename_columnmethod. This is the most efficient way to fix the error without re-processing your data.
from datasets import Dataset
# Sample data with the 'wrong' column name
data = {
"question": ["What is Ragas?"],
"answer": ["Ragas is an evaluation framework."],
"source": [["Ragas helps evaluate RAG pipelines."]], # This should be 'contexts'
"ground_truth": ["Ragas is a framework for RAG evaluation."]
}
ds = Dataset.from_dict(data)
# Fix: Rename 'source' to 'contexts'
if "contexts" not in ds.column_names:
ds = ds.rename_column("source", "contexts")
print(ds.column_names)
# Output: ['question', 'answer', 'contexts', 'ground_truth']
Approach 2: Formatting Contexts CorrectlyRagas expects contexts to be a list of strings for each row. If your RAG pipeline returns a single string, you must wrap it in a list. If it returns LangChain Document objects, you must extract the page_content.
# Incorrect format (will cause issues even if named correctly)
# contexts: "This is a single string"
# Correct format
# contexts: ["Chunk 1 text", "Chunk 2 text"]
# Example of extracting from LangChain documents
retrieved_docs = retriever.get_relevant_documents(query)
context_strings = [doc.page_content for doc in retrieved_docs]
# Add to your data dictionary
eval_data = {
"question": [query],
"answer": [result_from_llm],
"contexts": [context_strings], # Note the nested list
"ground_truth": [actual_answer]
}
Approach 3: Using the EvaluationDataset Class (Ragas 0.2+)Newer versions of Ragas provide a more structured way to handle data. Instead of passing a raw dataset, you can use the EvaluationDataset or SingleTurnSample classes which help validate the schema before you run the heavy evaluation logic.
from ragas import EvaluationDataset
# This will raise a clear validation error early if 'contexts' is missing
eval_ds = EvaluationDataset.from_dict({
"question": [...],
"answer": [...],
"contexts": [[...]],
"ground_truth": [...]
})
VerificationTo confirm the fix, run a minimal evaluation with just one metric. This saves API credits while verifying the schema.
from ragas import evaluate
from ragas.metrics import context_precision
# If this runs without ValueError, your schema is correct
result = evaluate(
dataset=ds,
metrics=[context_precision]
)
print("Evaluation successful:", result)
PreventionTo avoid this error in future projects, implement a simple mapping function for your RAG output. Since different frameworks (LangChain, LlamaIndex, Haystack) use different naming conventions for retrieved nodes, a mapper ensures consistency.
def prepare_ragas_dataset(rag_results):
"""
Standardizes RAG output to Ragas format.
rag_results: List of dicts containing 'query', 'result', 'source_documents'
"""
formatted_data = {
"question": [],
"answer": [],
"contexts": [],
"ground_truth": []
}
for item in rag_results:
formatted_data["question"].append(item["query"])
formatted_data["answer"].append(item["result"])
# Ensure contexts is a list of strings
formatted_data["contexts"].append([doc.page_content for doc in item["source_documents"]])
formatted_data["ground_truth"].append(item["expected_answer"])
return Dataset.from_dict(formatted_data)
By enforcing this schema at the data ingestion layer, you prevent the ValueError from ever reaching the Ragas evaluation engine.

