Cannot hash SwigPyObject (or any binded objects)
See original GitHub issueSummary
Streamlit doesn’t knows how to hash SwigPyObjects. No problem, you can use hash_funcs
parameter of the streamlit.cache
decorator (work really good for elasticsearch index or other fancy stuff).
However, SwigPyObjects are NO python objects (they are just bindings) so there is no types in python for it.
Streamlit indicates builtins.SwigPyObjects
in the error messages but python doesn’t have a SwigPyObject in the builtins.
So it’s not possible to hsah or to silentely ask to do nothing with this types.
In my use case, I have a dictionary with one elasticsearch index and one faiss index. The one with elasticsearch is dealed with a hash_funcs={elasticsearch.Elasticsearch: id}
but I cannot do the same for faiss.
Steps to reproduce
What are the steps we should take to reproduce the bug:
- create a faiss index
- Create some streamlit basic app
- Try to put the faiss index in the cache of the function (
@st.cache()
) - Watch it fail
Expected behavior:
Allow to ignore or deal with non python types (for me ignoring them it’s okay)
Actual behavior:
Traceback
UnhashableTypeError: Cannot hash object of type builtins.SwigPyObject, found in the arguments of get_fig_embeddings().
While caching the arguments of get_fig_embeddings(), Streamlit encountered an object of type builtins.SwigPyObject, which it does not know how to hash.
To address this, please try helping Streamlit understand how to hash that type by passing the hash_funcs argument into @st.cache. For example:
@st.cache(hash_funcs={builtins.SwigPyObject: my_hash_func})
def my_func(...):
...
If you don't know where the object of type builtins.SwigPyObject is coming from, try looking at the hash chain below for an object that you do recognize, then pass that to hash_funcs instead:
Object of type builtins.SwigPyObject: <Swig Object of type 'faiss::IndexFlat *' at 0x7f0d9845ff00>
Object of type builtins.tuple: ('this', <Swig Object of type 'faiss::IndexFlat *' at 0x7f0d9845ff00>)
Object of type builtins.dict: {'this': <Swig Object of type 'faiss::IndexFlat *' at 0x7f0d9845ff00>}
Object of type faiss.swigfaiss.IndexFlat: <faiss.swigfaiss.IndexFlat; proxy of <Swig Object of type 'faiss::IndexFlat *' at 0x7f0d9845ff00> >
Object of type builtins.tuple: ('faiss', <faiss.swigfaiss.IndexFlat; proxy of <Swig Object of type 'faiss::IndexFlat *' at 0x7f0d9845ff00> >)
Object of type builtins.dict: {'graph': <networkx.classes.digraph.DiGraph object at 0x7f0ded9d7d60>, 'db': <Elasticsearch([{}])>, 'faiss_idx': ['List of lots of hashes that i cut'], 'faiss': <faiss.swigfaiss.IndexFlat; proxy of <Swig Object of type 'faiss::IndexFlat *' at 0x7f0d9845ff00> >}
Object of type builtins.tuple: ({'graph': <networkx.classes.digraph.DiGraph object at 0x7f0ded9d7d
Is this a regression?
No
Debug info
- Streamlit version: 0.69.2
- Python version: 3.8.6
- Using python venv native module (same problem with no virtual env)
- OS version: Arhclinux kernel zen 5.9.1
- Browser version: Firefox 82.0
Additional information
Thanks in advance, streamlit is really a wondefull tool
Issue Analytics
- State:
- Created 3 years ago
- Reactions:1
- Comments:7 (1 by maintainers)
@Katy-Wei Here’s my code -
This works fine for me and gives me no errors/weird behaviors.
I also think the caching is working since if I cache the
create_index_gpu()
function, I get the same index if I call the function multiple times. Also, my application would be much slower if caching didn’t work. Perhaps your object isn’t simply an index but something else?@Katy-Wei I also had the same issue. I solved it by adding this decorator instead of just
@st.cache
-@st.cache(hash_funcs={"builtins.SwigPyObject": lambda _: None})