{"record":{"id":"3c025ab7b7cd1ab4","repo":"microsoft/autogen","slug":"unable-to-convert-value-to-enum-typetoconvert","errorCode":null,"errorMessage":"Unable to convert \"{value}\" to enum {typeToConvert}.","messagePattern":"Unable to convert \"(.+?)\" to enum (.+?)\\.","errorType":"exception","errorClass":"JsonException","httpStatus":null,"severity":"error","filePath":"dotnet/src/AutoGen.Anthropic/Converters/JsonPropertyNameEnumCoverter.cs","lineNumber":26,"sourceCode":"\nnamespace AutoGen.Anthropic.Converters;\n\ninternal sealed class JsonPropertyNameEnumConverter<T> : JsonConverter<T> where T : struct, Enum\n{\n    public override T Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)\n    {\n        string value = reader.GetString() ?? throw new JsonException(\"Value was null.\");\n\n        foreach (var field in typeToConvert.GetFields())\n        {\n            var attribute = field.GetCustomAttribute<JsonPropertyNameAttribute>();\n            if (attribute?.Name == value)\n            {\n                return (T)Enum.Parse(typeToConvert, field.Name);\n            }\n        }\n\n        throw new JsonException($\"Unable to convert \\\"{value}\\\" to enum {typeToConvert}.\");\n    }\n\n    public override void Write(Utf8JsonWriter writer, T value, JsonSerializerOptions options)\n    {\n        var field = value.GetType().GetField(value.ToString());\n        var attribute = field?.GetCustomAttribute<JsonPropertyNameAttribute>();\n\n        if (attribute != null)\n        {\n            writer.WriteStringValue(attribute.Name);\n        }\n        else\n        {\n            writer.WriteStringValue(value.ToString());\n        }\n    }\n}\n","sourceCodeStart":8,"sourceCodeEnd":44,"githubUrl":"https://github.com/microsoft/autogen/blob/027ecf0a379bcc1d09956d46d12d44a3ad9cee14/dotnet/src/AutoGen.Anthropic/Converters/JsonPropertyNameEnumCoverter.cs#L8-L44","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\nthrow new JsonException($\"Unable to convert \\\"{value}\\\" to enum {typeToConvert}.\");\n\n// after (fork option: fall back to a catch-all member instead of throwing)\n// add to the enum: [JsonPropertyName(\"server_tool_use\")] ServerToolUse\nreturn (T)Enum.Parse(typeToConvert, field.Name);","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"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); }","preventionTips":["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."],"tags":["anthropic","json-converter","enum","api-drift","version-mismatch","autogen"],"backgroundTag":null,"analyzedSha":"027ecf0a379bcc1d09956d46d12d44a3ad9cee14","analyzedAt":"2026-08-15T03:38:00.719Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}