Using a list of class instances as selectbox options leads to serialization error

I wanted to use the following class

class Metric():
    def __init__(self, label: str, metric: str) -> None:
        self.label = label
        self.metric = metric
    def get_label(self) -> str:
        return self.label
    def get_metric(self) -> str:
        return self.metric

        label="Offer Count",
        label="Median Price",
        label="Median Price per Day",
        label="Median Offer Count per Request",

and use the list of Metric instances as options for a selectbox like the following

        label="Select a metric",
        format_func=lambda metric: metric.get_label(),

which leads to this error:

Exception in thread ScriptRunner.scriptThread:
Traceback (most recent call last):
  File "/usr/local/lib/python3.9/", line 980, in _bootstrap_inner
  File "/usr/local/lib/python3.9/", line 917, in run
    self._target(*self._args, **self._kwargs)
  File "/usr/local/lib/python3.9/site-packages/streamlit/scriptrunner/", line 289, in _run_script_thread
    widget_states = self._session_state.get_widget_states()
  File "/usr/local/lib/python3.9/site-packages/streamlit/state/", line 82, in get_widget_states
    return self._state.get_widget_states()
  File "/usr/local/lib/python3.9/site-packages/streamlit/state/", line 576, in get_widget_states
    return self._new_widget_state.as_widget_states()
  File "/usr/local/lib/python3.9/site-packages/streamlit/state/", line 249, in as_widget_states
    states = [
  File "/usr/local/lib/python3.9/site-packages/streamlit/state/", line 252, in <listcomp>
    if self.get_serialized(widget_id)
  File "/usr/local/lib/python3.9/site-packages/streamlit/state/", line 230, in get_serialized
    serialized = metadata.serializer(item.value)
  File "/usr/local/lib/python3.9/site-packages/streamlit/elements/", line 160, in serialize_select_box
    return index_(opt, v)
  File "/usr/local/lib/python3.9/site-packages/streamlit/", line 129, in index_
    raise ValueError("{} is not in iterable".format(str(x)))
ValueError: <Metric.Metric object at 0x7f7edc543940> is not in iterable

The selectbox itself is working absolutely fine. Can someone help me with this issue?

Hi @itsmartinhi, welcome to the Streamlit community!

Is this a simplified example of the class you are trying to provide or is this your actual example? In general, for the pattern you are showing, I’m sure having a class instance here is adding to the code clarity, so the easiest thing would be to remove it and use a list of strings.


Thank you for your response!
The class displayed here is kinda simplified. I worked around the issue by wrapping the class instances into a dictionary. Pretty ugly, but it works, and I still have the full functionality of my class.