microsoft/autogen · error · JsonException

Value was null.

Error message

Value was null.

What it means

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.

Source

Thrown at dotnet/src/AutoGen.Anthropic/Converters/JsonPropertyNameEnumCoverter.cs:15

// Copyright (c) Microsoft Corporation. All rights reserved.
// JsonPropertyNameEnumCoverter.cs

using System;
using System.Reflection;
using System.Text.Json;
using System.Text.Json.Serialization;

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>();

View on GitHub (pinned to 027ecf0a37)

Solutions

  1. Update AutoGen.Anthropic — newer versions handle null enum tokens (skip or map to default) in this converter.
  2. If you construct requests yourself, omit enum fields rather than sending null.
  3. For local forks, make the converter null-tolerant: if (reader.TokenType == JsonTokenType.Null) return default; (only if your logic tolerates a default value).
  4. Log the JSON payload to identify which enum field arrives as null and whether it matters for your flow.

Example fix

// before
string value = reader.GetString() ?? throw new JsonException("Value was null.");

// after (tolerate null tokens as the enum default)
if (reader.TokenType == JsonTokenType.Null) { reader.Read(); return default; }
string value = reader.GetString()!;
Defensive patterns

Strategy: try-catch

Try / catch

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 */ }

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of microsoft/autogen@027ecf0a37 (2026-08-15). Data as JSON: /api/errors/bff651ff8b3663f8. Report an issue: GitHub.