microsoft/semantic-kernel · error · ArgumentNullException

dictionary

Error message

dictionary

What it means

Thrown by HuggingFaceChatCompletionMetadata.FromDictionary when the dictionary argument is null. The method converts a raw response dictionary into a typed metadata object; null is a caller error. The message 'dictionary' is the nameof(dictionary) parameter name.

Source

Thrown at dotnet/src/Connectors/Connectors.HuggingFace/Models/HuggingFaceChatCompletionMetadata.cs:125

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

    /// <summary>
    /// The log probabilities of the completion.
    /// </summary>
    public object? LogProbs
    {
        get => this.GetValueFromDictionary(nameof(this.LogProbs));
        internal init => this.SetValueInDictionary(value, nameof(this.LogProbs));
    }

    /// <summary>
    /// Converts a dictionary to a <see cref="HuggingFaceChatCompletionMetadata"/> object.
    /// </summary>
    public static HuggingFaceChatCompletionMetadata FromDictionary(IReadOnlyDictionary<string, object?> dictionary) => dictionary switch
    {
        null => throw new ArgumentNullException(nameof(dictionary)),
        HuggingFaceChatCompletionMetadata metadata => metadata,
        IDictionary<string, object?> metadata => new HuggingFaceChatCompletionMetadata(metadata),
        _ => new HuggingFaceChatCompletionMetadata(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. Pass an empty dictionary for absent metadata instead of null.
  3. Ensure the upstream deserialization always yields a non-null dictionary.

Example fix

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

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

Strategy: validation

Validate before calling

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

Type guard

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

Prevention

When it happens

Trigger: Calling HuggingFaceChatCompletionMetadata.FromDictionary(null) directly, or passing a possibly-null dictionary produced upstream.

Common situations: Forwarding metadata from a response whose dictionary accessor returned null; refactors that removed a non-null guarantee; custom metadata adapters.

Related errors


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