microsoft/semantic-kernel · error · InvalidOperationException

Failed to deserialize execution state for sessionId={session

Error message

Failed to deserialize execution state for sessionId={sessionId}, data={text}

What it means

FlowStatusProvider loads a serialized ExecutionState from the memory store for a given sessionId. If the stored text fails JSON deserialization into ExecutionState (JsonSerializer.Deserialize throws), the exception is caught and rethrown as InvalidOperationException with the sessionId and raw data text. This guards against resuming a flow from corrupted or incompatible state.

Source

Thrown at dotnet/src/Experimental/Orchestration.Flow/Execution/FlowStatusProvider.cs:58

        this._memoryStore = memoryStore;
        this._collectionName = collectionName ?? nameof(FlowStatusProvider);
    }

    /// <inheritdoc/>
    public async Task<ExecutionState> GetExecutionStateAsync(string sessionId)
    {
        var result = await (this._memoryStore.GetAsync(this._collectionName, this.GetExecutionStateStorageKey(sessionId))).ConfigureAwait(false);
        var text = result?.Metadata.Text ?? string.Empty;

        if (!string.IsNullOrEmpty(text))
        {
            try
            {
                return JsonSerializer.Deserialize<ExecutionState>(text) ?? new ExecutionState();
            }
            catch
            {
                throw new InvalidOperationException(
                    $"Failed to deserialize execution state for sessionId={sessionId}, data={text}");
            }
        }
        else
        {
            return new ExecutionState();
        }
    }

    /// <inheritdoc/>
    public async Task SaveExecutionStateAsync(string sessionId, ExecutionState state)
    {
        var json = JsonSerializer.Serialize(state);
        await this._memoryStore.UpsertAsync(this._collectionName, this.CreateMemoryRecord(this.GetExecutionStateStorageKey(sessionId), json))
            .ConfigureAwait(false);
    }

    private string GetExecutionStateStorageKey(string sessionId)

View on GitHub (pinned to c028a0c7dc)

Solutions

  1. Clear the stored execution state for the affected sessionId and restart the flow from the beginning.
  2. If the ExecutionState schema changed, implement a migration or version field so old states can be upgraded.
  3. Inspect the raw 'data' in the exception message to identify whether it's truncated, malformed, or structurally incompatible.
  4. Ensure only one version of the Orchestration.Flow package writes to the same memory store collection.
  5. Add a schema version property to ExecutionState and validate it before deserialization.

Example fix

// before — old session state from previous version causes crash
var state = await statusProvider.GetExecutionStateAsync(sessionId);

// after — catch incompatible state and restart
try {
    var state = await statusProvider.GetExecutionStateAsync(sessionId);
} catch (InvalidOperationException) {
    // stale/corrupt state — clear and start fresh
    await statusProvider.ClearAsync(sessionId);
    var state = await statusProvider.GetExecutionStateAsync(sessionId);
}
Defensive patterns

Strategy: try-catch

Validate before calling

// Pre-call: optionally probe state validity before loading
// (The provider itself does the deserialize; you can wrap GetExecutionStateAsync)
// Best prevention: include a schema version in your state and validate it.
if (!await statusProvider.HasStateAsync(sessionId))
{
    // No state — safe to start fresh; no deserialization risk
}

Try / catch

ExecutionState state;
try
{
    state = await statusProvider.GetExecutionStateAsync(sessionId);
}
catch (InvalidOperationException ex) when (ex.Message.Contains("Failed to deserialize"))
{
    logger.LogWarning(ex, "Corrupt state for session {Id}; clearing and starting fresh.", sessionId);
    await statusProvider.ClearExecutionStateAsync(sessionId);
    state = new ExecutionState(); // restart clean
}

Prevention

When it happens

Trigger: The memory store contains a text record for the session's execution-state key that is not valid JSON for the ExecutionState type. Occurs when state was written by an older/incompatible version, manually edited, or corrupted in storage.

Common situations: Deploying a new version of the Orchestration.Flow package where ExecutionState's structure changed, making old serialized states undesharializable. Using a volatile or shared memory store where another process wrote incompatible data. Manual editing or truncation of stored state text.

Related errors


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