microsoft/autogen · error · Exception
Agent state for {agentId} is not a valid JSON object.
Error message
Agent state for {agentId} is not a valid JSON object. What it means
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.
Source
Thrown at dotnet/src/Microsoft.AutoGen/Core.Grpc/GrpcAgentRuntime.cs:448
}
public ValueTask<AgentProxy> TryGetAgentProxyAsync(Contracts.AgentId agentId)
{
// TODO: Do we want to support getting remote agent proxies?
return ValueTask.FromResult(new AgentProxy(agentId, this));
}
public async ValueTask LoadStateAsync(JsonElement state)
{
HashSet<AgentType> registeredTypes = this._agentsContainer.RegisteredAgentTypes;
foreach (var agentIdStr in state.EnumerateObject())
{
Contracts.AgentId agentId = Contracts.AgentId.FromStr(agentIdStr.Name);
if (agentIdStr.Value.ValueKind != JsonValueKind.Object)
{
throw new Exception($"Agent state for {agentId} is not a valid JSON object.");
}
if (registeredTypes.Contains(agentId.Type))
{
IHostableAgent agent = await this._agentsContainer.EnsureAgentAsync(agentId);
await agent.LoadStateAsync(agentIdStr.Value);
}
}
}
public async ValueTask<JsonElement> SaveStateAsync()
{
Dictionary<string, JsonElement> state = new();
foreach (var agent in this._agentsContainer.LiveAgents)
{
var agentState = await agent.SaveStateAsync();
state[agent.Id.ToString()] = JsonSerializer.SerializeToElement(agentState);
}View on GitHub (pinned to 027ecf0a37)
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
Example fix
// before
// state JSON: { "myagent/instance1": 42 }
await runtime.LoadStateAsync(rootElement);
// after
// state JSON: { "myagent/instance1": { "state": 42 } }
await runtime.LoadStateAsync(rootElement); Defensive patterns
Strategy: validation
Validate before calling
foreach (var p in root.EnumerateObject())
{
if (p.Value.ValueKind != JsonValueKind.Object)
throw new InvalidDataException($"State entry {p.Name} is not an object");
} Type guard
static bool IsValidStateDocument(JsonElement e) => e.ValueKind == JsonValueKind.Object && e.EnumerateObject().All(p => p.Value.ValueKind == JsonValueKind.Object);
Try / catch
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"); } Prevention
- Treat saved state as opaque; only feed back documents produced by SaveStateAsync
- Validate the state schema before loading during migrations
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- expand_scenario expects an str or list for 'template'
- JSON content must be a dict
- JSON content must be a dict
- Failed to list MCP prompts
- Failed to get MCP prompt
AI-assisted analysis of microsoft/autogen@027ecf0a37 (2026-08-15).
Data as JSON: /api/errors/578e4f44d9e62b98.
Report an issue: GitHub.