Skip to content

.Net: Bug: Gemini LLM returns 400 error when plugin input class contains nullable properties #11675

@m-soltys

Description

@m-soltys

Describe the bug
When a class with a nullable property (e.g. string? Location) is used as an input parameter to a plugin method in a Gemini LLM function call, a 400 error is returned from the Gemini API. This error does not occur when the same property is defined as non-nullable (string Location).

To Reproduce
Steps to reproduce the behavior:

  1. Define a plugin method that accepts a class with a nullable string property string? Location.
  2. Register the plugin with Semantic Kernel
  3. Invoke a prompt using GeminiPromptExecutionSettings with ToolCallBehavior = AutoInvokeKernelFunctions.
  4. Observe that the Gemini API returns a 400 error.
{
  "name": "Plugin_GetWeather",
  "description": "Get the weather for a given location.",
  "parameters": {
    "type": "object",
    "required": ["request"],
    "properties": {
      "request": {
        "type": "object",
        "properties": {
          "Location": {
            "type": ["string", "null"]
          }
        }
      }
    }
  }
}

Expected behavior
The function should be successfully invoked, and Gemini should handle nullable fields in the schema without returning an error.
The issue is caused by the type definition "type": ["string", "null"]

Screenshots
N/A

Platform

  • Language: C#
  • Source: Microsoft.SemanticKernel 1.47, Microsoft.SemanticKernel.Connectors.Google 1.47.0-alpha
  • AI model: Gemini-2.0-flash
  • IDE: All
  • OS: Windows

Additional context

var kernelBuilder = Kernel.CreateBuilder()
    .AddGoogleAIGeminiChatCompletion(
        modelId: "gemini-2.0-flash",
        apiKey: apiKey);

kernelBuilder.Plugins.AddFromType<Plugin>();

var promptExecutionSettings = new GeminiPromptExecutionSettings()
{
    ToolCallBehavior = GeminiToolCallBehavior.AutoInvokeKernelFunctions,
};

var kernel = kernelBuilder
    .Build();

var response = await kernel.InvokePromptAsync("Hi, what's the weather in New York?", new(promptExecutionSettings));

Console.WriteLine(response.ToString());

class Plugin
{
    [KernelFunction]
    [Description("Get the weather for a given location.")]
    string GetWeather(Request request)
    {
        return $"The weather in {request?.Location} is sunny.";
    }
}

class Request
{
    public string? Location { get; set; }
}

Metadata

Metadata

Assignees

Labels

.NETIssue or Pull requests regarding .NET codebugSomething isn't working

Type

Projects

Status

Sprint: Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions