Introducing Google Gen AI .NET SDK

Introducing Google Gen AI .NET SDK
Introducing Google Gen AI .NET SDK

Last year, we announced the Google Gen AI SDK as the new unified library for Gemini on Google AI (via the Gemini Developer API) and Vertex AI (via the Vertex AI API). 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! 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:

dotnet add package Google.GenAI

Import it to your code as follows:

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):

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):

// 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:

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:

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:

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
Skateboard

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:

// 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:

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 as the new unified SDK to talk to Gemini. Here are some links to learn more:


See also