microsoft/semantic-kernel · error · JsonException

GeminiPart is invalid. One and only one property among Text,

Error message

GeminiPart is invalid. One and only one property among Text, InlineData, FileData, FunctionCall, and FunctionResponse should be set.

What it means

Thrown by GeminiPart.OnDeserialized (an IJsonOnDeserialized callback) when a deserialized GeminiPart does not have exactly one of its five union properties set (Text, InlineData, FileData, FunctionCall, FunctionResponse). The IsValid() method counts non-null properties among these five and requires the total to be exactly 1. Note: Thought and ThoughtSignature are NOT counted — a part can have Thought=true alongside exactly one content property.

Source

Thrown at dotnet/src/Connectors/Connectors.Google/Core/Gemini/Models/GeminiPart.cs:89

    /// Checks whether only one property of the GeminiPart instance is not null.
    /// Returns true if only one property among Text, InlineData, FileData, FunctionCall, and FunctionResponse is not null,
    /// Otherwise, it returns false.
    /// </summary>
    public bool IsValid()
    {
        return (this.Text is not null ? 1 : 0) +
            (this.InlineData is not null ? 1 : 0) +
            (this.FileData is not null ? 1 : 0) +
            (this.FunctionCall is not null ? 1 : 0) +
            (this.FunctionResponse is not null ? 1 : 0) == 1;
    }

    /// <inheritdoc />
    public void OnDeserialized()
    {
        if (!this.IsValid())
        {
            throw new JsonException(
                "GeminiPart is invalid. One and only one property among Text, InlineData, FileData, FunctionCall, and FunctionResponse should be set.");
        }
    }

    /// <summary>
    /// Inline media bytes like image or video data.
    /// </summary>
    internal sealed class InlineDataPart
    {
        /// <summary>
        /// The IANA standard MIME type of the source data.
        /// </summary>
        /// <remarks>
        /// Acceptable values include: "image/png", "image/jpeg", "image/heic", "image/heif", "image/webp".
        /// </remarks>
        [JsonPropertyName("mimeType")]
        [JsonRequired]
        public string MimeType { get; set; } = null!;

View on GitHub (pinned to c028a0c7dc)

Solutions

  1. This is almost always a response-parsing issue, not a caller error — check the raw API response.
  2. Upgrade the Connectors.Google package to match the API version.
  3. If mocking Gemini responses in tests, ensure each part has exactly one content field.
  4. Catch JsonException around Gemini calls and log the response for diagnosis.

Example fix

null
Defensive patterns

Strategy: try-catch

Validate before calling

null

Type guard

null

Try / catch

try { var response = await client.GetChatMessageContentsAsync(history, settings, ct); }
catch (JsonException ex) when (ex.Message.Contains("GeminiPart is invalid"))
{
    logger.LogError(ex, "Gemini returned a part with invalid structure. " +
        "Possible API version mismatch. Enable response logging for details.");
    throw;
}

Prevention

When it happens

Trigger: A Gemini API response contains a 'parts' array entry where zero or more than one of the five content fields is populated. This fires automatically after System.Text.Json finishes deserializing each GeminiPart instance, before the connector processes it.

Common situations: Google's API response format changes to include a part with multiple content fields. A mock/test server returns malformed parts. An API error response that partially matches the GeminiResponse schema but has incomplete or malformed parts. Google introduces a new content field that co-exists with an existing one.

Related errors


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