question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cannot hash SwigPyObject (or any binded objects)

See original GitHub issue

Summary

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:

  1. create a faiss index
  2. Create some streamlit basic app
  3. Try to put the faiss index in the cache of the function (@st.cache())
  4. 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:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
s4chincommented, Apr 13, 2021

@Katy-Wei Here’s my code -

@st.cache(hash_funcs={"builtins.SwigPyObject": lambda _: None})
def get_index(embeddings, gpu_nos):
    index_gpu = create_index_gpu(gpu_nos) # Function to create faiss index on gpu(not cached)
    index_gpu.add(embeddings)
    return index_gpu

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?

1reaction
s4chincommented, Apr 13, 2021

@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})

Read more comments on GitHub >

github_iconTop Results From Across the Web

UnhashableType: Cannot hash object of type _thread._local
I keep getting the same error returned when I use st.cache before a function doing a complex query & pandas manipulation.
Read more >
Make SWIG wrapped builtin class "hashable" in Python
For performance reasons, I'm interested in switching some of the wrapping to use SWIG's -builtin option, which removes the layers of Python ...
Read more >
https://home.cc.umanitoba.ca/~psgendb/doc/pkg/Rcor...
Jellyfish binding """ from sys import version_info if version_info >= (2,6,0): ... pass _newclass = 0 class MerDNA(_object): """Class representing a mer.
Read more >
Xapian Python3 Bindings 1.4.21 documentation
This class can not be instantiated directly - instead a subclass should be ... get_ebound(self): """ Return a bound on the full size...
Read more >
tpm2-software/community - Gitter
hrm, perhaps the problem is using debian? i looked into the tpm2-tss repository and i see ... I get TypeError: 'SwigPyObject' object is...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found