Skip to content

Function call with IChatClient suddenly fails with 400 invalid argument error. #192

@KichangKim

Description

@KichangKim

Hi. I fonud that function call by using IChatClient suddenly fails with 400 invalid argument error. Same code works on yesterday. So I think that Google's API specification may be changed or temporarily server issue.

Here is reproduction code:

using Microsoft.Extensions.AI;
using ModelContextProtocol.Client;
using Mscc.GenerativeAI.Microsoft;
using System.ComponentModel;
using System.Diagnostics;
using System.Text;

var apiKey = "[YOUR API KEY]";
var model = "gemini-3-flash-preview";
var nativeClient = new GeminiChatClient(apiKey, model: model, logger: null);

var chatClient = nativeClient
    .AsBuilder()
    .UseFunctionInvocation(loggerFactory: null, configure: (functionInvokingClient) =>
    {
        functionInvokingClient.MaximumIterationsPerRequest = 999;
        functionInvokingClient.IncludeDetailedErrors = true;
    })
    .Build();

var chatOptions = new ChatOptions();

var tools = new List<AITool>();
tools.Add(AIFunctionFactory.Create(Add));

chatOptions.Tools = tools;
chatOptions.ToolMode = ChatToolMode.Auto;

var thinkingConfig = new Dictionary<string, object>();
thinkingConfig["ThinkingLevel"] = "Minimal";
chatOptions.AdditionalProperties = new AdditionalPropertiesDictionary()
{
    ["ThinkingConfig"] = thinkingConfig,
};

var systemMessage = new ChatMessage(ChatRole.System, "You are assistant.");
var userMessage = new ChatMessage(ChatRole.User, "What is 2 + 2?");

Console.WriteLine($"Q : {userMessage.Text}");

var output = await chatClient.GetResponseAsync<int>(new []{systemMessage, userMessage}, options: chatOptions);

Console.WriteLine($"A : {output.Result}");

int Add(int a, int b)
{
    return a + b;
}

This code returns error like this:

Unhandled exception. Mscc.GenerativeAI.Types.GeminiApiException: The request was not successful. Last API response:
{
"error": {
"code": 400,
"message": "Request contains an invalid argument.",
"status": "INVALID_ARGUMENT"
}
}

Metadata

Metadata

Labels

.NETPull requests that update .NET codearea/sdkbugSomething isn't workinggemini-api

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions