Streamlit bug with number_input in 1.50.0

I wrote the below very simple repro for a strange behavior where “number_input” is not returning proper values. Run the repro in 1.38.0 and it works perfectly as expected. Try it in 1.50.0. It does not work correctly.

Whats going on? I had updated streamlit version and my finance app simply started to misbehave…. Sigh!

import streamlit as st

choice = st.selectbox(
    'Select a choice: A means 5.2. B means 10.2',
    ['A', 'B'],
    key='sbox'
)

st.write('You selected {}'.format(choice))

if choice == 'A':
    widget_value=5.2
else:
    widget_value=10.2

ui_name='A Number'
ui_control = st.number_input(
                    ui_name,
                    key = ui_name,
                    value = widget_value,
                    step=0.05,
                    format='%.6f'
                )
st.write('Value returned = {}'.format(ui_control))

Hey, thanks for sharing a clear repro and welcome to the Streamlit community! :partying_face: This is a known issue introduced in Streamlit 1.50.0: when you use a widget with a key and set its value dynamically (like your number_input), the widget doesn’t update as expected after the first run. This is due to a change in how Streamlit handles widget identity and session state—now, the key takes precedence, and updating the value parameter alone won’t reset the widget’s value after initialization. That’s why your code works in 1.38.0 but not in 1.50.0.

To fix this, you should update the value in st.session_state[ui_name] directly, or use a callback to synchronize the widget value. See the official workaround and explanation in the Streamlit docs and related GitHub issues. Here’s a quick fix for your code:

import streamlit as st

choice = st.selectbox(
    'Select a choice: A means 5.2. B means 10.2',
    ['A', 'B'],
    key='sbox'
)

st.write('You selected {}'.format(choice))

if choice == 'A':
    widget_value = 5.2
else:
    widget_value = 10.2

ui_name = 'A Number'

if ui_name not in st.session_state or st.session_state[ui_name] not in [5.2, 10.2]:
    st.session_state[ui_name] = widget_value

ui_control = st.number_input(
    ui_name,
    key=ui_name,
    step=0.05,
    format='%.6f'
)
st.write('Value returned = {}'.format(ui_control))

If you want more detail or a step-by-step breakdown, just let me know! And if you have a repo or more code, feel free to share for deeper debugging. Community folks, feel free to jump in with your own tips or workarounds!

Sources:

1 Like

Why do you change such basic behavior and make our life so difficult? Twinkle boys..

Agent Streamy! Now I want to set the number from user as well as through the select box in my code. How do I do that?

@AgentStreamy