GenerativeAI.Gemini 1.0.2

dotnet add package GenerativeAI.Gemini --version 1.0.2
                    
NuGet\Install-Package GenerativeAI.Gemini -Version 1.0.2
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="GenerativeAI.Gemini" Version="1.0.2" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="GenerativeAI.Gemini" Version="1.0.2" />
                    
Directory.Packages.props
<PackageReference Include="GenerativeAI.Gemini" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add GenerativeAI.Gemini --version 1.0.2
                    
#r "nuget: GenerativeAI.Gemini, 1.0.2"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package GenerativeAI.Gemini@1.0.2
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=GenerativeAI.Gemini&version=1.0.2
                    
Install as a Cake Addin
#tool nuget:?package=GenerativeAI.Gemini&version=1.0.2
                    
Install as a Cake Tool

GenerativeAI.Gemini

A comprehensive .NET 9 library for seamless integration with Google's Gemini generative AI models. This package provides strongly-typed clients, dependency injection support, structured response handling, and advanced features like tool integration and schema-based generation.

Features

  • Strongly-typed API - Full IntelliSense support with comprehensive type safety
  • Dependency Injection - Native ASP.NET Core DI integration
  • Flexible Prompt Building - Support for text, files, and mixed content
  • Structured Responses - Automatic JSON schema generation and deserialization
  • Tool Integration - Built-in support for code execution, web search, and URL context
  • Async/Await - Modern async programming patterns
  • Configuration Binding - Seamless integration with .NET configuration system

Installation

Add the NuGet package to your project:

dotnet add package GenerativeAI.Gemini

Getting Started

Register Services with API Key

You can register the Gemini client in your DI container using an API key and (optionally) a model:

services.AddGemini("your-api-key", "optional-model-name");

Or bind from configuration (example for appsettings.json):

{
  "Gemini": {
    "ApiKey": "your-api-key",
    "Model": "optional-model-name"
  }
}

2. Create and Execute Prompts

Create a prompt using IGeminiClient, then add text or inline data as needed:

public class Example
{
    private readonly IGeminiClient _geminiClient;

    public Example(IGeminiClient geminiClient)
    {
        _geminiClient = geminiClient;

        // Alternative: Create GeminiClient directly without DI
        // _geminiClient = new GeminiClient("your-api-key", GeminiModel.Gemini2_5Pro);
    }

    public async Task<IEnumerable<string>> GetResponseAsync()
    {
        // Create a prompt
        var prompt = _geminiClient.CreatePrompt();

        // Add text to the prompt
        prompt.AddText("You are a helpful assistant. Answer the question: What is the capital of France?");

        // Optionally, add inline data (e.g., an image as base64)
        // prompt.AddInlineData(base64ImageString, "image/png");

        // Execute the model and get a plain text response
        var response = await _geminiClient.GenerateTextAsync(prompt);

        // Extract all the text parts
        var textParts = response.TextParts.Select(part => part.text);

        return textParts;
    }
}

3. Advanced Prompt Options

You can customize prompt options such as temperature, top-k, top-p, and max output tokens by passing a GeminiPromptOptions instance to CreatePrompt:

// Example of customizing prompt options
var options = new GeminiPromptOptions
{
    Temperature = 0.7,
    TopK = 40,
    TopP = 0.9,
    MaxTokens = 150
};

var prompt = _geminiClient.CreatePrompt(options);

License

This project is licensed under the MIT License.

Important Notes

  • This library is in preview and may change in future releases.
  • Gemini API usage may incur costs.
Product Compatible and additional computed target framework versions.
.NET net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.2 382 9/7/2025