Session_state needs two clicks to perform action

Hello everyone, greetings from Brazil, I have a problem with ā€œst.session_stateā€, in which I need to execute the action twice for it to be executed, Iā€™m using ā€œon_changeā€ and even so it only works after incrementing and running it for the second time turn. I apologize if it was a little confusing, my English isnā€™t that good.

    with col2:
            def cid_change():
                st.session_state.cid_change = True
                st.rerun()
            container = st.container()
            
            for key, value in st.session_state.df.items():
                if key == "CID":
                    st.session_state.inputs[key] = st.text_input(key, value.upper(), on_change=cid_change).upper()
                    if st.session_state.cid_change:
                        temp_motivo = cid_query(st.session_state.inputs["CID"])
                        st.session_state.inputs["Motivo do Afastamento"] = temp_motivo
                        st.session_state.df = st.session_state.inputs
                        st.session_state.cid_change = False
                else:
                    st.session_state.inputs[key] = st.text_input(key, value)

Hello,

Did you read this document Widget updating for every second input when using session state - Streamlit Docs?

The code is iterating session_state df and then it is assigning it in the loopā€¦ I donā€™t think itā€™s a good ideaā€¦