{"record":{"id":"31ee1b969728ea29","repo":"microsoft/autogen","slug":"state-is-not-initialized","errorCode":null,"errorMessage":"State is not initialized.","messagePattern":"State is not initialized\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"dotnet/src/Microsoft.AutoGen/AgentChat/State/SerializedState.cs","lineNumber":46,"sourceCode":"    private readonly JsonSerializerOptions SerializerOptions = new()\n    {\n        Converters =\n        {\n            new SerializedStateConverter(),\n        },\n        TypeInfoResolver = new MessageSerializationHelpers.MessagesTypeInfoResolver(),\n    };\n\n    public JsonElement AsJson()\n    {\n        if (this.jsonValue != null)\n        {\n            return this.jsonValue.Value;\n        }\n\n        if (this.deserializedValue == null)\n        {\n            throw new InvalidOperationException(\"State is not initialized.\");\n        }\n\n        this.jsonValue = JsonSerializer.SerializeToElement(this.deserializedValue, SerializerOptions);\n        return this.jsonValue.Value;\n    }\n\n    public static SerializedState Create<T>(T state) where T : notnull\n    {\n        return new SerializedState((object)state);\n    }\n\n    public T As<T>()\n    {\n        if (this.deserializedValue is T value)\n        {\n            return value;\n        }\n","sourceCodeStart":28,"sourceCodeEnd":64,"githubUrl":"https://github.com/microsoft/autogen/blob/027ecf0a379bcc1d09956d46d12d44a3ad9cee14/dotnet/src/Microsoft.AutoGen/AgentChat/State/SerializedState.cs#L28-L64","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Check for empty/missing state before binding (validate the JSON has the expected state member).","Create state via SerializedState.Create<T>(value) so deserializedValue is always populated.","Upgrade path: if the checkpoint schema changed between package versions, re-create or migrate the checkpoint rather than loading it.","If this fires during deserialization of persisted state, verify the state-saving side actually wrote the payload (inspect the saved JSON)."],"exampleFix":"// before\nSerializedState s = JsonSerializer.Deserialize<SerializedState>(json);\nvar el = s.AsJson(); // throws: nothing was persisted\n\n// after\nvar s = SerializedState.Create(myState); // or ensure the source JSON contains the state payload\nvar el = s.AsJson();","handlingStrategy":"validation","validationCode":"bool HasState(SerializedState s) => s is not null && (s.AsJsonWorks());\n// pragmatic guard: only call AsJson on state produced by SerializedState.Create<T> or verified JSON","typeGuard":null,"tryCatchPattern":"try { var json = state.AsJson(); }\ncatch (InvalidOperationException) { /* no state persisted; treat as fresh/default state */ }","preventionTips":["Always create SerializedState via SerializedState.Create<T>(value) so the holder is populated.","Validate persisted checkpoint files contain the state member before deserializing.","Add a state-version field to checkpoints so schema drift is detected before reads."],"tags":["serialization","state","json","checkpoint"],"backgroundTag":null,"analyzedSha":"027ecf0a379bcc1d09956d46d12d44a3ad9cee14","analyzedAt":"2026-08-15T03:38:00.719Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}