{"record":{"id":"7c46de227022e75f","repo":"microsoft/semantic-kernel","slug":"state-has-not-been-initialized","errorCode":null,"errorMessage":"State has not been initialized","messagePattern":"State has not been initialized","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"python/samples/getting_started_with_processes/step01/step01_processes.py","lineNumber":61,"sourceCode":"    def create_default_state(self) -> \"UserInputState\":\n        \"\"\"Creates the default UserInputState.\"\"\"\n        return UserInputState()\n\n    def populate_user_inputs(self):\n        \"\"\"Method to be overridden by the user to populate with custom user messages.\"\"\"\n        pass\n\n    async def activate(self, state: KernelProcessStepState[UserInputState]):\n        \"\"\"Activates the step and sets the state.\"\"\"\n        state.state = state.state or self.create_default_state()\n        self.state = state.state\n        self.populate_user_inputs()\n\n    @kernel_function(name=GET_USER_INPUT)\n    async def get_user_input(self, context: KernelProcessStepContext):\n        \"\"\"Gets the user input.\"\"\"\n        if not self.state:\n            raise ValueError(\"State has not been initialized\")\n\n        user_message = input(\"USER: \")\n\n        # print(f\"USER: {user_message}\")\n\n        if \"exit\" in user_message:\n            await context.emit_event(process_event=ChatBotEvents.Exit, data=None)\n            return\n\n        self.state.current_input_index += 1\n\n        # Emit the user input event\n        await context.emit_event(process_event=CommonEvents.UserInputReceived, data=user_message)\n\n\nclass ScriptedInputStep(UserInputStep):\n    def populate_user_inputs(self):\n        \"\"\"Override the method to populate user inputs for the chat step.\"\"\"","sourceCodeStart":43,"sourceCodeEnd":79,"githubUrl":"https://github.com/microsoft/semantic-kernel/blob/c028a0c7dc4f0814cdcbaba9d998f187a41197bf/python/samples/getting_started_with_processes/step01/step01_processes.py#L43-L79","documentation":"Thrown inside the process sample's get_user_input kernel function: self.state is still falsy when invoked. activate() is supposed to initialize state (falling back to create_default_state()), so this fires only if activate() never ran or failed to set state — i.e. the step was invoked outside the process lifecycle that calls activate first.","triggerScenarios":"Calling get_user_input() on a step instance that was not activated by the process runtime (no activate() → state stays None); unit-testing the kernel function directly on a freshly constructed step; state reset to None after a failed activation.","commonSituations":"Instantiating UserInputStep and invoking its kernel function manually in tests instead of running it through KernelProcess; a custom host that skips the activate phase.","solutions":["Run the step through the KernelProcess runtime (which calls activate() before invoking functions) rather than calling get_user_input directly.","In tests, manually call await step.activate(KernelProcessStepState(...)) before invoking get_user_input.","Ensure activate() does not swallow exceptions that would leave state unset."],"exampleFix":"# before\nstep = UserInputStep()\nawait step.get_user_input(ctx)  # state is None -> error\n# after\nstate = KernelProcessStepState(name='UserInputStep', state=UserInputState())\nawait step.activate(state)\nawait step.get_user_input(ctx)","handlingStrategy":"validation","validationCode":"assert step.state is not None, 'activate() must be called before invoking step functions'\n# In tests:\nfrom semantic_kernel.processes.kernel_process.kernel_process_step_state import KernelProcessStepState\nawait step.activate(KernelProcessStepState(name='UserInputStep', state=UserInputState()))","typeGuard":"def step_is_activated(step) -> bool:\n    return getattr(step, 'state', None) is not None","tryCatchPattern":"try:\n    await step.get_user_input(ctx)\nexcept ValueError as e:\n    if 'State has not been initialized' in str(e):\n        await step.activate(KernelProcessStepState(name='UserInputStep', state=UserInputState()))\n        await step.get_user_input(ctx)\n    else:\n        raise","preventionTips":["Run steps through the KernelProcess runtime, which activates them first.","In tests, call activate() with a populated state before any function.","Don't invoke step kernel functions on freshly constructed instances.","Make activate() robust so it always sets state."],"tags":["processes","semantic-kernel","state","activation","sample"],"backgroundTag":null,"analyzedSha":"c028a0c7dc4f0814cdcbaba9d998f187a41197bf","analyzedAt":"2026-08-13T13:48:05.040Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}