microsoft/semantic-kernel · error · ArgumentNullException

dictionary

Error message

dictionary

What it means

Thrown by HuggingFaceTextGenerationStreamMetadata.FromDictionary when the dictionary argument is null. Same pattern as the sibling FromDictionary methods for streaming text-generation metadata; null is a caller error. Message 'dictionary' is the parameter name.

Source

Thrown at dotnet/src/Connectors/Connectors.HuggingFace/Models/HuggingFaceTextGenerationStreamMetadata.cs:99

        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 HuggingFaceTextGenerationStreamMetadata FromDictionary(IReadOnlyDictionary<string, object?> dictionary) => dictionary switch
    {
        null => throw new ArgumentNullException(nameof(dictionary)),
        HuggingFaceTextGenerationStreamMetadata metadata => metadata,
        IDictionary<string, object?> metadata => new HuggingFaceTextGenerationStreamMetadata(metadata),
        _ => new HuggingFaceTextGenerationStreamMetadata(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 stream metadata.
  3. Guarantee the upstream stream parser produces non-null dictionaries.

Example fix

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

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

Strategy: validation

Validate before calling

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

Type guard

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

Prevention

When it happens

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

Common situations: Custom streaming pipelines forwarding possibly-null per-chunk metadata; refactors removing non-null guarantees.

Related errors


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