microsoft/aspire · error · JsonException

Expected start of chat message object.

Error message

Expected start of chat message object.

What it means

ReadChatMessage parses one chat message from a Utf8JsonReader and requires the current token to be StartObject. If the reader is positioned at any other token (array, string, number), a JsonException with this message is thrown. Each element of a chat-message array must be a JSON object with role and parts fields.

Solutions

  1. Fix the producer to emit chat messages as JSON objects with role and parts fields.
  2. Add a pre-check on the reader token type and skip/coerce non-object elements before calling ReadChatMessage.
  3. Validate stored payloads against the expected GenAI message schema at write time.

Example fix

// before
var (role, parts, truncated) = GenAIMessageParsingHelper.ReadChatMessage(ref reader);
// after
if (reader.TokenType != JsonTokenType.StartObject)
{
    reader.Skip(); // tolerate malformed element
    return null;
}
var (role, parts, truncated) = GenAIMessageParsingHelper.ReadChatMessage(ref reader);
Defensive patterns

Strategy: try-catch

Validate before calling

if (reader.TokenType != JsonTokenType.StartObject)
{
    reader.Skip(); // element is not a message object
}

Try / catch

try
{
    var (role, parts, truncated) = GenAIMessageParsingHelper.ReadChatMessage(ref reader);
}
catch (JsonException ex)
{
    logger.LogWarning(ex, "Chat message element is not a JSON object; skipping");
}

Prevention

When it happens

Trigger: An element inside the messages array is not an object — e.g. the array contains plain strings, numbers, or nulls instead of {"role":..., "parts":[...]} objects.

Common situations: Telemetry producers emitting non-conformant message arrays (legacy or custom exporters); hand-crafted test fixtures; schema drift where messages were stored as bare strings.

Understand the failure class

Background: Schema validation failed / invalid input schema: payload rejected because its shape doesn't match the expected schema — this error's family across 28 libraries.

Related errors


AI-assisted analysis of microsoft/aspire@25830f84bd (2026-09-16). Data as JSON: /api/errors/c18d87f75da1fe10. Report an issue: GitHub.

Appendix: source

Thrown at src/Aspire.Dashboard/Model/GenAI/GenAIMessageParsingHelper.cs:91

            catch (InvalidOperationException)
            {
                return (items, true);
            }
        }

        return (items, false);
    }

    internal static MessagePart? ReadMessagePart(ref Utf8JsonReader reader)
    {
        return JsonSerializer.Deserialize(ref reader, GenAIMessagesContext.Default.MessagePart);
    }

    internal static (string role, List<MessagePart> parts, bool partsTruncated) ReadChatMessage(ref Utf8JsonReader reader)
    {
        if (reader.TokenType != JsonTokenType.StartObject)
        {
            throw new JsonException("Expected start of chat message object.");
        }

        string? role = null;
        List<MessagePart>? parts = null;
        var partsTruncated = false;

        while (reader.Read())
        {
            if (reader.TokenType == JsonTokenType.EndObject)
            {
                break;
            }

            if (reader.TokenType != JsonTokenType.PropertyName)
            {
                throw new JsonException("Expected property name.");
            }

View on GitHub (pinned to 25830f84bd)