github/copilot-sdk · error · JsonException

Expected a non-empty string token when reading

Error message

Expected a non-empty string token when reading {typeToConvert.Name}.

What it means

After confirming the token is a string, GeneratedStringEnumJson.ReadValue reads it and throws this JsonException if the string is null, empty, or whitespace. String-backed enum types require a non-empty value to be meaningful.

Solutions

  1. Provide a valid non-empty string value in the JSON.
  2. Make the property nullable in the model so empty values can be represented as null instead.
  3. Sanitize/normalize upstream data before deserialization.
  4. Catch JsonException and surface a field-specific validation message.

Example fix

// before
{"source": ""}
// after
{"source": "user"}
Defensive patterns

Strategy: validation

Validate before calling

// reject empty enum strings before deserializing
var raw = doc.RootElement.GetProperty("source").GetString();
if (string.IsNullOrWhiteSpace(raw)) throw new ArgumentException("source must be a non-empty string");

Type guard

static bool IsValidEnumString(string? s) => !string.IsNullOrWhiteSpace(s);

Try / catch

try { var m = JsonSerializer.Deserialize<Message>(json); }
catch (JsonException ex) { throw new InvalidDataException("empty string enum value in payload", ex); }

Prevention

When it happens

Trigger: Deserializing JSON such as `"source": ""` or `"source": " "` into a type that uses GeneratedStringEnumJson as its converter.

Common situations: Server returns empty-string placeholders for unset enum fields; data pipelines that write empty defaults; user-supplied JSON with blank values.

Understand the failure class

Background: "must not be empty", "cannot be empty" — required-field validation errors across open-source libraries — this error's family across 41 libraries.

Related errors


AI-assisted analysis of github/copilot-sdk@cd8cf15dc3 (2026-09-09). Data as JSON: /api/errors/b54e712d347043bf. Report an issue: GitHub.

Appendix: source

Thrown at dotnet/src/Types.cs:31

using System.Text.Json.Nodes;
using System.Text.Json.Serialization;
using System.Threading.Tasks;

namespace GitHub.Copilot;

internal static class GeneratedStringEnumJson
{
    internal static string ReadValue(ref Utf8JsonReader reader, Type typeToConvert)
    {
        if (reader.TokenType != JsonTokenType.String)
        {
            throw new JsonException($"Expected a string token when reading {typeToConvert.Name}, but found {reader.TokenType}.");
        }

        var value = reader.GetString();
        if (string.IsNullOrWhiteSpace(value))
        {
            throw new JsonException($"Expected a non-empty string token when reading {typeToConvert.Name}.");
        }

        return value!;
    }

    internal static void WriteValue(Utf8JsonWriter writer, string value, Type typeToConvert)
    {
        if (string.IsNullOrWhiteSpace(value))
        {
            throw new JsonException($"Expected a non-empty string value when writing {typeToConvert.Name}.");
        }

        writer.WriteStringValue(value);
    }
}

/// <summary>Diagnostic IDs for the Copilot SDK.</summary>
internal static class Diagnostics

View on GitHub (pinned to cd8cf15dc3)