Skip to content

Tool Function Invocation with Google Gemini Fails Due to Missing ID #6380

@ademirtug

Description

@ademirtug

Description

When attempting to use Google Gemini via Microsoft.Extensions.AI with tool function invocation enabled, the initial chat functionality works perfectly. However, as soon as a tool function is called, an exception is thrown:

Reproduction Steps

Run the following code with your actual Project ID and API key, then send a request to the /test-chat endpoint to reproduce the issue.

`

var builder = WebApplication.CreateBuilder(args);

OpenAIClientOptions opt = new OpenAIClientOptions() 
{ 
    Endpoint = new Uri("https://generativelanguage.googleapis.com/v1beta/openai"), 
    ProjectId = "MYPROJECTID" 
};

builder.Services.AddChatClient(services =>
    new OpenAI.Chat.ChatClient("models/gemini-2.0-flash", new ApiKeyCredential("MYAPIKEY"), opt)
        .AsIChatClient())
    .UseFunctionInvocation();

builder.Services.AddHttpClient();

var app = builder.Build();

string systemMessage = """
    Always respond in the user's language. Use a clear and user-friendly Markdown format to present information. 
    When using tools (including RAG-based tools) to retrieve data, ensure only relevant information is included. 
    Do not add extra commentary or explanations beyond the retrieved data.
    """;

app.MapGet("/test-chat", async (IChatClient client) =>
{
    ChatOptions chatOptions = new()
    {
        Tools = [
            AIFunctionFactory.Create(GetPersonAge),
        ],
        Temperature = 0.7f
    };

    var chatHistory = new List<ChatMessage>
    {
        new(ChatRole.System, systemMessage),
        new(ChatRole.User, "How old is Alice")
    };

    var response = await client.GetResponseAsync(chatHistory, chatOptions);
    return response.Messages;
});

app.Run();

[Description("Gets the age of a person specified by a given name.")]
int GetPersonAge(string personName)
{
    return personName switch
    {
        "Alice" => 42,
        "Bob" => 35,
        _ => 26,
    };
}

`

Expected behavior

A response indicating that Alice is 42 years old.

Actual behavior

System.ArgumentException: 'Value cannot be an empty string. (Parameter 'id')'

Regression?

No response

Known Workarounds

No response

Configuration

.NET SDK:
Version: 9.0.201
Commit: 071aaccdc2
Workload version: 9.0.200-manifests.a3a1a094
MSBuild version: 17.13.13+1c2026462

Microsoft.Extensions.AI.OpenAI 9.4.3-preview.1.25230.7

Metadata

Metadata

Assignees

Labels

area-aiMicrosoft.Extensions.AI librariesbugThis issue describes a behavior which is not expected - a bug.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions