The error you're encountering stems from a type mismatch between the key used to index RAG_results and the expected type for list indexing in Python. Let's break this down and provide a clear solution.
🧠 Root Cause
You're using RAG_results = [""] * len(data) which creates a list of strings. Lists in Python only accept integer indices (e.g., RAG_results[0], RAG_results[1], etc.).
However, in the loop:
for idx, row in tqdm(data.iterrows()):
The idx variable represents the DataFrame index, which may not be an integer. It could be a string, datetime, or any other hashable type, depending on how your DataFrame is structured. This is why Pylance is complaining:
Argument of type "Hashable" cannot be assigned to parameter "key" of type "slice[Any, Any, Any]"
This means idx is not compatible with list indexing (which only allows integers).
✅ Solution
To fix this, you have two options:
✅ Option 1: Use enumerate to get the position instead of the index
Use enumerate(data.iterrows()) to get the row index (position), which is always an integer. This ensures compatibility with list indexing:
queries = []
query_indices = []
for pos, (idx, row) in tqdm(enumerate(data.iterrows())):
query = row[col_to_perform_rag]
if query.strip().lower() in ["", "not found"]:
RAG_results[pos] = "" # pos is an integer
elif query.strip().lower() == "healthy":
RAG_results[pos] = "Healthy"
else:
queries.append(query)
query_indices.append(pos) # pos is the position, not the original index
This ensures that you're using integer indices to update the list RAG_results.
✅ Option 2: Convert idx to an integer (if safe)
If you must use the original index, ensure it's an integer before using it as a key:
for idx, row in tqdm(data.iterrows()):
if isinstance(idx, int):
query = row[col_to_perform_rag]
if query.strip().lower() in ["", "not found"]:
RAG_results[idx] = ""
elif query.strip().lower() == "healthy":
RAG_results[idx] = "Healthy"
else:
queries.append(query)
query_indices.append(idx)
else:
# Handle non-integer index case (optional)
pass
This is less recommended unless you're certain all indices are integers.
📌 Why This Happens
data.iterrows() yields (index, row) pairs.
index can be any hashable type (not necessarily an integer).
RAG_results is a list, which only accepts integer indices.
🧪 Quick Fix (Recommended)
Replace your loop with:
for pos, (idx, row) in tqdm(enumerate(data.iterrows())):
query = row[col_to_perform_rag]
if query.strip().lower() in ["", "not found"]:
RAG_results[pos] = ""
elif query.strip().lower() == "healthy":
RAG_results[pos] = "Healthy"
else:
queries.append(query)
query_indices.append(pos)
This ensures pos is always an integer and avoids the Pylance error.
🧩 Final Note
If you're trying to map results back to the original DataFrame, you can later assign RAG_results as a new column:
data['RAG_Result'] = RAG_results
This will correctly align the results with the original DataFrame's rows, provided the loop order matches the DataFrame's index.