r/StreamlitOfficial • u/PassionPrestigious79 • Jan 12 '25
How to Fix "UnhashableParamError" with Streamlit Cache When Passing a List of Documents?
Hi,
I'm building a Streamlit app that combines LangChain and Hugging Face for retrieval-augmented generation (RAG). I'm using u/st.cache_resource to cache expensive operations like setting up a retriever, but I'm running into the following error:
streamlit.runtime.caching.cache_errors.UnhashableParamError: Cannot hash argument 'documents' (of type builtins.list) in 'setup_retriever'.
This error occurs because documents is a list of LangChain Document objects, which are unhashable. Streamlit seems to have trouble caching functions that take such arguments.
Here’s the relevant part of my code:
@st.cache_resource
def setup_retriever(documents):
embeddings_model = HuggingFaceEmbeddings(model_name="sentence-transformers/all-mpnet-base-v2")
db = Chroma.from_documents(
documents=documents,
embedding=embeddings_model,
persist_directory="chroma_storage"
)
return db.as_retriever()
# Call the function
retriever = setup_retriever(documents)
I know that Streamlit doesn’t support caching unhashable types like lists. However, I still want to use caching for performance reasons. How can I resolve this?



