microsoft/autogen · error · JsonException
Unable to convert "{value}" to enum {typeToConvert}.
Error message
Unable to convert "{value}" to enum {typeToConvert}. What it means
The same JsonPropertyNameEnumConverter throws JsonException('Unable to convert "X" to enum T') when the JSON string does not match any [JsonPropertyName] label on the target enum's fields — i.e. Anthropic sent an enum value (or an error string) that this AutoGen.Anthropic build has no member for. It is the classic symptom of API-side enum expansion (new stop_reason values, new content types) outpacing the package.
Source
Thrown at dotnet/src/AutoGen.Anthropic/Converters/JsonPropertyNameEnumCoverter.cs:26
namespace AutoGen.Anthropic.Converters;
internal sealed class JsonPropertyNameEnumConverter<T> : JsonConverter<T> where T : struct, Enum
{
public override T Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
string value = reader.GetString() ?? throw new JsonException("Value was null.");
foreach (var field in typeToConvert.GetFields())
{
var attribute = field.GetCustomAttribute<JsonPropertyNameAttribute>();
if (attribute?.Name == value)
{
return (T)Enum.Parse(typeToConvert, field.Name);
}
}
throw new JsonException($"Unable to convert \"{value}\" to enum {typeToConvert}.");
}
public override void Write(Utf8JsonWriter writer, T value, JsonSerializerOptions options)
{
var field = value.GetType().GetField(value.ToString());
var attribute = field?.GetCustomAttribute<JsonPropertyNameAttribute>();
if (attribute != null)
{
writer.WriteStringValue(attribute.Name);
}
else
{
writer.WriteStringValue(value.ToString());
}
}
}
View on GitHub (pinned to 027ecf0a37)
Solutions
- Update AutoGen.Anthropic to the latest version (enum coverage for new API values lands in updates).
- Disable beta features / the prompt-caching beta header that unlocks enum values the package doesn't know.
- If you maintain a fork, add the new value with its [JsonPropertyName] label to the affected enum.
- Capture the raw JSON to identify the offending string and enum, then check the Anthropic changelog for the field.
Example fix
// before
throw new JsonException($"Unable to convert \"{value}\" to enum {typeToConvert}.");
// after (fork option: fall back to a catch-all member instead of throwing)
// add to the enum: [JsonPropertyName("server_tool_use")] ServerToolUse
return (T)Enum.Parse(typeToConvert, field.Name); Defensive patterns
Strategy: try-catch
Try / catch
try { var resp = await client.CreateChatCompletionAsync(request, ct); } catch (JsonException ex) when (ex.Message.StartsWith("Unable to convert \"")) { /* Anthropic sent an enum label this package version lacks -> upgrade AutoGen.Anthropic; meanwhile disable the beta feature that produces it */ throw new InvalidOperationException("Anthropic API value not supported by this AutoGen.Anthropic version — update the package.", ex); } Prevention
- Upgrade AutoGen.Anthropic whenever you adopt newly released claude models or enable beta headers.
- Log the raw JSON on JsonException to identify which enum field and value failed.
- Pin Anthropic model versions in production until you have smoke-tested a package upgrade.
When it happens
Trigger: Anthropic adds a stop_reason / content type / event type (e.g. 'max_tokens' vs 'max_tokens_to_stop', 'refusal', 'server_tool_use') not present in the package's enum; an error string arrives where an enum was expected; the beta header enables features whose enum labels the package predates.
Common situations: Old AutoGen.Anthropic package against a new model/API version; account enrolled in Anthropic betas; sending the request through a gateway that substitutes its own enum labels.
Related errors
- Unknown content type
- Value was null.
- Failed to deserialize response
- system message has already been set and only one system mess
- Unexpected message type: {message?.GetType()}
AI-assisted analysis of microsoft/autogen@027ecf0a37 (2026-08-15).
Data as JSON: /api/errors/3c025ab7b7cd1ab4.
Report an issue: GitHub.