Is it possible to have a streamlit multiselect input with default values that are not present in columns array?

Hi everyone,
I’m developing a streamlit app where I want my streamlit multiselect to have default values ( array ) based on an LLM call.

But, I’m getting error when one or more element of default array is not present in the options array. I know this is streamlit’s characteristic but I want a feature like this. Is there any workaround for my use case ?

Workflow of my app :

User makes an LLM call. LLM returns a JSON . These JSON values get prepopulated in the form. All of the form inputs are of multiselect type and I use ‘default’ array to prepopulate these JSON values, I get ‘options’ array of the multiselect by an api call . I get error when one or more element of the default array is not present in the options array.

Hello,
Do you have some example code?
I used to have kind of the same isue, but not sure if it will work for your specific problem :

options = ["apple", "banana", "cherry", "date"]

# Default values returned by LLM call
default_values = ["apple", "blueberry", "cherry"]

# Filter the default values to only include those in options
filtered_defaults = [value for value in default_values if value in options]

# Use the filtered defaults in the multiselect
selected_values = st.multiselect(
    "Select fruits",
    options=options,
    default=filtered_defaults
)

With only selectbox :

        index_adjust = [YOURLIST].index(self.default_options.get("FROMLLM",  [YOURLIST][0]))
        options['adjust_pfs'] = st.selectbox('This is an example', [YOURLIST],
                                             key=f'adjust', index=index_adjust)

Hi @Faltawer ,
thanks for replying.
I’m following a similar approach like the code you shared. But, I want that in multiselect all of the values returned by the LLM should be set as it is.

I know it is not possible with multiselect because that is it’s default behaviour.
But I somehow need to have all the options returned by the LLM.

Is there any workaround for this in streamlit ?

Just add the values returned by the LLM to the multiselect options?

1 Like

yes , this looks like the only solution for me when using Streamlit

What other kind of solutions could there be? If a selected value is not in the options and it gets accidentally deleted by the user, it cannot be added again. That doesn’t look like good UX.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.