{"record":{"id":"bff651ff8b3663f8","repo":"microsoft/autogen","slug":"value-was-null","errorCode":null,"errorMessage":"Value was null.","messagePattern":"Value was null\\.","errorType":"exception","errorClass":"JsonException","httpStatus":null,"severity":"error","filePath":"dotnet/src/AutoGen.Anthropic/Converters/JsonPropertyNameEnumCoverter.cs","lineNumber":15,"sourceCode":"// Copyright (c) Microsoft Corporation. All rights reserved.\n// JsonPropertyNameEnumCoverter.cs\n\nusing System;\nusing System.Reflection;\nusing System.Text.Json;\nusing System.Text.Json.Serialization;\n\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","sourceCodeStart":1,"sourceCodeEnd":33,"githubUrl":"https://github.com/microsoft/autogen/blob/027ecf0a379bcc1d09956d46d12d44a3ad9cee14/dotnet/src/AutoGen.Anthropic/Converters/JsonPropertyNameEnumCoverter.cs#L1-L33","documentation":"The JsonPropertyNameEnumConverter (used for Anthropic enums serialized via [JsonPropertyName] labels) throws JsonException('Value was null.') when the JSON token for an enum property is a JSON null instead of a string. Per STJ rules a null token for a non-nullable struct enum cannot be converted, so the reader.GetString() returns null and the converter fails.","triggerScenarios":"An Anthropic API response containing \"stop_reason\": null (or any enum property serialized as null, which the API does for in-flight streaming deltas) hitting a converter-attributed enum; hand-written request JSON with explicit nulls for enum fields.","commonSituations":"Streaming responses where stop_reason is null until the final message_delta; API schema making an enum field nullable in newer versions; test fixtures with null placeholders.","solutions":["Update AutoGen.Anthropic — newer versions handle null enum tokens (skip or map to default) in this converter.","If you construct requests yourself, omit enum fields rather than sending null.","For local forks, make the converter null-tolerant: if (reader.TokenType == JsonTokenType.Null) return default; (only if your logic tolerates a default value).","Log the JSON payload to identify which enum field arrives as null and whether it matters for your flow."],"exampleFix":"// before\nstring value = reader.GetString() ?? throw new JsonException(\"Value was null.\");\n\n// after (tolerate null tokens as the enum default)\nif (reader.TokenType == JsonTokenType.Null) { reader.Read(); return default; }\nstring value = reader.GetString()!;","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try { var resp = await client.CreateChatCompletionAsync(request, ct); } catch (JsonException ex) when (ex.Message == \"Value was null.\") { /* nullable enum field (e.g. stop_reason in streaming deltas) — upgrade AutoGen.Anthropic or treat as 'not yet set' and continue */ }","preventionTips":["When hand-building request JSON, omit enum fields instead of writing null.","In streaming consumers, tolerate missing stop_reason until the final message_delta event.","Upgrade AutoGen.Anthropic when adopting new claude models; converter null-tolerance improves over time."],"tags":["anthropic","json-converter","enum","null-handling","streaming","autogen"],"backgroundTag":null,"analyzedSha":"027ecf0a379bcc1d09956d46d12d44a3ad9cee14","analyzedAt":"2026-08-15T03:38:00.719Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}