microsoft/semantic-kernel · error · ArgumentNullException

dictionary

Error message

dictionary

What it means

Thrown by GeminiMetadata.FromDictionary when the dictionary argument is null. FromDictionary converts a raw response dictionary into a GeminiMetadata; passing null is a programming error. The message 'dictionary' is the parameter name from nameof(dictionary) on the ArgumentNullException.

Source

Thrown at dotnet/src/Connectors/Connectors.Google/Models/Gemini/GeminiMetadata.cs:137

    /// The thought signature for text responses with thinking enabled.
    /// </summary>
    /// <remarks>
    /// When thinking is enabled, Gemini may return a signature on the last text part that should
    /// be included in subsequent requests to maintain optimal reasoning quality. Unlike function
    /// call signatures, text response signatures are recommended but not strictly validated.
    /// </remarks>
    public string? ThoughtSignature
    {
        get => this.GetValueFromDictionary(nameof(this.ThoughtSignature)) as string;
        internal init => this.SetValueInDictionary(value, nameof(this.ThoughtSignature));
    }

    /// <summary>
    /// Converts a dictionary to a <see cref="GeminiMetadata"/> object.
    /// </summary>
    public static GeminiMetadata FromDictionary(IReadOnlyDictionary<string, object?> dictionary) => dictionary switch
    {
        null => throw new ArgumentNullException(nameof(dictionary)),
        GeminiMetadata metadata => metadata,
        IDictionary<string, object?> metadata => new GeminiMetadata(metadata),
        _ => new GeminiMetadata(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 and skip or use an empty dictionary when null.
  2. Ensure the upstream response always produces a non-null dictionary (the connector normally does this).
  3. Pass new Dictionary<string,object?>() instead of null for absent metadata.

Example fix

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

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

Strategy: validation

Validate before calling

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

Type guard

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

Prevention

When it happens

Trigger: Calling GeminiMetadata.FromDictionary(null) directly, or a code path that supplies a null dictionary (e.g. a metadata accessor returning null from the API response being forwarded into FromDictionary).

Common situations: Forwarding a possibly-null metadata dictionary without a null check; deserialization helpers that yield null on empty responses; refactors that drop a prior non-null guarantee.

Related errors


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