microsoft/semantic-kernel · error · ValueError

State has not been initialized

Error message

State has not been initialized

What it means

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.

Source

Thrown at python/samples/getting_started_with_processes/step01/step01_processes.py:61

    def create_default_state(self) -> "UserInputState":
        """Creates the default UserInputState."""
        return UserInputState()

    def populate_user_inputs(self):
        """Method to be overridden by the user to populate with custom user messages."""
        pass

    async def activate(self, state: KernelProcessStepState[UserInputState]):
        """Activates the step and sets the state."""
        state.state = state.state or self.create_default_state()
        self.state = state.state
        self.populate_user_inputs()

    @kernel_function(name=GET_USER_INPUT)
    async def get_user_input(self, context: KernelProcessStepContext):
        """Gets the user input."""
        if not self.state:
            raise ValueError("State has not been initialized")

        user_message = input("USER: ")

        # print(f"USER: {user_message}")

        if "exit" in user_message:
            await context.emit_event(process_event=ChatBotEvents.Exit, data=None)
            return

        self.state.current_input_index += 1

        # Emit the user input event
        await context.emit_event(process_event=CommonEvents.UserInputReceived, data=user_message)


class ScriptedInputStep(UserInputStep):
    def populate_user_inputs(self):
        """Override the method to populate user inputs for the chat step."""

View on GitHub (pinned to c028a0c7dc)

Solutions

  1. Run the step through the KernelProcess runtime (which calls activate() before invoking functions) rather than calling get_user_input directly.
  2. In tests, manually call await step.activate(KernelProcessStepState(...)) before invoking get_user_input.
  3. Ensure activate() does not swallow exceptions that would leave state unset.

Example fix

# before
step = UserInputStep()
await step.get_user_input(ctx)  # state is None -> error
# after
state = KernelProcessStepState(name='UserInputStep', state=UserInputState())
await step.activate(state)
await step.get_user_input(ctx)
Defensive patterns

Strategy: validation

Validate before calling

assert step.state is not None, 'activate() must be called before invoking step functions'
# In tests:
from semantic_kernel.processes.kernel_process.kernel_process_step_state import KernelProcessStepState
await step.activate(KernelProcessStepState(name='UserInputStep', state=UserInputState()))

Type guard

def step_is_activated(step) -> bool:
    return getattr(step, 'state', None) is not None

Try / catch

try:
    await step.get_user_input(ctx)
except ValueError as e:
    if 'State has not been initialized' in str(e):
        await step.activate(KernelProcessStepState(name='UserInputStep', state=UserInputState()))
        await step.get_user_input(ctx)
    else:
        raise

Prevention

When it happens

Trigger: 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.

Common situations: Instantiating UserInputStep and invoking its kernel function manually in tests instead of running it through KernelProcess; a custom host that skips the activate phase.

Related errors


AI-assisted analysis of microsoft/semantic-kernel@c028a0c7dc (2026-08-13). Data as JSON: /api/errors/7c46de227022e75f. Report an issue: GitHub.