Cacheing output of expensive function calls

Would it be accurate to claim that streamlit.cache is designed more for cacheing data import activity rather than the outputs of computation? I’m asking because, based on the limited time I have spent with it, it has problems with hashing objects used inside of function calls. For example, I am using rpy2 to call an R script inside of a function, but it cannot deal with one or more of the objects it uses:

Streamlit cannot hash an object of type <class 'rpy2.robjects.conversion.Converter'>.

Is there a way of making cacheing work with arbitrary function calls? I was hoping that the output (in this case, a numpy array) was all that was being cached, but it looks like the serialization goes deeper than that.

1 Like

Hello! Thanks for the question.

Many types of objects can be cached as long as python can find a way to serialize them. It may be that the objects returned by the function you’re having trouble with are not exposing any serialization methods to Python.

Perhaps you can write a little more of a wrapper function around the call to the R script such that the inputs to the function as well as the data returned from the object are converted to something more streamlit-native, like a Dataframe or a python dictionary? You should be able to @st.cache such a function without issue.

If you’re already doing that, let us know – I’m totally just making guesses here without seeing your code.

Also, check out this very similar thread about cacheing with different types of objects.

st.cache is always improving – it’s actually one of our highest priorities right now. You can try out the latest streamlit off the develop branch if you’re eager to find out if recent updates might solve your particular issue.

1 Like

Thanks for the response; this is all very helpful.