How to disable hashing for parameter of imported function (not using st.experimental_memo as decorator)

So I’m getting this error:

streamlit.caching.cache_errors.UnhashableParamError: Cannot hash argument ‘model’ (of type project.models.ClassificationModel) in ‘_get_labels’.

To address this, you can tell Streamlit not to hash this argument by adding a
leading underscore to the argument’s name in the function signature:

@st.experimental_memo
def _get_labels(_model, ...):
    ...

However, I am defining the _get_labels function in another module and importing it.

I would like to apply the st.experimental_memo function like this:

cached_get_labels = st.experimental_memo(_get_labels)

How can I disable hashing for model in this case?

Hey @StefanBrand,

Sounds like you might want to create a memoized wrapper function that just passes its arguments to the un-memoized _get_labels function:

from wherever import _get_labels

@st.experimental_memo
def cached_get_labels(_model, ...):
    return _get_labels(_model, ...)

Thanks, I came up with this:

@st.experimental_memo
def cached_get_labels(*args, **kwargs):
    return _get_labels(kwargs.pop("_model"), *args, **kwargs)

Edit: This works as well:

@st.experimental_memo
def cached_get_labels(_model, *args, **kwargs):
    return _get_labels(_model, *args, **kwargs)

The solution is not ideal. With st.cache it is possible to do something like this and use it as a function:

from functools import partial


st_cache_hashs = partial(
    st.cache, hash_funcs={Labels: lambda _: input_parameters}
)

You’re right, you can’t use partial to build the wrapper if you have “don’t hash me” arguments - this is a limitation of the new design.

We’ve internally considered allowing an alternate, explicit syntax for unhashable arguments, something like:

@st.experimental_memo(not_hashed={"model"})
def cached_get_labels(model, ...):
  return _get_labels(model, ...)

Which would allow for constructing wrappers without creating a new function. (We’re currently leaning away from this alternate syntax, but if this is something you feel strongly about, please open an issue on Github!)