# Introducing Google Gen AI .NET SDK

Author: [Mete Atamel](https://atamel.dev/) | Published: 2025-10-23 | Updated: 2025-10-24 | Topics: GenAI, VertexAI, Gemini, Google Cloud Platform, DotNet | Canonical: https://cloud.google.com/blog/topics/developers-practitioners/introducing-google-gen-ai-net-sdk


![Introducing Google Gen AI .NET SDK](https://storage.googleapis.com/gweb-cloudblog-publish/images/image.max-2500x2500.jpg)

Last year, we
[announced](https://medium.com/google-cloud/gemini-on-vertex-ai-and-google-ai-now-unified-with-the-new-google-gen-ai-sdk-094a7ebca8e6)
the [Google Gen AI SDK](https://cloud.google.com/vertex-ai/generative-ai/docs/sdks/overview) as the new unified library
for Gemini on Google AI (via the [Gemini Developer API](https://ai.google.dev/gemini-api/docs)) and Vertex AI (via the
[Vertex AI API](https://cloud.google.com/vertex-ai/generative-ai/docs/learn/overview)). At the time, it was only a
**Python** SDK. Since then, the team has been busy adding support for **Go**, **Node.js**, and **Java** but my favorite
language, **C\#**, was missing until now.

Today, I’m happy to announce that we now have a [Google Gen AI .NET SDK](https://github.com/googleapis/dotnet-genai/)!
This SDK enables C#/.NET developers use Gemini from Google AI or Vertex AI with a single unified library.

Let’s take a look at the details.

## Installation

To install the library, run the following command in your .NET project directory:

```shell
dotnet add package Google.GenAI
```

Import it to your code as follows:

```csharp
using Google.GenAI;
```

## Create a client

First, you need to create a client to talk to Gemini.

You can target Gemini on Google AI (via the [Gemini Developer API](https://ai.google.dev/gemini-api/docs)):

```csharp
using Google.GenAI;

//  Gemini Developer API
var client = new Client(apiKey: apiKey);
```

Or you can target Gemini on Vertex AI (via the [Vertex AI API](https://cloud.google.com/vertex-ai/generative-ai/docs/learn/overview)):

```csharp
// Vertex AI API
var client = new Client(
    project: project, location: location, vertexAI: true
)
```

## Generate text

Once you have the client, you can generate text with a unary response:

```csharp
var response = await client.Models.GenerateContentAsync(
      model: "gemini-2.0-flash", contents: "why is the sky blue?"
    );
Console.WriteLine(response.Candidates[0].Content.Parts[0].Text);
```

You can also generate text with a streaming response:

```csharp
await foreach (var chunk in client.Models.GenerateContentStreamAsync(
        model: "gemini-2.0-flash",
        contents: "why is the sky blue?"
    )) {
        Console.WriteLine(chunk.Candidates[0].Content.Parts[0].Text);
    }
```

## Generate image

Generating images is also straightforward with the new library:

```csharp
var response = await client.Models.GenerateImagesAsync(
  model: "imagen-3.0-generate-002",
  prompt: "Red skateboard"
);

// Save the image to a file
var image = response.GeneratedImages.First().Image;
await File.WriteAllBytesAsync("skateboard.jpg", image.ImageBytes);
```

![Skateboard](https://storage.googleapis.com/gweb-cloudblog-publish/images/skateboard.max-1100x1100.png)

## Configuration

Of course, all of the text and image generation is highly configurable.

For example, you can define a response schema and a generation configuration with system instructions and other settings for text generation as follows:

```csharp
// Define a response schema
Schema countryInfo = new()
{
    Properties = new Dictionary<string, Schema> {
          {
            "name", new Schema { Type = Type.STRING, Title = "Name" }
          },
          {
            "population", new Schema { Type = Type.INTEGER, Title = "Population" }
          },
          {
            "capital", new Schema { Type = Type.STRING, Title = "Capital" }
          },
          {
            "language", new Schema { Type = Type.STRING, Title = "Language" }
          }
    },
    PropertyOrdering = ["name", "population", "capital", "language"],
    Required = ["name", "population", "capital", "language"],
    Title = "CountryInfo",
    Type = Type.OBJECT
};

// Define a generation config
GenerateContentConfig config = new()
{
    ResponseSchema = countryInfo,
    ResponseMimeType = "application/json",
    SystemInstruction = new Content
    {
        Parts = [
              new Part {Text = "Only answer questions on countries. For everything else, say I don't know."}
        ]
    },
    MaxOutputTokens = 1024,
    Temperature = 0.1,
    TopP = 0.8,
    TopK = 40,
};

var response = await client.Models.GenerateContentAsync(
     model: "gemini-2.0-flash",
     contents: "Give me information about Cyprus",
     config: config);
```

Similarly, you can specify image generation configuration:

```csharp
GenerateImagesConfig config = new()
{
    NumberOfImages = 1,
    AspectRatio = "1:1",
    SafetyFilterLevel = SafetyFilterLevel.BLOCK_LOW_AND_ABOVE,
    PersonGeneration = PersonGeneration.DONT_ALLOW,
    IncludeSafetyAttributes = true,
    IncludeRaiReason = true,
    OutputMimeType = "image/jpeg",
};

var response = await client.Models.GenerateImagesAsync(
    model: "imagen-3.0-generate-002",
    prompt: "Red skateboard",
    config: config
);
```

## Conclusion

In this blog post, we introduced the [Google Gen AI .NET SDK](https://github.com/googleapis/dotnet-genai/) as the new
unified SDK to talk to Gemini. Here are some links to learn more:

* [API reference](https://googleapis.github.io/dotnet-genai/api/index.html)
* [Source code on GitHub](https://github.com/googleapis/dotnet-genai/)
* [DemoApp](https://github.com/googleapis/dotnet-genai/tree/main/DemoApp) with samples on Live API and more


---
*Read the full article at [https://cloud.google.com/blog/topics/developers-practitioners/introducing-google-gen-ai-net-sdk](https://cloud.google.com/blog/topics/developers-practitioners/introducing-google-gen-ai-net-sdk)*
