microsoft/aspire · error · JsonException

Unexpected end of JSON while reading role value.

Error message

Unexpected end of JSON while reading role value.

What it means

In ReadChatMessage, after the "role" property name the parser must read the next token to get the role string. If reader.Read() returns false, the UTF-8 buffer ended inside the object, so the role value is unavailable and JsonException is thrown. Utf8JsonReader is non-buffering, so a truncated input is unrecoverable here.

Solutions

  1. Verify the stored message JSON is complete and well-formed (e.g. with a JSON linter) before the dashboard parses it
  2. Re-export telemetry with larger body/attribute size limits so the message is not cut mid-field
  3. Regenerate the telemetry entry from the source application if the export was truncated
  4. Handle JsonException in the visualizer path and show the raw body as an error placeholder instead of throwing
Defensive patterns

Strategy: validation

Validate before calling

using var doc = JsonDocument.Parse(json); // throws earlier with a clearer message if truncated
if (!doc.RootElement.TryGetProperty("role", out var role) || role.ValueKind != JsonValueKind.String)
    throw new FormatException("Chat message JSON is truncated or role is missing.");

Type guard

static bool HasCompleteRole(JsonElement e) =>
    e.ValueKind == JsonValueKind.Object &&
    e.TryGetProperty("role", out var r) && r.ValueKind == JsonValueKind.String;

Try / catch

try { ParseChatMessage(json); }
catch (JsonException ex) when (ex.Message.Contains("Unexpected end of JSON"))
{
    logger.LogWarning("Truncated GenAI message payload: {Reason}", ex.Message);
}

Prevention

When it happens

Trigger: Parsing a chat message JSON string that terminates immediately after "role" with no value, e.g. {"role" — the reader hits end of data instead of the role string token.

Common situations: Telemetry bodies truncated by a size cap exactly inside the role field; corrupted or partially written exported trace files; manual edits of recorded payloads.

Understand the failure class

Background: JSON parse error: "Unexpected token" / "not valid JSON" / "failed to parse" — what JSON parsers are really complaining about — this error's family across 45 libraries.

Related errors


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

Appendix: source

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

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

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

            var propertyName = reader.GetString();

            switch (propertyName)
            {
                case "role":
                    if (!reader.Read())
                    {
                        throw new JsonException("Unexpected end of JSON while reading role value.");
                    }
                    role = reader.GetString();
                    break;
                case "parts":
                    // DeserializeArrayIncrementally reads the StartArray token itself.
                    (parts, partsTruncated) = DeserializeArrayIncrementally<MessagePart>(ref reader, ReadMessagePart);
                    break;
                default:
                    if (!reader.Read())
                    {
                        throw new JsonException("Unexpected end of JSON while reading property value.");
                    }

                    if (!reader.TrySkip())
                    {
                        throw new JsonException("Unexpected end of JSON while skipping property value.");
                    }
                    break;

View on GitHub (pinned to 25830f84bd)