{"record":{"id":"578e4f44d9e62b98","repo":"microsoft/autogen","slug":"agent-state-for-agentid-is-not-a-valid-json-obje","errorCode":null,"errorMessage":"Agent state for {agentId} is not a valid JSON object.","messagePattern":"Agent state for (.+?) is not a valid JSON object\\.","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"dotnet/src/Microsoft.AutoGen/Core.Grpc/GrpcAgentRuntime.cs","lineNumber":448,"sourceCode":"    }\n\n    public ValueTask<AgentProxy> TryGetAgentProxyAsync(Contracts.AgentId agentId)\n    {\n        // TODO: Do we want to support getting remote agent proxies?\n        return ValueTask.FromResult(new AgentProxy(agentId, this));\n    }\n\n    public async ValueTask LoadStateAsync(JsonElement state)\n    {\n        HashSet<AgentType> registeredTypes = this._agentsContainer.RegisteredAgentTypes;\n\n        foreach (var agentIdStr in state.EnumerateObject())\n        {\n            Contracts.AgentId agentId = Contracts.AgentId.FromStr(agentIdStr.Name);\n\n            if (agentIdStr.Value.ValueKind != JsonValueKind.Object)\n            {\n                throw new Exception($\"Agent state for {agentId} is not a valid JSON object.\");\n            }\n\n            if (registeredTypes.Contains(agentId.Type))\n            {\n                IHostableAgent agent = await this._agentsContainer.EnsureAgentAsync(agentId);\n                await agent.LoadStateAsync(agentIdStr.Value);\n            }\n        }\n    }\n\n    public async ValueTask<JsonElement> SaveStateAsync()\n    {\n        Dictionary<string, JsonElement> state = new();\n        foreach (var agent in this._agentsContainer.LiveAgents)\n        {\n            var agentState = await agent.SaveStateAsync();\n            state[agent.Id.ToString()] = JsonSerializer.SerializeToElement(agentState);\n        }","sourceCodeStart":430,"sourceCodeEnd":466,"githubUrl":"https://github.com/microsoft/autogen/blob/027ecf0a379bcc1d09956d46d12d44a3ad9cee14/dotnet/src/Microsoft.AutoGen/Core.Grpc/GrpcAgentRuntime.cs#L430-L466","documentation":"Thrown by GrpcAgentRuntime.LoadStateAsync when the persisted agent-state JSON contains an entry whose value is not a JSON object. State is expected as a map of agentId string to an object that the agent's LoadStateAsync can consume; a scalar, array, or string value for any agentId is rejected.","triggerScenarios":"Calling LoadStateAsync with a JsonElement whose per-agent values are not objects, e.g. {\"type/key\": \"someString\"} or {\"type/key\": 123}; hand-edited or externally produced state files; a state document saved by a different version with a different schema.","commonSituations":"Restoring checkpoints written by an older/newer version of the framework; passing the wrong JsonDocument root (e.g. an element nested one level too deep or shallow); user-modified state JSON.","solutions":["Inspect the state JSON at the failing agentId and correct the entry so each value is a JSON object","Make sure you pass the exact element produced by SaveStateAsync (a top-level object mapping agentId strings to agent state objects)","If migrating state between versions, write a conversion step that reshapes entries into per-agent objects before loading"],"exampleFix":"// before\n// state JSON: { \"myagent/instance1\": 42 }\nawait runtime.LoadStateAsync(rootElement);\n\n// after\n// state JSON: { \"myagent/instance1\": { \"state\": 42 } }\nawait runtime.LoadStateAsync(rootElement);","handlingStrategy":"validation","validationCode":"foreach (var p in root.EnumerateObject())\n{\n    if (p.Value.ValueKind != JsonValueKind.Object)\n        throw new InvalidDataException($\"State entry {p.Name} is not an object\");\n}","typeGuard":"static bool IsValidStateDocument(JsonElement e) => e.ValueKind == JsonValueKind.Object && e.EnumerateObject().All(p => p.Value.ValueKind == JsonValueKind.Object);","tryCatchPattern":"try { await runtime.LoadStateAsync(state); } catch (Exception ex) when (ex.Message.Contains(\"not a valid JSON object\")) { _logger.LogError(ex, \"Corrupt state file; starting fresh\"); }","preventionTips":["Treat saved state as opaque; only feed back documents produced by SaveStateAsync","Validate the state schema before loading during migrations"],"tags":["state-management","json","persistence","validation"],"backgroundTag":null,"analyzedSha":"027ecf0a379bcc1d09956d46d12d44a3ad9cee14","analyzedAt":"2026-08-15T03:38:00.719Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}