streamlit/streamlit · error · StreamlitAPIException

`st.session_state.{user_key}.{k}` cannot be modified after t

Error message

`st.session_state.{user_key}.{k}` cannot be modified after the component with key `{user_key}` is instantiated.

What it means

Error "`st.session_state.{user_key}.{k}` cannot be modified after the component with key `{user_key}` is instantiated." thrown in streamlit/streamlit.

Source

Thrown at lib/streamlit/components/v2/presentation.py:75

    Returns
    -------
    WidgetValuePresenter
        A callable that merges the trigger event values into the component's
        base state for presentation in `st.session_state`.

    """

    def _present(base_value: object, session_state: SessionState) -> object:
        def _check_modification(k: str) -> None:
            ctx = get_script_run_ctx()
            if ctx is not None and component_id is not None:
                user_key = session_state._key_id_mapper.get_key_from_id(component_id)
                if (
                    component_id in ctx.shared.widget_ids_this_run
                    or user_key in ctx.shared.form_ids_this_run
                ):
                    raise StreamlitAPIException(
                        f"`st.session_state.{user_key}.{k}` cannot be modified after the component"
                        f" with key `{user_key}` is instantiated."
                    )

        # Base state must be a flat mapping; otherwise, present as-is.
        base_map: dict[str, object] | None = None
        if isinstance(base_value, dict):
            base_map = cast("dict[str, object]", base_value)

        if base_map is not None:
            # Read the trigger aggregator payloads if present
            try:
                agg_meta = session_state._new_widget_state.widget_metadata.get(
                    aggregator_id
                )
                if agg_meta is None or agg_meta.value_type != "json_trigger_value":
                    return base_value

View on GitHub (pinned to 202661fc55)

When it happens

Trigger: Thrown at lib/streamlit/components/v2/presentation.py:75 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of streamlit/streamlit@202661fc55 (2026-08-26). Data as JSON: /api/errors/e625bb76c2ae8c2b. Report an issue: GitHub.