microsoft/semantic-kernel · error · ArgumentNullException

dictionary

Error message

dictionary

What it means

Thrown by HuggingFaceTextGenerationMetadata.FromDictionary when the dictionary argument is null. Same pattern as the other metadata FromDictionary methods: converting a raw response dictionary into typed metadata; null is a programming error. Message 'dictionary' is the parameter name.

Source

Thrown at dotnet/src/Connectors/Connectors.HuggingFace/Models/HuggingFaceTextGenerationMetadata.cs:69

        get => this.GetValueFromDictionary(nameof(this.GeneratedTokens)) as int?;
        internal init => this.SetValueInDictionary(value, nameof(this.GeneratedTokens));
    }

    /// <summary>
    /// Finish reason.
    /// </summary>
    public string? FinishReason
    {
        get => this.GetValueFromDictionary(nameof(this.FinishReason)) as string;
        internal init => this.SetValueInDictionary(value, nameof(this.FinishReason));
    }

    /// <summary>
    /// Converts a dictionary to a <see cref="HuggingFaceChatCompletionMetadata"/> object.
    /// </summary>
    public static HuggingFaceTextGenerationMetadata FromDictionary(IReadOnlyDictionary<string, object?> dictionary) => dictionary switch
    {
        null => throw new ArgumentNullException(nameof(dictionary)),
        HuggingFaceTextGenerationMetadata metadata => metadata,
        IDictionary<string, object?> metadata => new HuggingFaceTextGenerationMetadata(metadata),
        _ => new HuggingFaceTextGenerationMetadata(dictionary.ToDictionary(pair => pair.Key, pair => pair.Value))
    };

    private void SetValueInDictionary(object? value, string propertyName)
        => this.Dictionary[propertyName] = value;

    private object? GetValueFromDictionary(string propertyName)
        => this.Dictionary.TryGetValue(propertyName, out var value) ? value : null;
}

View on GitHub (pinned to c028a0c7dc)

Solutions

  1. Null-check before calling FromDictionary.
  2. Use an empty dictionary for absent metadata.
  3. Guarantee the upstream source produces non-null dictionaries.

Example fix

// before
var meta = HuggingFaceTextGenerationMetadata.FromDictionary(raw);  // raw may be null

// after
var meta = raw is null ? null : HuggingFaceTextGenerationMetadata.FromDictionary(raw);
Defensive patterns

Strategy: validation

Validate before calling

static HuggingFaceTextGenerationMetadata? SafeMeta(IReadOnlyDictionary<string,object?>? d)
    => d is null ? null : HuggingFaceTextGenerationMetadata.FromDictionary(d);

Type guard

static bool IsNonNullDict(IReadOnlyDictionary<string,object?>? d) => d is not null;

Prevention

When it happens

Trigger: Calling HuggingFaceTextGenerationMetadata.FromDictionary(null) or forwarding a null dictionary from an upstream response.

Common situations: Custom text-generation pipelines forwarding possibly-null metadata; refactors removing non-null guarantees.

Related errors


AI-assisted analysis of microsoft/semantic-kernel@c028a0c7dc (2026-08-13). Data as JSON: /api/errors/561860fd62e000ed. Report an issue: GitHub.