microsoft/autogen · error · InvalidOperationException

State is not initialized.

Error message

State is not initialized.

What it means

SerializedState.AsJson throws InvalidOperationException when the object holds neither a cached JsonElement nor a deserialized value. SerializedState is a lazy dual-representation holder: it is populated either via SerializedState.Create<T>(state) (deserialized form) or via the internal JsonElement constructor. If both fields are null the instance was default-constructed or deserialized from JSON that carried no state payload.

Source

Thrown at dotnet/src/Microsoft.AutoGen/AgentChat/State/SerializedState.cs:46

    private readonly JsonSerializerOptions SerializerOptions = new()
    {
        Converters =
        {
            new SerializedStateConverter(),
        },
        TypeInfoResolver = new MessageSerializationHelpers.MessagesTypeInfoResolver(),
    };

    public JsonElement AsJson()
    {
        if (this.jsonValue != null)
        {
            return this.jsonValue.Value;
        }

        if (this.deserializedValue == null)
        {
            throw new InvalidOperationException("State is not initialized.");
        }

        this.jsonValue = JsonSerializer.SerializeToElement(this.deserializedValue, SerializerOptions);
        return this.jsonValue.Value;
    }

    public static SerializedState Create<T>(T state) where T : notnull
    {
        return new SerializedState((object)state);
    }

    public T As<T>()
    {
        if (this.deserializedValue is T value)
        {
            return value;
        }

View on GitHub (pinned to 027ecf0a37)

Solutions

  1. Check for empty/missing state before binding (validate the JSON has the expected state member).
  2. Create state via SerializedState.Create<T>(value) so deserializedValue is always populated.
  3. Upgrade path: if the checkpoint schema changed between package versions, re-create or migrate the checkpoint rather than loading it.
  4. If this fires during deserialization of persisted state, verify the state-saving side actually wrote the payload (inspect the saved JSON).

Example fix

// before
SerializedState s = JsonSerializer.Deserialize<SerializedState>(json);
var el = s.AsJson(); // throws: nothing was persisted

// after
var s = SerializedState.Create(myState); // or ensure the source JSON contains the state payload
var el = s.AsJson();
Defensive patterns

Strategy: validation

Validate before calling

bool HasState(SerializedState s) => s is not null && (s.AsJsonWorks());
// pragmatic guard: only call AsJson on state produced by SerializedState.Create<T> or verified JSON

Try / catch

try { var json = state.AsJson(); }
catch (InvalidOperationException) { /* no state persisted; treat as fresh/default state */ }

Prevention

When it happens

Trigger: Calling AsJson() on a SerializedState produced by System.Text.Json deserialization of an empty/absent state document (the parameterless/private constructors leave both fields null), or on a default(SerializedState) struct-like usage / new SerializedState() path.

Common situations: Checkpoint/restore workflows where the persisted checkpoint file is empty, truncated, or the state property was renamed between versions; deserializing a wrapper DTO whose 'state' member is missing so SerializedState binds with no data; unit tests constructing SerializedState directly.

Related errors


AI-assisted analysis of microsoft/autogen@027ecf0a37 (2026-08-15). Data as JSON: /api/errors/31ee1b969728ea29. Report an issue: GitHub.