GeminiDotnet is a lightweight yet fully-featured library for interacting with Google's Gemini API in modern .NET. GeminiDotnet is performant and Native AOT compatible, using System.Text.Json source-generation for JSON serialization, and has minimal dependencies.
This respository contains two packages which users can choose from. The recommended entry-point is GeminiDotnet.Extensions.AI which provides implementations of the Microsoft.Extensions.AI.Abstractions APIs. These provide common abstractions over generative AI models, allowing users to swap out their model provider without rewriting their code. Alternatively, if you'd like a direct, lightweight mapping to the Google Gemini API, you can use GeminiDotnet directly.
GeminiDotnetfor direct interaction with Gemini APIGeminiDotnet.Extensions.AIfor use withMicrosoft.Extensions.AI(recommended).
Note
Since writing this library, Google have released first-party support for C#, which you may prefer. As of Google.GenAI v0.11.0, this now includes Microsoft.Extensions.AI support too.
| Package | Latest | Downloads |
|---|---|---|
| GeminiDotnet | ||
| GeminiDotnet.Extensions.AI |
The following examples use the GeminiDotnet.Extensions.AI package.
To get incremental updates while the model continues to output its response, you can use the streaming overloads.
var options = new GeminiClientOptions { ApiKey = _apiKey, ModelId = "gemini-2.5-flash" };
IChatClient client = new GeminiChatClient(options);
await foreach (var update in client.GetStreamingResponseAsync("What is AI?"))
{
Console.Write(update);
}Using Microsoft.Extensions.AI.FunctionInvokingChatClient to handle automatic function invocation, it is simple to wire up function calling to arbitrary .NET functions.
var geminiClient = new GeminiChatClient(new GeminiClientOptions
{
ApiKey = _apiKey, ModelId = "gemini-2.5-flash"
});
[Description("Gets the current weather")]
static string GetCurrentWeather(string location, DateOnly date)
{
return $"It's raining in {location} on {date}.";
}
IChatClient client = new ChatClientBuilder(geminiClient)
.UseFunctionInvocation()
.Build();
List<ChatMessage> messages =
[
new(ChatRole.User, "Should I wear a rain coat in London tomorrow (1st Oct, 2000)? Get the current weather if needed.")
];
var options = new ChatOptions
{
Tools = [AIFunctionFactory.Create(GetCurrentWeather, nameof(GetCurrentWeather))]
};
var response = await client.GetResponseAsync(messages, options, cancellationToken);The Gemini API provides a code execution feature that enables the model to generate and run Python code and learn iteratively from the results until it arrives at a final output. You can enable and use this as follows.
var options = new GeminiClientOptions
{
ApiKey = _apiKey, ModelId = "gemini-2.5-flash"
};
IChatClient geminiClient = new GeminiChatClient(options);
var chatOptions = new ChatOptions { Tools = [new CodeInterpreterTool()] };
var response = await geminiClient.GetResponseAsync(
[new(ChatRole.User, "What is the sum of the first 42 fibonacci numbers? Generate and run code to do the calculation.")],
chatOptions,
cancellationToken);