Skip to content

fix(stapp): fall back to st.rerun() when fragment scope rerun is unavailable#574

Open
Kailigithub wants to merge 1 commit into
lsdefine:mainfrom
Kailigithub:fix/stapp-fragment-rerun-startup-crash
Open

fix(stapp): fall back to st.rerun() when fragment scope rerun is unavailable#574
Kailigithub wants to merge 1 commit into
lsdefine:mainfrom
Kailigithub:fix/stapp-fragment-rerun-startup-crash

Conversation

@Kailigithub
Copy link
Copy Markdown
Contributor

Summary

Fixes #564

The Streamlit sidebar in frontends/stapp.py crashes on startup when the persisted LLM selectbox value (st.session_state['sidebar_llm_select']) disagrees with the agent's current llm_no:

StreamlitAPIException: scope="fragment" can only be specified from
@st.fragment-decorated functions during fragment reruns.

Root cause

render_sidebar() is decorated with @st.fragment. On the initial app run, the function executes as part of the main script — not as a fragment rerun. If selected_idx != current_idx triggers in that first execution, calling st.rerun(scope="fragment") is illegal and Streamlit aborts.

The other st.rerun(scope="app") calls in the same fragment are unaffected — scope="app" is legal from both fragment and non-fragment contexts; the restriction applies only to scope="fragment".

Fix

Wrap the call so that when fragment-scoped rerun is unavailable (initial run / outside a fragment rerun), we fall back to a full st.rerun(). The user-visible behavior is unchanged: the LLM is switched and the UI refreshes either way.

if selected_idx != current_idx:
    agent.next_llm(selected_idx)
    try:
        st.rerun(scope="fragment")
    except st.StreamlitAPIException:
        st.rerun()

frontends/stapp2.py does not use scope="fragment" and is unaffected. No other frontend touches this pattern.

Verification

  • python3 -m py_compile frontends/stapp.py — passes
  • ruff check frontends/stapp.py — no new findings introduced by this change (60 pre-existing project-style warnings remain, untouched)
  • Manual review: try/except is the documented Streamlit pattern for this scenario; behavior on subsequent fragment reruns (where scope="fragment" is legal and faster) is preserved

…ailable

Issue lsdefine#564 reports a StreamlitAPIException on startup when the LLM selectbox
in the sidebar detects a stale selected_idx (e.g. from a prior session_state).
The handler calls st.rerun(scope="fragment"), which is only legal during a
fragment rerun — on the initial script run it raises:

  StreamlitAPIException: scope="fragment" can only be specified from
  @st.fragment-decorated functions during fragment reruns.

Wrap the call so that on the initial run (or any context where fragment
rerun is unavailable) we fall back to a full st.rerun() instead of crashing.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

error at startup

1 participant