{"record":{"id":"e625bb76c2ae8c2b","repo":"streamlit/streamlit","slug":"st-session-state-user-key-k-cannot-be-modifi","errorCode":null,"errorMessage":"`st.session_state.{user_key}.{k}` cannot be modified after the component with key `{user_key}` is instantiated.","messagePattern":"`st\\.session_state\\.(.+?)\\.(.+?)` cannot be modified after the component with key `(.+?)` is instantiated\\.","errorType":"exception","errorClass":"StreamlitAPIException","httpStatus":null,"severity":"error","filePath":"lib/streamlit/components/v2/presentation.py","lineNumber":75,"sourceCode":"\n    Returns\n    -------\n    WidgetValuePresenter\n        A callable that merges the trigger event values into the component's\n        base state for presentation in `st.session_state`.\n\n    \"\"\"\n\n    def _present(base_value: object, session_state: SessionState) -> object:\n        def _check_modification(k: str) -> None:\n            ctx = get_script_run_ctx()\n            if ctx is not None and component_id is not None:\n                user_key = session_state._key_id_mapper.get_key_from_id(component_id)\n                if (\n                    component_id in ctx.shared.widget_ids_this_run\n                    or user_key in ctx.shared.form_ids_this_run\n                ):\n                    raise StreamlitAPIException(\n                        f\"`st.session_state.{user_key}.{k}` cannot be modified after the component\"\n                        f\" with key `{user_key}` is instantiated.\"\n                    )\n\n        # Base state must be a flat mapping; otherwise, present as-is.\n        base_map: dict[str, object] | None = None\n        if isinstance(base_value, dict):\n            base_map = cast(\"dict[str, object]\", base_value)\n\n        if base_map is not None:\n            # Read the trigger aggregator payloads if present\n            try:\n                agg_meta = session_state._new_widget_state.widget_metadata.get(\n                    aggregator_id\n                )\n                if agg_meta is None or agg_meta.value_type != \"json_trigger_value\":\n                    return base_value\n","sourceCodeStart":57,"sourceCodeEnd":93,"githubUrl":"https://github.com/streamlit/streamlit/blob/202661fc552db98c2ba0bb0e113d619689baf9de/lib/streamlit/components/v2/presentation.py#L57-L93","documentation":"Error \"`st.session_state.{user_key}.{k}` cannot be modified after the component with key `{user_key}` is instantiated.\" thrown in streamlit/streamlit.","triggerScenarios":"Thrown at lib/streamlit/components/v2/presentation.py:75 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":[],"exampleFix":null,"handlingStrategy":null,"validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"202661fc552db98c2ba0bb0e113d619689baf9de","analyzedAt":"2026-08-26T09:13:21.291Z","schemaVersion":2},"datasetVersion":"2026-08-26T14:46:13.012Z"}