{"record":{"id":"7c99cce00ab3ac0e","repo":"microsoft/autogen","slug":"cannot-deserialize-state-to-typeof-t","errorCode":null,"errorMessage":"Cannot deserialize state to {typeof(T)}.","messagePattern":"Cannot deserialize state to (.+?)\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"dotnet/src/Microsoft.AutoGen/AgentChat/State/SerializedState.cs","lineNumber":76,"sourceCode":"    public T As<T>()\n    {\n        if (this.deserializedValue is T value)\n        {\n            return value;\n        }\n\n        if (this.deserializedValue != null)\n        {\n            throw new InvalidOperationException($\"Cannot convert state of type {this.deserializedValue.GetType()} to {typeof(T)}.\");\n        }\n\n        if (this.jsonValue == null)\n        {\n            throw new InvalidOperationException(\"State is not initialized.\");\n        }\n\n        T? result = JsonSerializer.Deserialize<T>(this.jsonValue!.Value, SerializerOptions)\n                    ?? throw new InvalidOperationException($\"Cannot deserialize state to {typeof(T)}.\");\n\n        this.deserializedValue = result;\n\n        return result;\n    }\n\n    private object? deserializedValue;\n    private JsonElement? jsonValue;\n\n    private SerializedState(object state)\n    {\n        this.deserializedValue = state;\n    }\n\n    public SerializedState(JsonElement json)\n    {\n        this.jsonValue = json;\n    }","sourceCodeStart":58,"sourceCodeEnd":94,"githubUrl":"https://github.com/microsoft/autogen/blob/027ecf0a379bcc1d09956d46d12d44a3ad9cee14/dotnet/src/Microsoft.AutoGen/AgentChat/State/SerializedState.cs#L58-L94","documentation":"SerializedState.As<T>() throws InvalidOperationException when JsonSerializer.Deserialize<T> on the stored JsonElement returns null. System.Text.Json returns null only when the payload is the literal JSON null (or a nullable type deserialized to null), so this indicates the persisted state JSON is null-valued rather than absent — the element exists but carries no data.","triggerScenarios":"The stored JsonElement is JsonValueKind.Null (e.g. persisted state was written as \"state\": null) and As<T>() is called; also possible for nullable T where the converter yields null.","commonSituations":"A save path that serialized a null state (agent crashed or had nothing to save, wrote null instead of skipping); JSON transforms that null out optional members; mixing serializers where null round-trips differently.","solutions":["On the save side, skip writing state when the value is null instead of writing JSON null.","On the load side, treat JSON-null state as 'no state' (check jsonValue.Value.ValueKind == JsonValueKind.Null) and fall back to a default instance.","Validate checkpoint content after external edits or migrations."],"exampleFix":"// before\n// persisted: { \"state\": null }\nvar s = serializedState.As<MyState>(); // Deserialize returns null -> throws\n\n// after\nvar s = (serializedState.AsJson().ValueKind == JsonValueKind.Null)\n    ? new MyState()\n    : serializedState.As<MyState>();","handlingStrategy":"validation","validationCode":"bool IsNullPayload(SerializedState s) =>\n    s.AsJsonWorks() && s.AsJson().ValueKind == JsonValueKind.Null;\n// check ValueKind == JsonValueKind.Null before calling As<T>","typeGuard":null,"tryCatchPattern":"try { return s.As<T>(); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"Cannot deserialize state\"))\n{ /* persisted JSON null; supply default */ }","preventionTips":["Never persist JSON null as state; skip the save instead.","After any manual checkpoint edit, verify the state member is a non-null JSON object/array."],"tags":["serialization","state","json","null-payload"],"backgroundTag":null,"analyzedSha":"027ecf0a379bcc1d09956d46d12d44a3ad9cee14","analyzedAt":"2026-08-15T03:38:00.719Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}