Streamlit alternative st.session_state

Any alternate dictionary forms of storage I could store my oauth_state or flow object that ain’t wiped by st.session_state? For some reason when I get st.query_params[“state”] and st.session_state[“oauth_state”] after these lines of code:

if "oauth_flow" not in st.session_state:

    flow = Flow.from_client_secrets_file(
        "credentials.json",
        scopes=SCOPES,
        redirect_uri=REDIRECT_URI,
    )

    authorization_url, state = flow.authorization_url(
        access_type="offline",
        prompt="consent",
    )

    st.session_state["oauth_flow"] = flow
    st.session_state["oauth_state"] = state
    st.session_state["authorization_url"] = authorization_url


Somehow the memory of st.session_state is not constant when a new tab is opened. I don't know what module or dictionary to use that is easy-to-implement and stuff. Thanks

Hey, thanks for your question! You’re absolutely right—st.session_state is only persistent for the current browser tab/session and gets wiped on refresh, new tabs, or after OAuth redirects. This is a common pain point for OAuth flows in Streamlit. There’s no built-in Streamlit dictionary or module that persists across sessions/tabs, as st.session_state is intentionally ephemeral and per-session.

For persistence across reloads or redirects, you’ll need to use an external store (like a database, file, or cookies). Some folks use browser cookies (with packages like extra-streamlit-components), or serialize state to disk (e.g., with pickle or JSON), or pass state via URL query parameters. See examples and discussion in this forum thread and this one. There’s no official Streamlit API for persistent, cross-session state.

Sources: