github/copilot-sdk · error · JsonException
Expected a non-empty string value when writing
Error message
Expected a non-empty string value when writing {typeToConvert.Name}. What it means
GeneratedStringEnumJson.WriteValue validates on serialization that the string being written is non-empty. It throws this JsonException when serializing an object whose string-backed enum property is null, empty, or whitespace, because writing an empty string would produce an invalid enum representation.
Solutions
- Initialize the property with a valid enum value before serializing.
- Make the property nullable and skip it when null (JsonIgnoreCondition.WhenWritingNull).
- Validate the value at construction time (guard in the wrapper type constructor).
- Catch JsonException during serialization and log the offending member.
Example fix
// before
new MessageSource("")
// after
new MessageSource("user") Defensive patterns
Strategy: validation
Validate before calling
// validate before serializing
if (string.IsNullOrWhiteSpace(message.Source?.Value))
throw new InvalidOperationException("Source must be a non-empty value before serialization"); Type guard
static bool IsSerializable(MessageSource? s) => !string.IsNullOrWhiteSpace(s?.Value);
Try / catch
try { JsonSerializer.Serialize(payload); }
catch (JsonException ex) { log.LogError(ex, "payload contains empty string enum value"); } Prevention
- Initialize string-backed enum properties at construction
- Guard wrapper-type constructors against empty strings
- Use JsonIgnoreCondition.WhenWritingNull for optional enums
- Unit-test serialization of all model states
When it happens
Trigger: Serializing an object where a GeneratedStringEnumJson-backed property is `""`, whitespace, or null (when the converter is invoked for null), e.g. `JsonSerializer.Serialize(new Message { Source = new MessageSource("") })`.
Common situations: Objects constructed programmatically with uninitialized string fields; models populated from databases with empty defaults; default-constructed wrappers holding "".
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
- Expected a non-empty string token when reading
- Failed to serialize FFI JSON parameter.
- The value cannot be an empty string or composed entirely of…
- Failed to deserialize permission request
- Invalid tool name: must not be null or empty.
AI-assisted analysis of github/copilot-sdk@cd8cf15dc3 (2026-09-09).
Data as JSON: /api/errors/0bf46bb541639239.
Report an issue: GitHub.
Appendix: source
Thrown at dotnet/src/Types.cs:41
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
{
/// <summary>Indicates an experimental API that may change or be removed.</summary>
internal const string Experimental = "GHCP001";
}
/// <summary>
/// Log level for the Copilot runtime. Use the well-known values exposed as
/// static members (<see cref="None"/>, <see cref="Error"/>, <see cref="Warning"/>,
/// <see cref="Info"/>, <see cref="Debug"/>, <see cref="All"/>), or construct
/// your own with <see cref="CopilotLogLevel(string)"/> if the runtime acceptsView on GitHub (pinned to cd8cf15dc3)