Caching issue with sqlalchemy

I am using the SQL alchemy package to run queries on a postgres db.

from sqlalchemy import create_engine
from sqlalchemy import text

I am getting the following error when trying to cache results:

Streamlit cannot hash an object of type <class ‘sqlalchemy.engine.base.Engine’>.

Happy to share more details if needed.

Short answer, try:

@st.cache(ignore_hash=True)
def ...

But please know that this is an advanced usage of st.cache which will not check for mutation of the return value, in this case sqlalchemy.engine.base.Engine, which can lead to subtle bugs if you’re not careful!

Please let us know if you run into those bugs and we’ll see if we can help!