New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
UnhashableParamError for Pydantic models when using st.cache_data() #6290
Comments
So two things:
# This totally works
@st.cache_data()
def identity(name: str):
return Person(name=name)
Disclaimer: I'm not a pickle expert, I think this approach works but not guaranteed import streamlit as st
import pickle
import functools
from pydantic import BaseModel
class Person(BaseModel):
name: str
def __reduce__(self):
return (functools.partial(Person, name=self.name), tuple())
def _repr_html_(self):
return f"I am a Person named {self.name}"
@st.cache_data()
def identity(person: Person):
return person
person = identity(Person(name="Lee"))
st.write(person)
person = pickle.loads(pickle.dumps(person))
st.write(person) cc @tconkling who can maybe help keep me honest |
Ah - Digging through some other issues, I also found a simpler solution using from dataclasses import dataclass
from pydantic import BaseModel
# need init=False to prevent it from overriding Pydantic's init method
@dataclass(init=False)
class Person(BaseModel):
name: str
def _repr_html_(self):
return f"I am a Person named {self.name}" |
@sfc-gh-jcarroll thanks for looking into this issue. Using dataclasses seems the best way to resolve this problem in the example I gave. However, changing the It would be best if Streamlit natively supports using pydantic models as input arguments to cachable functions since it is a common use case. Alternatively, bringing back the |
Count me as another vote endorsing @mmz-001 's opinion above. I just ran into the same problem. |
I believe this will be fixed with #6502 in the next release. Let us know if it doesn't seem to solve the issue. |
Fixed with 1.24.0 |
This is still an issue for me in 1.24.1. UnhashableParamError: Cannot hash argument 'reporter' (of type reporting.Reporter) in 'load_data'. Here is the basics of the reporting.Reporter class class Reporter: def hash(self): |
Checklist
Summary
The new
st.cache_data()
decorator can't hash arguments which are Pydantic models, howeverst.cache()
seems to work fine.Reproducible Code Example
Steps To Reproduce
Expected Behavior
Streamlit should be able to cache the parameters.
Current Behavior
Error:
Is this a regression?
Debug info
Additional Information
No response
Are you willing to submit a PR?
The text was updated successfully, but these errors were encountered: