{"record":{"id":"2aa692772925cb83","repo":"microsoft/autogen","slug":"the-group-chat-has-not-been-initialized-it-must-b","errorCode":null,"errorMessage":"The group chat has not been initialized. It must be run before it can be saved.","messagePattern":"The group chat has not been initialized\\. It must be run before it can be saved\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"dotnet/src/Microsoft.AutoGen/AgentChat/GroupChat/GroupChatBase.cs","lineNumber":305,"sourceCode":"        {\n            this.OutputSink?.Reset();\n        }\n    }\n\n    public ValueTask ResetAsync(CancellationToken cancel)\n    {\n        const string TaskAlreadyRunning = \"The group chat is currently running. It must be stopped before it can be reset.\";\n        return this.RunManager.RunAsync(\n            this.ResetInternalAsync,\n            cancel,\n            message: TaskAlreadyRunning);\n    }\n\n    public ValueTask<JsonElement> SaveStateAsync()\n    {\n        if (!this.runtimeLayer.HasRunOnce)\n        {\n            throw new InvalidOperationException(\"The group chat has not been initialized. It must be run before it can be saved.\");\n        }\n\n        const string TaskAlreadyRunning = \"The team cannot be saved while it is running.\";\n        return this.RunManager.RunAsync(\n            SaveStateInternalAsync,\n            CancellationToken.None, // TODO: Change this API?\n            message: TaskAlreadyRunning);\n\n        async ValueTask<JsonElement> SaveStateInternalAsync(CancellationToken _)\n        {\n            TeamState teamState = new()\n            {\n                TeamId = this.TeamId,\n                RuntimeState = await this.Runtime!.SaveStateAsync(),\n            };\n\n            JsonElement result = SerializedState.Create(teamState).AsJson();\n","sourceCodeStart":287,"sourceCodeEnd":323,"githubUrl":"https://github.com/microsoft/autogen/blob/027ecf0a379bcc1d09956d46d12d44a3ad9cee14/dotnet/src/Microsoft.AutoGen/AgentChat/GroupChat/GroupChatBase.cs#L287-L323","documentation":"GroupChatBase.SaveStateAsync serializes the team's runtime state, but that state (runtimeLayer.HasRunOnce) only exists after the team has been initialized by a run. Calling SaveStateAsync on a freshly constructed group chat throws InvalidOperationException telling you the chat must be run at least once before saving.","triggerScenarios":"Creating a team (e.g. RoundRobinGroupChat / graph chat) and immediately calling SaveStateAsync() before any RunAsync/StreamAsync call; or after a reset that cleared the runtime layer.","commonSituations":"Implementing checkpoint-on-start patterns (save initial state before running) which this API does not support; resuming logic that saves unconditionally; calling save after a failed/aborted first run that never completed initialization.","solutions":["Run the team once (even with an empty/minimal task) before calling SaveStateAsync","For 'initial state' persistence, persist your own construction inputs (team definition) instead of SaveStateAsync","Check the team has run before saving, and handle this exception in checkpoint code as 'nothing to save yet'"],"exampleFix":"// before\nvar team = new RoundRobinGroupChat(agents, manager);\nvar state = await team.SaveStateAsync(); // throws\n// after\nawait foreach (var _ in team.StreamAsync(\"start\", cancellationToken)) { break; } // initialize\nvar state = await team.SaveStateAsync();","handlingStrategy":"validation","validationCode":"// No public HasRunOnce; approximate by tracking whether you have run the team yourself\nif (!hasRunAtLeastOnce)\n{\n    await foreach (var _ in team.StreamAsync(\"init\", ct)) { break; } // force initialization\n    hasRunAtLeastOnce = true;\n}\nvar state = await team.SaveStateAsync();","typeGuard":null,"tryCatchPattern":"catch (InvalidOperationException ex) when (ex.Message.Contains(\"must be run before it can be saved\"))\n{\n    // nothing to checkpoint yet: skip saving, or run the team once to initialize it\n}","preventionTips":["Only call SaveStateAsync after at least one successful run","Persist the team definition (construction inputs) separately for 'initial state' checkpointing","Treat this exception as a benign 'nothing to save yet' signal in checkpoint loops"],"tags":["autogen","group-chat","state-management","lifecycle","csharp"],"backgroundTag":null,"analyzedSha":"027ecf0a379bcc1d09956d46d12d44a3ad9cee14","analyzedAt":"2026-08-15T03:38:00.719Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}